-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGraph.js
More file actions
671 lines (602 loc) · 21.1 KB
/
Graph.js
File metadata and controls
671 lines (602 loc) · 21.1 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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
import Tree from './Tree';
import FibonacciHeap from './FibonacciHeap';
/**
* Creates an undirected graph structure backed by adjacency lists.
* Each edge stores a context object provided by the caller.
*
* @class Graph
* @returns {Graph} A new graph instance
*/
export default function Graph() {
var _edges = {},
MAXIMUMTOTALWEIGHT = 1,
MINIMUMWEIGHT = 2;
/**
* Adds an undirected edge between two nodes.
* If the edge already exists, it will not be replaced.
*
* @param {string} from The starting node id
* @param {string} to The ending node id
* @param {object} edge The edge context object
*/
function addEdge(from, to, edge) {
if ((_edges[from] == null || _edges[from][to] == null) && edge != null) {
if (_edges[from] == null) {
_edges[from] = {};
}
_edges[from][to] = edge;
if (_edges[to] == null) {
_edges[to] = {};
}
_edges[to][from] = edge;
}
}
/**
* Retrieves the stored edge context object for a given pair of nodes.
*
* @param {string} from The source node id
* @param {string} to The target node id
* @returns {object|null} The edge's context object, or null if none exists
*/
function edge(from, to) {
var result = null;
if (_edges[from] != null && _edges[from][to]) {
result = _edges[from][to];
}
return result;
}
/**
* Checks whether a node exists in the graph.
*
* @param {string} from The node id
* @returns {boolean} True if the node is present in the graph
*/
function hasNode(from) {
return _edges.hasOwnProperty(from);
}
/**
* Callback invoked for each edge during iteration.
*
* @callback onEdgeCallback
* @param {string} from The start node id
* @param {string} to The end node id
* @param {Object} edge The edge context object
*/
/**
* Iterates over all edges in the graph.
*
* @param {object} thisArg Execution context for the callback
* @param {onEdgeCallback} onEdge Callback invoked for each edge
*/
function loopEdges(thisArg, onEdge) {
var neighbours, fromKey, toKey;
if (onEdge != null) {
for (fromKey in _edges) {
if (_edges.hasOwnProperty(fromKey)) {
neighbours = _edges[fromKey];
if (neighbours != null) {
for (toKey in neighbours) {
if (neighbours.hasOwnProperty(toKey)) {
onEdge.call(thisArg, fromKey, toKey, neighbours[toKey]);
}
}
}
}
}
}
}
/**
* Iterates over all edges connected to a specific node.
*
* @param {object} thisArg Execution context for the callback
* @param {string} itemid The node id whose edges to iterate
* @param {onEdgeCallback} onEdge Callback invoked for each connected edge
*/
function loopNodeEdges(thisArg, itemid, onEdge) {
var neighbours, neighbourKey;
if (onEdge != null) {
neighbours = _edges[itemid];
if (neighbours != null) {
for (neighbourKey in neighbours) {
if (neighbours.hasOwnProperty(neighbourKey)) {
onEdge.call(thisArg, neighbourKey, neighbours[neighbourKey]);
}
}
}
}
}
/**
* Callback for node iteration functions.
*
* @callback onNodeCallback
* @param {string} to The node id visited
* @returns {boolean} Return true to stop traversal early
*/
/**
* Traverses all connected nodes starting from the given node.
* If no start node is provided, traversal begins with the first available node.
*
* @param {object} thisArg Execution context for the callback
* @param {string} [startNode] Optional starting node id
* @param {onNodeCallback} onItem Callback invoked for each visited node
*/
function loopNodes(thisArg, startNode, onItem) {
var processed = {};
if (startNode == null) {
for (startNode in _edges) {
if (_edges.hasOwnProperty(startNode)) {
if (!processed.hasOwnProperty[startNode]) {
_loopNodes(thisArg, startNode, processed, onItem);
}
}
}
} else {
_loopNodes(thisArg, startNode, processed, onItem);
}
}
function _loopNodes(thisArg, startNode, processed, onItem) {
var margin = [],
marginKey,
newMargin,
index, len,
neighbours, neighbourKey;
margin.push(startNode);
processed[startNode] = true;
if (onItem != null) {
while (margin.length > 0) {
newMargin = [];
/* iterate neighbours of every node on margin */
for (index = 0, len = margin.length; index < len; index += 1) {
marginKey = margin[index];
if (onItem.call(thisArg, marginKey)) {
return;
}
neighbours = _edges[marginKey];
for (neighbourKey in neighbours) {
if (neighbours.hasOwnProperty(neighbourKey) && !processed.hasOwnProperty(neighbourKey)) {
processed[neighbourKey] = true;
newMargin.push(neighbourKey);
}
}
}
margin = newMargin;
}
}
}
/**
* Returns edge weight used in certain algorithms.
*
* @callback getGraphEdgeWeightCallback
* @param {object} edge The edge context object
* @param {string} fromItem The start node id
* @param {string} toItem The end node id
* @returns {number} The weight of the edge
*/
/**
* Computes a maximum spanning tree using a priority queue.
* The graph may be disconnected; a start node is required.
*
* @param {string} startNode Node to begin spanning tree search
* @param {getGraphEdgeWeightCallback} getWeightFunc Function returning edge weight
* @returns {tree} A Tree structure containing the maximum spanning tree
*/
function getSpanningTree(startNode, getWeightFunc) {
var result = Tree(),
margin = FibonacciHeap(true),
marginNode,
parents = {}, /* if parent for item is set then it was laready visited */
neighbours, neighbourKey, neighbourWeight, currentWeight;
/* add start node to margin */
margin.add(startNode, 0, null /*parent of root node is null*/);
parents[startNode] = null;
/* search graph */
while ((marginNode = margin.extractRoot()) != null) {
/* iterate neighbours of every node on margin */
neighbours = _edges[marginNode.key];
for (neighbourKey in neighbours) {
if (neighbours.hasOwnProperty(neighbourKey) && !result.node(neighbourKey)) {
neighbourWeight = getWeightFunc != null ? getWeightFunc(neighbours[neighbourKey]) : neighbours[neighbourKey];
currentWeight = margin.getPriority(neighbourKey);
if (currentWeight == null) {
margin.add(neighbourKey, neighbourWeight, null);
parents[neighbourKey] = marginNode.key.toString();
} else {
if (currentWeight <= neighbourWeight) {
/* improve node distance */
margin.setPriority(neighbourKey, neighbourWeight);
parents[neighbourKey] = marginNode.key.toString();
}
}
}
}
/* add next margin item to resul tree */
result.add(parents[marginNode.key], marginNode.key.toString(), {});
}
return result;
}
function _findStartNode(thisArg, onEdgeWeight) {
var result = null,
fromItem, toItems, toItem,
weight = 0,
maxWeight = null;
for (fromItem in _edges) {
if (_edges.hasOwnProperty(fromItem)) {
toItems = _edges[fromItem];
weight = 0;
for (toItem in toItems) {
if (toItems.hasOwnProperty(toItem)) {
weight += onEdgeWeight.call(thisArg, toItems[toItem], fromItem, toItem);
}
}
if (weight > maxWeight || maxWeight == null) {
result = fromItem;
maxWeight = weight;
}
}
}
return result;
}
/**
* Get graph growth sequence. The sequence of graph traversing order.
*
* @param {object} thisArg The callback function invocation context
* @param {getGraphEdgeWeightCallback} getWeightFunc Callback function to get weight of an edge.
* @param {onNodeCallback} onItem A callback function to be called for every node of the growth sequence
*/
function getTotalWeightGrowthSequence(thisArg, onEdgeWeight, onItem) {
var startNode = _findStartNode(thisArg, onEdgeWeight);
_getGrowthSequence(thisArg, startNode, onEdgeWeight, onItem, MAXIMUMTOTALWEIGHT);
}
/**
* Get minimum weight graph growth sequence. The sequence of the traversing order of the graph nodes.
*
* @param {object} thisArg The callback function invocation context
* @param {string} [startNode=undefined] The optional start node id
* @param {getGraphEdgeWeightCallback} onEdgeWeight Callback function to get weight of an edge.
* @param {onNodeCallback} onItem A callback function to be called for every node of the growth sequence
*/
function getMinimumWeightGrowthSequence(thisArg, startNode, onEdgeWeight, onItem) {
_getGrowthSequence(thisArg, startNode, onEdgeWeight, onItem, MINIMUMWEIGHT);
}
function _getGrowthSequence(thisArg, startNode, onEdgeWeight, onItem, growsMode) {
var margin = {}, marginKey,
itemsToRemove = [], /* if margin item has no neighbours to expand we remove it from margin*/
hasNeighbours,
processed = {}, /* if item is set then it was already visited */
marginLength = 0, /* curent margin length */
nextMarginKey,
nextMarginWeight,
bestWeight,
neighbours, neighbourKey, neighbourWeight,
index, len;
if (onEdgeWeight != null && onItem != null) {
if (startNode == null) {
startNode = _findStartNode(thisArg, onEdgeWeight);
}
if (startNode != null) {
onItem.call(thisArg, startNode);
/* add start node to margin */
margin[startNode] = true;
marginLength += 1;
/* add startNode to result tree */
processed[startNode] = null;
/* search graph */
while (marginLength > 0) {
itemsToRemove = [];
nextMarginKey = null;
nextMarginWeight = null;
bestWeight = {};
/* iterate neighbours of every node on margin */
for (marginKey in margin) {
if (margin.hasOwnProperty(marginKey)) {
neighbours = _edges[marginKey];
hasNeighbours = false;
for (neighbourKey in neighbours) {
if (neighbours.hasOwnProperty(neighbourKey) && !processed.hasOwnProperty(neighbourKey)) {
neighbourWeight = onEdgeWeight.call(thisArg, neighbours[neighbourKey], marginKey, neighbourKey);
hasNeighbours = true;
switch (growsMode) {
case MAXIMUMTOTALWEIGHT:
if (bestWeight[neighbourKey] == null) {
bestWeight[neighbourKey] = 0;
}
bestWeight[neighbourKey] += neighbourWeight;
if (!nextMarginWeight || bestWeight[neighbourKey] > nextMarginWeight) {
nextMarginKey = neighbourKey;
nextMarginWeight = bestWeight[neighbourKey];
}
break;
case MINIMUMWEIGHT:
if (bestWeight[neighbourKey] == null) {
bestWeight[neighbourKey] = neighbourWeight;
} else {
bestWeight[neighbourKey] = Math.min(bestWeight[neighbourKey], neighbourWeight);
}
if (!nextMarginWeight || bestWeight[neighbourKey] < nextMarginWeight) {
nextMarginKey = neighbourKey;
nextMarginWeight = bestWeight[neighbourKey];
}
break;
}
}
}
if (!hasNeighbours) {
itemsToRemove.push(marginKey);
}
}
}
if (nextMarginKey == null) {
/* no items to expand to exit*/
break;
} else {
margin[nextMarginKey] = true;
marginLength += 1;
processed[nextMarginKey] = true;
/* add next margin item to result sequence */
onItem.call(thisArg, nextMarginKey);
}
for (index = 0, len = itemsToRemove.length; index < len; index += 1) {
/* delete visited node from margin */
delete margin[itemsToRemove[index]];
marginLength -= 1;
}
}
}
}
}
/**
* Callback invoked when a full path has been reconstructed.
*
* @callback onPathFoundCallback
* @param {string[]} path The node sequence forming the path
* @param {string} to The end node id
*/
/**
* Computes the shortest paths from a start node to one or more target nodes.
*
* @param {object} thisArg Execution context for callbacks
* @param {string} startNode Starting node id
* @param {string[]} endNodes Target node ids
* @param {getGraphEdgeWeightCallback} getWeightFunc Optional function returning edge weight
* @param {onPathFoundCallback} onPathFound Callback invoked when a target path is found
*/
function getShortestPath(thisArg, startNode, endNodes, getWeightFunc, onPathFound) {
var margin = FibonacciHeap(false),
distance = {},
breadcramps = {},
bestNodeOnMargin,
key,
children,
newDistance,
weight,
path,
currentNode,
endNodesHash = {},
index, len,
endsCount = 0, endsFound = 0;
/* create hash table of end nodes to find */
for (index = 0, len = endNodes.length; index < len; index += 1) {
key = endNodes[index];
if (!endNodesHash.hasOwnProperty(key)) {
endsCount += 1;
endNodesHash[key] = true;
}
}
/* add start node to margin */
margin.add(startNode, 0, null);
breadcramps[startNode] = null;
/* search graph */
while ((bestNodeOnMargin = margin.extractRoot()) != null) {
/* iterate neighbours of selected node on margin */
children = _edges[bestNodeOnMargin.key];
for (key in children) {
if (children.hasOwnProperty(key)) {
weight = 1;
if (getWeightFunc != null) {
weight = getWeightFunc.call(thisArg, children[key], bestNodeOnMargin, key);
newDistance = bestNodeOnMargin.priority + weight;
} else {
newDistance = bestNodeOnMargin.priority + 1;
}
if (weight >= 0) {
distance = margin.getPriority(key);
if (distance != null) {
if (distance > newDistance) {
margin.setPriority(key, newDistance);
breadcramps[key] = bestNodeOnMargin.key;
}
} else {
if (!breadcramps.hasOwnProperty(key)) {
margin.add(key, newDistance, null);
breadcramps[key] = bestNodeOnMargin.key;
}
}
}
}
}
if (endNodesHash.hasOwnProperty(bestNodeOnMargin.key)) {
/* trace path */
path = [];
currentNode = bestNodeOnMargin.key;
while (currentNode != null) {
path.push(currentNode);
currentNode = breadcramps[currentNode];
}
onPathFound.call(thisArg, path, bestNodeOnMargin.key);
endsFound += 1;
if (endsFound >= endsCount) {
break;
}
}
}
}
/**
* Callback used for filtering usable edges during DFS path search.
*
* @callback onPathEdgeCallback
* @param {string} from The start node id
* @param {string} to The end node id
* @param {Object} edge The edge context object
* @returns {boolean} True if the edge may be used in traversal
*/
/**
* Finds any path between two nodes using depth-first search.
*
* @param {object} thisArg Execution context for callbacks
* @param {string} startNode The start node id
* @param {string} endNode The end node id
* @param {onPathEdgeCallback} onEdge Callback deciding whether an edge is usable
* @returns {string[]} Array of node ids forming the found path
*/
function dfsPath(thisArg, startNode, endNode, onEdge) {
var margin = [],
backtrace = {};
margin.push(startNode);
backtrace[startNode] = null;
if (startNode != endNode) {
/* search graph */
while (margin.length > 0 && !backtrace.hasOwnProperty(endNode)) {
// Remove last node out of margin
var currentNode = margin[margin.length - 1];
margin.length -= 1;
// search its neighbours and add them to margin
var neighbours = _edges[currentNode];
for (var neighbour in neighbours) {
if (neighbours.hasOwnProperty(neighbour)) {
if (!backtrace.hasOwnProperty(neighbour)) {
// node is not passed yet, check edge capacity and add new neighbour to the margin
if (onEdge.call(thisArg, currentNode, neighbour, neighbours[neighbour])) {
margin.push(neighbour);
backtrace[neighbour] = currentNode;
if (neighbour == endNode) {
break;
}
}
}
}
}
}
}
currentNode = endNode;
var path = [];
while (backtrace.hasOwnProperty(currentNode)) {
path.push(currentNode);
currentNode = backtrace[currentNode];
}
var result = [];
if (path.length > 0) {
for (var index = path.length - 1; index >= 0; index -= 1) {
result.push(path[index]);
}
}
return result;
}
/**
* Computes a level graph starting from a given node.
* Levels are assigned via BFS using only edges allowed by the callback.
*
* @param {object} thisArg Execution context
* @param {string} startNode The start node id
* @param {onPathEdgeCallback} onEdge Callback deciding if an edge is valid to traverse
* @returns {Graph} A new graph representing the level structure
*/
function getLevelGraph(thisArg, startNode, onEdge) {
var level = {},
margin = [],
currentNode,
currentLevel,
neighbours;
margin.push(startNode);
level[startNode] = 1;
/* search graph level by level */
while (margin.length > 0) {
var newMargin = [];
for (var index = 0, len = margin.length; index < len; index += 1) {
currentNode = margin[index];
currentLevel = level[currentNode];
neighbours = _edges[currentNode];
for (var neighbour in neighbours) {
if (neighbours.hasOwnProperty(neighbour)) {
if (!level.hasOwnProperty(neighbour)) {
if (onEdge.call(thisArg, currentNode, neighbour, neighbours[neighbour])) {
newMargin.push(neighbour);
level[neighbour] = currentLevel + 1;
}
}
}
}
}
margin = newMargin;
}
// Create level graph, copy existing edges to the new graph
var levelGraph = Graph();
for (currentNode in _edges) {
if (level.hasOwnProperty(currentNode)) {
currentLevel = level[currentNode];
neighbours = _edges[currentNode];
for (neighbour in neighbours) {
if (level.hasOwnProperty(neighbour)) {
var neighbourLevel = level[neighbour];
if (currentLevel + 1 == neighbourLevel) {
levelGraph.addEdge(currentNode, neighbour, neighbours[neighbour]);
}
}
}
}
}
return levelGraph;
}
/**
* Performs a depth-first traversal starting at a node.
* Edge usability is determined by the onEdge callback.
*
* @param {object} thisArg Execution context
* @param {string} startNode The start node id
* @param {onPathEdgeCallback} onEdge Callback deciding edge usability
* @param {onNodeCallback} onNode Callback invoked for each newly visited node
*/
function dfsLoop(thisArg, startNode, onEdge, onNode) {
var margin = [],
visited = {},
currentNode;
margin.push(startNode);
visited[startNode] = true;
/* search graph */
while (margin.length > 0) {
// Remove last node out of margin
currentNode = margin[margin.length - 1];
margin.length -= 1;
// search its neighbours and add them to margin
var neighbours = _edges[currentNode];
for (var neighbour in neighbours) {
if (neighbours.hasOwnProperty(neighbour)) {
if (!visited.hasOwnProperty(neighbour)) {
// node is not passed yet, check edge capacity and add new neighbour to the margin
if (onEdge.call(thisArg, currentNode, neighbour, neighbours[neighbour])) {
margin.push(neighbour);
visited[neighbour] = true;
if (onNode.call(thisArg, neighbour)) {
return;
}
}
}
}
}
}
}
return {
addEdge: addEdge,
edge: edge,
hasNode: hasNode,
loopNodes: loopNodes,
loopEdges: loopEdges,
loopNodeEdges: loopNodeEdges,
getSpanningTree: getSpanningTree,
getTotalWeightGrowthSequence: getTotalWeightGrowthSequence,
getMinimumWeightGrowthSequence: getMinimumWeightGrowthSequence,
getShortestPath: getShortestPath,
dfsPath: dfsPath,
getLevelGraph: getLevelGraph,
dfsLoop: dfsLoop
};
};