forked from datavis-tech/graph-data-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
405 lines (328 loc) · 11.7 KB
/
test.js
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
// Unit tests for reactive-property.
var assert = require("assert");
// If using from the NPM package, this line would be
// var Graph = require("graph-data-structure");
var Graph = require("./index.js");
var outputGraph = require("graph-diagrams")({
// If true, writes graph files to ../graph-diagrams for visualization.
outputGraphs: false,
project: "graph-data-structure"
});
function output(graph, name){
outputGraph(graph.serialize(), name);
}
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.equal(graph.nodes().length, 2);
assert(contains(graph.nodes(), "a"));
assert(contains(graph.nodes(), "b"));
output(graph, "ab-nodes");
});
it("Should chain addNode.", function (){
var graph = Graph().addNode("a").addNode("b");
assert.equal(graph.nodes().length, 2);
assert(contains(graph.nodes(), "a"));
assert(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.equal(graph.nodes().length, 0);
});
it("Should chain removeNode.", function (){
var graph = Graph()
.addNode("a")
.addNode("b")
.removeNode("a")
.removeNode("b");
assert.equal(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.equal(graph.adjacent("a").length, 1);
assert.equal(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.equal(graph.adjacent("a").length, 1);
assert.equal(graph.adjacent("a")[0], "b");
assert.equal(graph.nodes().length, 2);
assert(contains(graph.nodes(), "a"));
assert(contains(graph.nodes(), "b"));
});
it("Should chain addEdge.", function (){
var graph = Graph().addEdge("a", "b");
assert.equal(graph.adjacent("a").length, 1);
assert.equal(graph.adjacent("a")[0], "b");
});
it("Should remove edges.", function (){
var graph = Graph();
graph.addEdge("a", "b");
graph.removeEdge("a", "b");
assert.equal(graph.adjacent("a").length, 0);
});
it("Should chain removeEdge.", function (){
var graph = Graph()
.addEdge("a", "b")
.removeEdge("a", "b");
assert.equal(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.equal(graph.nodes().length, 2);
assert(contains(graph.nodes(), "a"));
assert(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.equal(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.equal(graph.adjacent("a").length, 0);
});
it("Should compute indegree.", function (){
var graph = Graph();
graph.addEdge("a", "b");
assert.equal(graph.indegree("a"), 0);
assert.equal(graph.indegree("b"), 1);
graph.addEdge("c", "b");
assert.equal(graph.indegree("b"), 2);
});
it("Should compute outdegree.", function (){
var graph = Graph();
graph.addEdge("a", "b");
assert.equal(graph.outdegree("a"), 1);
assert.equal(graph.outdegree("b"), 0);
graph.addEdge("a", "c");
assert.equal(graph.outdegree("a"), 2);
});
});
describe("Algorithms", function() {
// 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(comesBefore(sorted, "pants", "shoes"));
assert(comesBefore(sorted, "underpants", "pants"));
assert(comesBefore(sorted, "underpants", "shoes"));
assert(comesBefore(sorted, "shirt", "jacket"));
assert(comesBefore(sorted, "shirt", "belt"));
assert(comesBefore(sorted, "belt", "jacket"));
assert.equal(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.equal(sorted.length, 2);
assert.equal(sorted[0], "b");
assert.equal(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.equal(sorted.length, 4);
assert(contains(sorted, "b"));
assert(contains(sorted, "c"));
assert(contains(sorted, "d"));
assert.equal(sorted[sorted.length - 1], "e");
assert(comesBefore(sorted, "b", "c"));
assert(comesBefore(sorted, "b", "e"));
assert(comesBefore(sorted, "c", "e"));
assert(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.equal(sorted.length, 2);
assert.equal(sorted[0], "b");
assert.equal(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(!contains(sorted, "b"));
output(graph, "cycles");
});
});
describe("Edge cases and error handling", function() {
it("Should return empty array of adjacent nodes for unknown nodes.", function (){
var graph = Graph();
assert.equal(graph.adjacent("a").length, 0);
assert.equal(graph.nodes(), 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.equal(graph.indegree("z"), 0);
});
it("Should return outdegree of 0 for unknown nodes.", function (){
var graph = Graph();
assert.equal(graph.outdegree("z"), 0);
});
});
describe("Serialization", function() {
var serialized;
function checkSerialized(graph){
assert.equal(graph.nodes.length, 3);
assert.equal(graph.links.length, 2);
assert.equal(graph.nodes[0].id, "a");
assert.equal(graph.nodes[1].id, "b");
assert.equal(graph.nodes[2].id, "c");
assert.equal(graph.links[0].source, "a");
assert.equal(graph.links[0].target, "b");
assert.equal(graph.links[1].source, "b");
assert.equal(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.equal(graph.getEdgeWeight("a", "b"), 5);
});
it("Should set edge weight via setEdgeWeight.", function (){
var graph = Graph()
.addEdge("a", "b")
.setEdgeWeight("a", "b", 5);
assert.equal(graph.getEdgeWeight("a", "b"), 5);
});
it("Should return weight of 1 if no weight set.", function (){
var graph = Graph().addEdge("a", "b");
assert.equal(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.deepEqual(graph.shortestPath("a", "b"), ["a", "b"]);
});
it("Should compute shortest path on two edges.", function (){
var graph = Graph()
.addEdge("a", "b")
.addEdge("b", "c");
assert.deepEqual(graph.shortestPath("a", "c"), ["a", "b", "c"]);
});
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.deepEqual(graph.shortestPath("s", "z"), ["s", "y", "z"]);
assert.deepEqual(graph.shortestPath("s", "x"), ["s", "y", "t", "x"]);
});
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.deepEqual(graph.shortestPath("a", "c"), ["a", "b", "c"]);
});
});
});
function contains(arr, item){
return arr.filter(function (d){
return d === item;
}).length > 0;
}
function comesBefore(arr, a, b){
var aIndex, bIndex;
arr.forEach(function (d, i){
if(d === a){ aIndex = i; }
if(d === b){ bIndex = i; }
});
return aIndex < bIndex;
}