forked from datavis-tech/graph-data-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
516 lines (440 loc) · 15.9 KB
/
test.ts
File metadata and controls
516 lines (440 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Unit tests for reactive-property.
import assert from "assert";
// If using from the NPM package, this line would be
// var Graph = require("graph-data-structure");
import Graph, { GraphInstance, Serialized } from "./index";
var outputGraph = require("graph-diagrams")({
// If true, writes graph files to ../graph-diagrams for visualization.
outputGraphs: false,
project: "graph-data-structure",
});
function output(graph: GraphInstance, name: string) {
outputGraph(graph.serialize(), name);
}
function withWeight(nodeList: any[], weight: number) {
//@ts-ignore
nodeList.weight = weight;
return nodeList;
}
describe("Graph", function () {
describe("Data structure", function () {
it("Should add nodes and list them.", function () {
var graph = Graph();
graph.addNode("a");
graph.addNode("b");
assert.strictEqual(graph.nodes().length, 2);
assert.ok(contains(graph.nodes(), "a"));
assert.ok(contains(graph.nodes(), "b"));
output(graph, "ab-nodes");
});
it("Should chain addNode.", function () {
var graph = Graph().addNode("a").addNode("b");
assert.strictEqual(graph.nodes().length, 2);
assert.ok(contains(graph.nodes(), "a"));
assert.ok(contains(graph.nodes(), "b"));
});
it("Should remove nodes.", function () {
var graph = Graph();
graph.addNode("a");
graph.addNode("b");
graph.removeNode("a");
graph.removeNode("b");
assert.strictEqual(graph.nodes().length, 0);
});
it("Should chain removeNode.", function () {
var graph = Graph()
.addNode("a")
.addNode("b")
.removeNode("a")
.removeNode("b");
assert.strictEqual(graph.nodes().length, 0);
});
it("Should add edges and query for adjacent nodes.", function () {
var graph = Graph();
graph.addNode("a");
graph.addNode("b");
graph.addEdge("a", "b");
assert.strictEqual(graph.adjacent("a").length, 1);
assert.strictEqual(graph.adjacent("a")[0], "b");
output(graph, "ab");
});
it("Should implicitly add nodes when edges are added.", function () {
var graph = Graph();
graph.addEdge("a", "b");
assert.strictEqual(graph.adjacent("a").length, 1);
assert.strictEqual(graph.adjacent("a")[0], "b");
assert.strictEqual(graph.nodes().length, 2);
assert.strictEqual(contains(graph.nodes(), "a"), true);
assert.strictEqual(contains(graph.nodes(), "b"), true);
});
it("Should chain addEdge.", function () {
var graph = Graph().addEdge("a", "b");
assert.strictEqual(graph.adjacent("a").length, 1);
assert.strictEqual(graph.adjacent("a")[0], "b");
});
it("Should remove edges.", function () {
var graph = Graph();
graph.addEdge("a", "b");
graph.removeEdge("a", "b");
assert.strictEqual(graph.adjacent("a").length, 0);
});
it("Should chain removeEdge.", function () {
var graph = Graph().addEdge("a", "b").removeEdge("a", "b");
assert.strictEqual(graph.adjacent("a").length, 0);
});
it("Should not remove nodes when edges are removed.", function () {
var graph = Graph();
graph.addEdge("a", "b");
graph.removeEdge("a", "b");
assert.strictEqual(graph.nodes().length, 2);
assert.ok(contains(graph.nodes(), "a"));
assert.ok(contains(graph.nodes(), "b"));
});
it("Should remove outgoing edges when a node is removed.", function () {
var graph = Graph();
graph.addEdge("a", "b");
graph.removeNode("a");
assert.strictEqual(graph.adjacent("a").length, 0);
});
it("Should remove incoming edges when a node is removed.", function () {
var graph = Graph();
graph.addEdge("a", "b");
graph.removeNode("b");
assert.strictEqual(graph.adjacent("a").length, 0);
});
it("Should compute indegree.", function () {
var graph = Graph();
graph.addEdge("a", "b");
assert.strictEqual(graph.indegree("a"), 0);
assert.strictEqual(graph.indegree("b"), 1);
graph.addEdge("c", "b");
assert.strictEqual(graph.indegree("b"), 2);
});
it("Should compute outdegree.", function () {
var graph = Graph();
graph.addEdge("a", "b");
assert.strictEqual(graph.outdegree("a"), 1);
assert.strictEqual(graph.outdegree("b"), 0);
graph.addEdge("a", "c");
assert.strictEqual(graph.outdegree("a"), 2);
});
});
describe("Algorithms", function () {
it("Should detect cycle.", function () {
var graph = Graph();
graph.addEdge("a", "b");
graph.addEdge("b", "a");
assert.ok(graph.hasCycle());
});
it("Should detect cycle (long).", function () {
var graph = Graph();
graph.addEdge("a", "b");
graph.addEdge("b", "c");
graph.addEdge("c", "d");
graph.addEdge("d", "a");
assert.ok(graph.hasCycle());
});
it("Should detect cycle (loop).", function () {
var graph = Graph();
graph.addEdge("a", "a");
assert.ok(graph.hasCycle());
});
it("Should not detect cycle.", function () {
var graph = Graph();
graph.addEdge("a", "b");
assert.ok(!graph.hasCycle());
});
// This example is from Cormen et al. "Introduction to Algorithms" page 550
it("Should compute topological sort.", function () {
var graph = Graph();
// Shoes depend on socks.
// Socks need to be put on before shoes.
graph.addEdge("socks", "shoes");
graph.addEdge("shirt", "belt");
graph.addEdge("shirt", "tie");
graph.addEdge("tie", "jacket");
graph.addEdge("belt", "jacket");
graph.addEdge("pants", "shoes");
graph.addEdge("underpants", "pants");
graph.addEdge("pants", "belt");
var sorted = graph.topologicalSort();
assert.ok(comesBefore(sorted, "pants", "shoes"));
assert.ok(comesBefore(sorted, "underpants", "pants"));
assert.ok(comesBefore(sorted, "underpants", "shoes"));
assert.ok(comesBefore(sorted, "shirt", "jacket"));
assert.ok(comesBefore(sorted, "shirt", "belt"));
assert.ok(comesBefore(sorted, "belt", "jacket"));
assert.strictEqual(sorted.length, 8);
output(graph, "getting-dressed");
});
it("Should compute topological sort, excluding source nodes.", function () {
var graph = Graph();
graph.addEdge("a", "b");
graph.addEdge("b", "c");
var sorted = graph.topologicalSort(["a"], false);
assert.strictEqual(sorted.length, 2);
assert.strictEqual(sorted[0], "b");
assert.strictEqual(sorted[1], "c");
output(graph, "abc");
});
it("Should compute topological sort tricky case.", function () {
var graph = Graph(); // a
// / \
graph.addEdge("a", "b"); // b |
graph.addEdge("a", "d"); // | d
graph.addEdge("b", "c"); // c |
graph.addEdge("d", "e"); // \ /
graph.addEdge("c", "e"); // e
var sorted = graph.topologicalSort(["a"], false);
assert.strictEqual(sorted.length, 4);
assert.ok(contains(sorted, "b"));
assert.ok(contains(sorted, "c"));
assert.ok(contains(sorted, "d"));
assert.strictEqual(sorted[sorted.length - 1], "e");
assert.ok(comesBefore(sorted, "b", "c"));
assert.ok(comesBefore(sorted, "b", "e"));
assert.ok(comesBefore(sorted, "c", "e"));
assert.ok(comesBefore(sorted, "d", "e"));
output(graph, "tricky-case");
});
it("Should exclude source nodes with a cycle.", function () {
var graph = Graph().addEdge("a", "b").addEdge("b", "c").addEdge("c", "a");
var sorted = graph.topologicalSort(["a"], false);
assert.strictEqual(sorted.length, 2);
assert.strictEqual(sorted[0], "b");
assert.strictEqual(sorted[1], "c");
output(graph, "cycle");
});
it("Should exclude source nodes with multiple cycles.", function () {
var graph = Graph()
.addEdge("a", "b")
.addEdge("b", "a")
.addEdge("b", "c")
.addEdge("c", "b")
.addEdge("a", "c")
.addEdge("c", "a");
var sorted = graph.topologicalSort(["a", "b"], false);
assert.ok(!contains(sorted, "b"));
output(graph, "cycles");
});
it("Should error on non-DAG topological sort", function () {
var graph = Graph();
graph.addEdge("a", "b");
graph.addEdge("b", "a");
assert.throws(graph.topologicalSort);
});
it("Should compute lowest common ancestors.", function () {
var graph = Graph()
.addEdge("a", "b")
.addEdge("b", "d")
.addEdge("c", "d")
.addEdge("b", "e")
.addEdge("c", "e")
.addEdge("d", "g")
.addEdge("e", "g")
.addNode("f");
assert.deepStrictEqual(graph.lowestCommonAncestors("a", "a"), ["a"]);
assert.deepStrictEqual(graph.lowestCommonAncestors("a", "b"), ["b"]);
assert.deepStrictEqual(graph.lowestCommonAncestors("a", "c"), ["d", "e"]);
assert.deepStrictEqual(graph.lowestCommonAncestors("a", "f"), []);
});
});
describe("Edge cases and error handling", function () {
it("Should return empty array of adjacent nodes for unknown nodes.", function () {
var graph = Graph();
assert.strictEqual(graph.adjacent("a").length, 0);
assert.strictEqual(graph.nodes().length, 0);
});
it("Should do nothing if removing an edge that does not exist.", function () {
assert.doesNotThrow(function () {
var graph = Graph();
graph.removeEdge("a", "b");
});
});
it("Should return indegree of 0 for unknown nodes.", function () {
var graph = Graph();
assert.strictEqual(graph.indegree("z"), 0);
});
it("Should return outdegree of 0 for unknown nodes.", function () {
var graph = Graph();
assert.strictEqual(graph.outdegree("z"), 0);
});
});
describe("Serialization", function () {
let serialized: Serialized;
function checkSerialized(graph: Serialized) {
assert.strictEqual(graph.nodes.length, 3);
assert.strictEqual(graph.links.length, 2);
assert.strictEqual(graph.nodes[0].id, "a");
assert.strictEqual(graph.nodes[1].id, "b");
assert.strictEqual(graph.nodes[2].id, "c");
assert.strictEqual(graph.links[0].source, "a");
assert.strictEqual(graph.links[0].target, "b");
assert.strictEqual(graph.links[1].source, "b");
assert.strictEqual(graph.links[1].target, "c");
}
it("Should serialize a graph.", function () {
var graph = Graph().addEdge("a", "b").addEdge("b", "c");
serialized = graph.serialize();
checkSerialized(serialized);
});
it("Should deserialize a graph.", function () {
var graph = Graph();
graph.deserialize(serialized);
checkSerialized(graph.serialize());
});
it("Should chain deserialize a graph.", function () {
var graph = Graph().deserialize(serialized);
checkSerialized(graph.serialize());
});
it("Should deserialize a graph passed to constructor.", function () {
var graph = Graph(serialized);
checkSerialized(graph.serialize());
});
});
describe("Edge Weights", function () {
it("Should set and get an edge weight.", function () {
var graph = Graph().addEdge("a", "b", 5);
assert.strictEqual(graph.getEdgeWeight("a", "b"), 5);
});
it("Should set edge weight via setEdgeWeight.", function () {
var graph = Graph().addEdge("a", "b").setEdgeWeight("a", "b", 5);
assert.strictEqual(graph.getEdgeWeight("a", "b"), 5);
});
it("Should return weight of 1 if no weight set.", function () {
var graph = Graph().addEdge("a", "b");
assert.strictEqual(graph.getEdgeWeight("a", "b"), 1);
});
});
describe("Dijkstra's Shortest Path Algorithm", function () {
it("Should compute shortest path on a single edge.", function () {
var graph = Graph().addEdge("a", "b");
assert.deepStrictEqual(
graph.shortestPath("a", "b"),
withWeight(["a", "b"], 1)
);
});
it("Should compute shortest path on two edges.", function () {
var graph = Graph().addEdge("a", "b").addEdge("b", "c");
assert.deepStrictEqual(
graph.shortestPath("a", "c"),
withWeight(["a", "b", "c"], 2)
);
});
it("Should compute shortest path on example from Cormen text (p. 659).", function () {
var graph = Graph()
.addEdge("s", "t", 10)
.addEdge("s", "y", 5)
.addEdge("t", "y", 2)
.addEdge("y", "t", 3)
.addEdge("t", "x", 1)
.addEdge("y", "x", 9)
.addEdge("y", "z", 2)
.addEdge("x", "z", 4)
.addEdge("z", "x", 6);
assert.deepStrictEqual(
graph.shortestPath("s", "z"),
withWeight(["s", "y", "z"], 5 + 2)
);
assert.deepStrictEqual(
graph.shortestPath("s", "x"),
withWeight(["s", "y", "t", "x"], 5 + 3 + 1)
);
});
it("Should throw error if source node not in graph.", function () {
var graph = Graph().addEdge("b", "c");
assert.throws(() => graph.shortestPath("a", "c"), /Source node/);
});
it("Should throw error if dest node not in graph.", function () {
var graph = Graph().addEdge("b", "c");
assert.throws(() => graph.shortestPath("b", "g"), /Destination node/);
});
it("Should throw error if no path exists.", function () {
var graph = Graph().addEdge("a", "b").addEdge("d", "e");
assert.throws(() => graph.shortestPath("a", "e"), /No path/);
});
it("Should be robust to disconnected subgraphs.", function () {
var graph = Graph().addEdge("a", "b").addEdge("b", "c").addEdge("d", "e");
assert.deepStrictEqual(
graph.shortestPath("a", "c"),
withWeight(["a", "b", "c"], 2)
);
});
});
describe("hadEdge", function () {
it("Should compute hasEdge.", function () {
var graph = Graph().addEdge("a", "b");
assert.strictEqual(graph.hasEdge("a", "b"), true);
assert.strictEqual(graph.hasEdge("b", "a"), false);
assert.strictEqual(graph.hasEdge("c", "a"), false);
});
});
describe("walk", function () {
it("Should traverse through all the nodes once", function () {
var graph = Graph()
.addEdge("s", "t", 10)
.addEdge("s", "y", 5)
.addEdge("t", "y", 2)
.addEdge("t", "x", 1)
.addEdge("y", "x", 9)
.addEdge("y", "z", 2)
.addEdge("x", "z", 4);
const nodes: string[] = [];
graph.walk((node: string) => {
nodes.push(node);
});
assert.deepStrictEqual(nodes, ["s", "t", "y", "x", "z"]);
});
});
describe("get cycles", function () {
it("Should get the cycles", function () {
var graph = Graph()
.addEdge("a", "b")
.addEdge("b", "a")
.addEdge("b", "c")
.addEdge("c", "b")
.addEdge("d", "e");
const cycles = graph.getCycles(["a"]);
assert.deepStrictEqual(cycles, [
["a", "b"],
["b", "c"],
]);
});
});
describe("edge ids", function () {
it("Should add ids to edges", () => {
let graph = Graph()
.addEdge("a", "b", undefined, "123")
.addEdge("b", "a", undefined, "456")
.addEdge("b", "c", undefined, "789");
assert.deepStrictEqual(graph.getEdgeId("a", "b"), "123");
assert.deepStrictEqual(graph.getEdgeId("b", "a"), "456");
assert.deepStrictEqual(graph.getEdgeId("b", "c"), "789");
});
it("Should remove ids when an edge is removed", () => {
let graph = Graph().addEdge("a", "b", undefined, "123");
graph.removeEdge("a", "b");
assert.strictEqual(graph.getEdgeId("a", "b"), undefined);
});
});
});
function contains<T>(arr: T[], item: T) {
return (
arr.filter(function (d) {
return d === item;
}).length > 0
);
}
function comesBefore<T>(arr: T[], a: T, b: T) {
let aIndex: number;
let bIndex: number;
arr.forEach(function (d, i) {
if (d === a) {
aIndex = i;
}
if (d === b) {
bIndex = i;
}
});
return aIndex! < bIndex!;
}