graphflux 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1396 @@
1
+ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.graphlib = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2
+ /**
3
+ * Copyright (c) 2014, Chris Pettitt
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * 1. Redistributions of source code must retain the above copyright notice, this
10
+ * list of conditions and the following disclaimer.
11
+ *
12
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ * this list of conditions and the following disclaimer in the documentation
14
+ * and/or other materials provided with the distribution.
15
+ *
16
+ * 3. Neither the name of the copyright holder nor the names of its contributors
17
+ * may be used to endorse or promote products derived from this software without
18
+ * specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+
32
+ var lib = require("./lib");
33
+
34
+ module.exports = {
35
+ Graph: lib.Graph,
36
+ json: require("./lib/json"),
37
+ alg: require("./lib/alg"),
38
+ version: lib.version
39
+ };
40
+
41
+ },{"./lib":17,"./lib/alg":8,"./lib/json":18}],2:[function(require,module,exports){
42
+ module.exports = components;
43
+
44
+ function components(g) {
45
+ var visited = {};
46
+ var cmpts = [];
47
+ var cmpt;
48
+
49
+ function dfs(v) {
50
+ if (Object.hasOwn(visited, v)) return;
51
+ visited[v] = true;
52
+ cmpt.push(v);
53
+ g.successors(v).forEach(dfs);
54
+ g.predecessors(v).forEach(dfs);
55
+ }
56
+
57
+ g.nodes().forEach(function(v) {
58
+ cmpt = [];
59
+ dfs(v);
60
+ if (cmpt.length) {
61
+ cmpts.push(cmpt);
62
+ }
63
+ });
64
+
65
+ return cmpts;
66
+ }
67
+
68
+ },{}],3:[function(require,module,exports){
69
+ module.exports = dfs;
70
+
71
+ /*
72
+ * A helper that preforms a pre- or post-order traversal on the input graph
73
+ * and returns the nodes in the order they were visited. If the graph is
74
+ * undirected then this algorithm will navigate using neighbors. If the graph
75
+ * is directed then this algorithm will navigate using successors.
76
+ *
77
+ * If the order is not "post", it will be treated as "pre".
78
+ */
79
+ function dfs(g, vs, order) {
80
+ if (!Array.isArray(vs)) {
81
+ vs = [vs];
82
+ }
83
+
84
+ var navigation = g.isDirected() ? v => g.successors(v) : v => g.neighbors(v);
85
+ var orderFunc = order === "post" ? postOrderDfs : preOrderDfs;
86
+
87
+ var acc = [];
88
+ var visited = {};
89
+ vs.forEach(v => {
90
+ if (!g.hasNode(v)) {
91
+ throw new Error("Graph does not have node: " + v);
92
+ }
93
+
94
+ orderFunc(v, navigation, visited, acc);
95
+ });
96
+
97
+ return acc;
98
+ }
99
+
100
+ function postOrderDfs(v, navigation, visited, acc) {
101
+ var stack = [[v, false]];
102
+ while (stack.length > 0) {
103
+ var curr = stack.pop();
104
+ if (curr[1]) {
105
+ acc.push(curr[0]);
106
+ } else {
107
+ if (!Object.hasOwn(visited, curr[0])) {
108
+ visited[curr[0]] = true;
109
+ stack.push([curr[0], true]);
110
+ forEachRight(navigation(curr[0]), w => stack.push([w, false]));
111
+ }
112
+ }
113
+ }
114
+ }
115
+
116
+ function preOrderDfs(v, navigation, visited, acc) {
117
+ var stack = [v];
118
+ while (stack.length > 0) {
119
+ var curr = stack.pop();
120
+ if (!Object.hasOwn(visited, curr)) {
121
+ visited[curr] = true;
122
+ acc.push(curr);
123
+ forEachRight(navigation(curr), w => stack.push(w));
124
+ }
125
+ }
126
+ }
127
+
128
+ function forEachRight(array, iteratee) {
129
+ var length = array.length;
130
+ while (length--) {
131
+ iteratee(array[length], length, array);
132
+ }
133
+
134
+ return array;
135
+ }
136
+
137
+ },{}],4:[function(require,module,exports){
138
+ var dijkstra = require("./dijkstra");
139
+
140
+ module.exports = dijkstraAll;
141
+
142
+ function dijkstraAll(g, weightFunc, edgeFunc) {
143
+ return g.nodes().reduce(function(acc, v) {
144
+ acc[v] = dijkstra(g, v, weightFunc, edgeFunc);
145
+ return acc;
146
+ }, {});
147
+ }
148
+
149
+ },{"./dijkstra":5}],5:[function(require,module,exports){
150
+ var PriorityQueue = require("../data/priority-queue");
151
+
152
+ module.exports = dijkstra;
153
+
154
+ var DEFAULT_WEIGHT_FUNC = () => 1;
155
+
156
+ function dijkstra(g, source, weightFn, edgeFn) {
157
+ return runDijkstra(g, String(source),
158
+ weightFn || DEFAULT_WEIGHT_FUNC,
159
+ edgeFn || function(v) { return g.outEdges(v); });
160
+ }
161
+
162
+ function runDijkstra(g, source, weightFn, edgeFn) {
163
+ var results = {};
164
+ var pq = new PriorityQueue();
165
+ var v, vEntry;
166
+
167
+ var updateNeighbors = function(edge) {
168
+ var w = edge.v !== v ? edge.v : edge.w;
169
+ var wEntry = results[w];
170
+ var weight = weightFn(edge);
171
+ var distance = vEntry.distance + weight;
172
+
173
+ if (weight < 0) {
174
+ throw new Error("dijkstra does not allow negative edge weights. " +
175
+ "Bad edge: " + edge + " Weight: " + weight);
176
+ }
177
+
178
+ if (distance < wEntry.distance) {
179
+ wEntry.distance = distance;
180
+ wEntry.predecessor = v;
181
+ pq.decrease(w, distance);
182
+ }
183
+ };
184
+
185
+ g.nodes().forEach(function(v) {
186
+ var distance = v === source ? 0 : Number.POSITIVE_INFINITY;
187
+ results[v] = { distance: distance };
188
+ pq.add(v, distance);
189
+ });
190
+
191
+ while (pq.size() > 0) {
192
+ v = pq.removeMin();
193
+ vEntry = results[v];
194
+ if (vEntry.distance === Number.POSITIVE_INFINITY) {
195
+ break;
196
+ }
197
+
198
+ edgeFn(v).forEach(updateNeighbors);
199
+ }
200
+
201
+ return results;
202
+ }
203
+
204
+ },{"../data/priority-queue":15}],6:[function(require,module,exports){
205
+ var tarjan = require("./tarjan");
206
+
207
+ module.exports = findCycles;
208
+
209
+ function findCycles(g) {
210
+ return tarjan(g).filter(function(cmpt) {
211
+ return cmpt.length > 1 || (cmpt.length === 1 && g.hasEdge(cmpt[0], cmpt[0]));
212
+ });
213
+ }
214
+
215
+ },{"./tarjan":13}],7:[function(require,module,exports){
216
+ module.exports = floydWarshall;
217
+
218
+ var DEFAULT_WEIGHT_FUNC = () => 1;
219
+
220
+ function floydWarshall(g, weightFn, edgeFn) {
221
+ return runFloydWarshall(g,
222
+ weightFn || DEFAULT_WEIGHT_FUNC,
223
+ edgeFn || function(v) { return g.outEdges(v); });
224
+ }
225
+
226
+ function runFloydWarshall(g, weightFn, edgeFn) {
227
+ var results = {};
228
+ var nodes = g.nodes();
229
+
230
+ nodes.forEach(function(v) {
231
+ results[v] = {};
232
+ results[v][v] = { distance: 0 };
233
+ nodes.forEach(function(w) {
234
+ if (v !== w) {
235
+ results[v][w] = { distance: Number.POSITIVE_INFINITY };
236
+ }
237
+ });
238
+ edgeFn(v).forEach(function(edge) {
239
+ var w = edge.v === v ? edge.w : edge.v;
240
+ var d = weightFn(edge);
241
+ results[v][w] = { distance: d, predecessor: v };
242
+ });
243
+ });
244
+
245
+ nodes.forEach(function(k) {
246
+ var rowK = results[k];
247
+ nodes.forEach(function(i) {
248
+ var rowI = results[i];
249
+ nodes.forEach(function(j) {
250
+ var ik = rowI[k];
251
+ var kj = rowK[j];
252
+ var ij = rowI[j];
253
+ var altDistance = ik.distance + kj.distance;
254
+ if (altDistance < ij.distance) {
255
+ ij.distance = altDistance;
256
+ ij.predecessor = kj.predecessor;
257
+ }
258
+ });
259
+ });
260
+ });
261
+
262
+ return results;
263
+ }
264
+
265
+ },{}],8:[function(require,module,exports){
266
+ module.exports = {
267
+ components: require("./components"),
268
+ dijkstra: require("./dijkstra"),
269
+ dijkstraAll: require("./dijkstra-all"),
270
+ findCycles: require("./find-cycles"),
271
+ floydWarshall: require("./floyd-warshall"),
272
+ isAcyclic: require("./is-acyclic"),
273
+ postorder: require("./postorder"),
274
+ preorder: require("./preorder"),
275
+ prim: require("./prim"),
276
+ tarjan: require("./tarjan"),
277
+ topsort: require("./topsort")
278
+ };
279
+
280
+ },{"./components":2,"./dijkstra":5,"./dijkstra-all":4,"./find-cycles":6,"./floyd-warshall":7,"./is-acyclic":9,"./postorder":10,"./preorder":11,"./prim":12,"./tarjan":13,"./topsort":14}],9:[function(require,module,exports){
281
+ var topsort = require("./topsort");
282
+
283
+ module.exports = isAcyclic;
284
+
285
+ function isAcyclic(g) {
286
+ try {
287
+ topsort(g);
288
+ } catch (e) {
289
+ if (e instanceof topsort.CycleException) {
290
+ return false;
291
+ }
292
+ throw e;
293
+ }
294
+ return true;
295
+ }
296
+
297
+ },{"./topsort":14}],10:[function(require,module,exports){
298
+ var dfs = require("./dfs");
299
+
300
+ module.exports = postorder;
301
+
302
+ function postorder(g, vs) {
303
+ return dfs(g, vs, "post");
304
+ }
305
+
306
+ },{"./dfs":3}],11:[function(require,module,exports){
307
+ var dfs = require("./dfs");
308
+
309
+ module.exports = preorder;
310
+
311
+ function preorder(g, vs) {
312
+ return dfs(g, vs, "pre");
313
+ }
314
+
315
+ },{"./dfs":3}],12:[function(require,module,exports){
316
+ var Graph = require("../graph");
317
+ var PriorityQueue = require("../data/priority-queue");
318
+
319
+ module.exports = prim;
320
+
321
+ function prim(g, weightFunc) {
322
+ var result = new Graph();
323
+ var parents = {};
324
+ var pq = new PriorityQueue();
325
+ var v;
326
+
327
+ function updateNeighbors(edge) {
328
+ var w = edge.v === v ? edge.w : edge.v;
329
+ var pri = pq.priority(w);
330
+ if (pri !== undefined) {
331
+ var edgeWeight = weightFunc(edge);
332
+ if (edgeWeight < pri) {
333
+ parents[w] = v;
334
+ pq.decrease(w, edgeWeight);
335
+ }
336
+ }
337
+ }
338
+
339
+ if (g.nodeCount() === 0) {
340
+ return result;
341
+ }
342
+
343
+ g.nodes().forEach(function(v) {
344
+ pq.add(v, Number.POSITIVE_INFINITY);
345
+ result.setNode(v);
346
+ });
347
+
348
+ // Start from an arbitrary node
349
+ pq.decrease(g.nodes()[0], 0);
350
+
351
+ var init = false;
352
+ while (pq.size() > 0) {
353
+ v = pq.removeMin();
354
+ if (Object.hasOwn(parents, v)) {
355
+ result.setEdge(v, parents[v]);
356
+ } else if (init) {
357
+ throw new Error("Input graph is not connected: " + g);
358
+ } else {
359
+ init = true;
360
+ }
361
+
362
+ g.nodeEdges(v).forEach(updateNeighbors);
363
+ }
364
+
365
+ return result;
366
+ }
367
+
368
+ },{"../data/priority-queue":15,"../graph":16}],13:[function(require,module,exports){
369
+ module.exports = tarjan;
370
+
371
+ function tarjan(g) {
372
+ var index = 0;
373
+ var stack = [];
374
+ var visited = {}; // node id -> { onStack, lowlink, index }
375
+ var results = [];
376
+
377
+ function dfs(v) {
378
+ var entry = visited[v] = {
379
+ onStack: true,
380
+ lowlink: index,
381
+ index: index++
382
+ };
383
+ stack.push(v);
384
+
385
+ g.successors(v).forEach(function(w) {
386
+ if (!Object.hasOwn(visited, w)) {
387
+ dfs(w);
388
+ entry.lowlink = Math.min(entry.lowlink, visited[w].lowlink);
389
+ } else if (visited[w].onStack) {
390
+ entry.lowlink = Math.min(entry.lowlink, visited[w].index);
391
+ }
392
+ });
393
+
394
+ if (entry.lowlink === entry.index) {
395
+ var cmpt = [];
396
+ var w;
397
+ do {
398
+ w = stack.pop();
399
+ visited[w].onStack = false;
400
+ cmpt.push(w);
401
+ } while (v !== w);
402
+ results.push(cmpt);
403
+ }
404
+ }
405
+
406
+ g.nodes().forEach(function(v) {
407
+ if (!Object.hasOwn(visited, v)) {
408
+ dfs(v);
409
+ }
410
+ });
411
+
412
+ return results;
413
+ }
414
+
415
+ },{}],14:[function(require,module,exports){
416
+ function topsort(g) {
417
+ var visited = {};
418
+ var stack = {};
419
+ var results = [];
420
+
421
+ function visit(node) {
422
+ if (Object.hasOwn(stack, node)) {
423
+ throw new CycleException();
424
+ }
425
+
426
+ if (!Object.hasOwn(visited, node)) {
427
+ stack[node] = true;
428
+ visited[node] = true;
429
+ g.predecessors(node).forEach(visit);
430
+ delete stack[node];
431
+ results.push(node);
432
+ }
433
+ }
434
+
435
+ g.sinks().forEach(visit);
436
+
437
+ if (Object.keys(visited).length !== g.nodeCount()) {
438
+ throw new CycleException();
439
+ }
440
+
441
+ return results;
442
+ }
443
+
444
+ class CycleException extends Error {
445
+ constructor() {
446
+ super(...arguments);
447
+ }
448
+ }
449
+
450
+ module.exports = topsort;
451
+ topsort.CycleException = CycleException;
452
+
453
+ },{}],15:[function(require,module,exports){
454
+ /**
455
+ * A min-priority queue data structure. This algorithm is derived from Cormen,
456
+ * et al., "Introduction to Algorithms". The basic idea of a min-priority
457
+ * queue is that you can efficiently (in O(1) time) get the smallest key in
458
+ * the queue. Adding and removing elements takes O(log n) time. A key can
459
+ * have its priority decreased in O(log n) time.
460
+ */
461
+ class PriorityQueue {
462
+ _arr = [];
463
+ _keyIndices = {};
464
+
465
+ /**
466
+ * Returns the number of elements in the queue. Takes `O(1)` time.
467
+ */
468
+ size() {
469
+ return this._arr.length;
470
+ }
471
+
472
+ /**
473
+ * Returns the keys that are in the queue. Takes `O(n)` time.
474
+ */
475
+ keys() {
476
+ return this._arr.map(function(x) { return x.key; });
477
+ }
478
+
479
+ /**
480
+ * Returns `true` if **key** is in the queue and `false` if not.
481
+ */
482
+ has(key) {
483
+ return Object.hasOwn(this._keyIndices, key);
484
+ }
485
+
486
+ /**
487
+ * Returns the priority for **key**. If **key** is not present in the queue
488
+ * then this function returns `undefined`. Takes `O(1)` time.
489
+ *
490
+ * @param {Object} key
491
+ */
492
+ priority(key) {
493
+ var index = this._keyIndices[key];
494
+ if (index !== undefined) {
495
+ return this._arr[index].priority;
496
+ }
497
+ }
498
+
499
+ /**
500
+ * Returns the key for the minimum element in this queue. If the queue is
501
+ * empty this function throws an Error. Takes `O(1)` time.
502
+ */
503
+ min() {
504
+ if (this.size() === 0) {
505
+ throw new Error("Queue underflow");
506
+ }
507
+ return this._arr[0].key;
508
+ }
509
+
510
+ /**
511
+ * Inserts a new key into the priority queue. If the key already exists in
512
+ * the queue this function returns `false`; otherwise it will return `true`.
513
+ * Takes `O(n)` time.
514
+ *
515
+ * @param {Object} key the key to add
516
+ * @param {Number} priority the initial priority for the key
517
+ */
518
+ add(key, priority) {
519
+ var keyIndices = this._keyIndices;
520
+ key = String(key);
521
+ if (!Object.hasOwn(keyIndices, key)) {
522
+ var arr = this._arr;
523
+ var index = arr.length;
524
+ keyIndices[key] = index;
525
+ arr.push({key: key, priority: priority});
526
+ this._decrease(index);
527
+ return true;
528
+ }
529
+ return false;
530
+ }
531
+
532
+ /**
533
+ * Removes and returns the smallest key in the queue. Takes `O(log n)` time.
534
+ */
535
+ removeMin() {
536
+ this._swap(0, this._arr.length - 1);
537
+ var min = this._arr.pop();
538
+ delete this._keyIndices[min.key];
539
+ this._heapify(0);
540
+ return min.key;
541
+ }
542
+
543
+ /**
544
+ * Decreases the priority for **key** to **priority**. If the new priority is
545
+ * greater than the previous priority, this function will throw an Error.
546
+ *
547
+ * @param {Object} key the key for which to raise priority
548
+ * @param {Number} priority the new priority for the key
549
+ */
550
+ decrease(key, priority) {
551
+ var index = this._keyIndices[key];
552
+ if (priority > this._arr[index].priority) {
553
+ throw new Error("New priority is greater than current priority. " +
554
+ "Key: " + key + " Old: " + this._arr[index].priority + " New: " + priority);
555
+ }
556
+ this._arr[index].priority = priority;
557
+ this._decrease(index);
558
+ }
559
+
560
+ _heapify(i) {
561
+ var arr = this._arr;
562
+ var l = 2 * i;
563
+ var r = l + 1;
564
+ var largest = i;
565
+ if (l < arr.length) {
566
+ largest = arr[l].priority < arr[largest].priority ? l : largest;
567
+ if (r < arr.length) {
568
+ largest = arr[r].priority < arr[largest].priority ? r : largest;
569
+ }
570
+ if (largest !== i) {
571
+ this._swap(i, largest);
572
+ this._heapify(largest);
573
+ }
574
+ }
575
+ }
576
+
577
+ _decrease(index) {
578
+ var arr = this._arr;
579
+ var priority = arr[index].priority;
580
+ var parent;
581
+ while (index !== 0) {
582
+ parent = index >> 1;
583
+ if (arr[parent].priority < priority) {
584
+ break;
585
+ }
586
+ this._swap(index, parent);
587
+ index = parent;
588
+ }
589
+ }
590
+
591
+ _swap(i, j) {
592
+ var arr = this._arr;
593
+ var keyIndices = this._keyIndices;
594
+ var origArrI = arr[i];
595
+ var origArrJ = arr[j];
596
+ arr[i] = origArrJ;
597
+ arr[j] = origArrI;
598
+ keyIndices[origArrJ.key] = i;
599
+ keyIndices[origArrI.key] = j;
600
+ }
601
+ }
602
+
603
+ module.exports = PriorityQueue;
604
+
605
+ },{}],16:[function(require,module,exports){
606
+ "use strict";
607
+
608
+ var DEFAULT_EDGE_NAME = "\x00";
609
+ var GRAPH_NODE = "\x00";
610
+ var EDGE_KEY_DELIM = "\x01";
611
+
612
+ // Implementation notes:
613
+ //
614
+ // * Node id query functions should return string ids for the nodes
615
+ // * Edge id query functions should return an "edgeObj", edge object, that is
616
+ // composed of enough information to uniquely identify an edge: {v, w, name}.
617
+ // * Internally we use an "edgeId", a stringified form of the edgeObj, to
618
+ // reference edges. This is because we need a performant way to look these
619
+ // edges up and, object properties, which have string keys, are the closest
620
+ // we're going to get to a performant hashtable in JavaScript.
621
+
622
+ class Graph {
623
+ _isDirected = true;
624
+ _isMultigraph = false;
625
+ _isCompound = false;
626
+
627
+ // Label for the graph itself
628
+ _label;
629
+
630
+ // Defaults to be set when creating a new node
631
+ _defaultNodeLabelFn = () => undefined;
632
+
633
+ // Defaults to be set when creating a new edge
634
+ _defaultEdgeLabelFn = () => undefined;
635
+
636
+ // v -> label
637
+ _nodes = {};
638
+
639
+ // v -> edgeObj
640
+ _in = {};
641
+
642
+ // u -> v -> Number
643
+ _preds = {};
644
+
645
+ // v -> edgeObj
646
+ _out = {};
647
+
648
+ // v -> w -> Number
649
+ _sucs = {};
650
+
651
+ // e -> edgeObj
652
+ _edgeObjs = {};
653
+
654
+ // e -> label
655
+ _edgeLabels = {};
656
+
657
+ /* Number of nodes in the graph. Should only be changed by the implementation. */
658
+ _nodeCount = 0;
659
+
660
+ /* Number of edges in the graph. Should only be changed by the implementation. */
661
+ _edgeCount = 0;
662
+
663
+ _parent;
664
+
665
+ _children;
666
+
667
+ constructor(opts) {
668
+ if (opts) {
669
+ this._isDirected = Object.hasOwn(opts, "directed") ? opts.directed : true;
670
+ this._isMultigraph = Object.hasOwn(opts, "multigraph") ? opts.multigraph : false;
671
+ this._isCompound = Object.hasOwn(opts, "compound") ? opts.compound : false;
672
+ }
673
+
674
+ if (this._isCompound) {
675
+ // v -> parent
676
+ this._parent = {};
677
+
678
+ // v -> children
679
+ this._children = {};
680
+ this._children[GRAPH_NODE] = {};
681
+ }
682
+ }
683
+
684
+ /* === Graph functions ========= */
685
+
686
+ /**
687
+ * Whether graph was created with 'directed' flag set to true or not.
688
+ */
689
+ isDirected() {
690
+ return this._isDirected;
691
+ }
692
+
693
+ /**
694
+ * Whether graph was created with 'multigraph' flag set to true or not.
695
+ */
696
+ isMultigraph() {
697
+ return this._isMultigraph;
698
+ }
699
+
700
+ /**
701
+ * Whether graph was created with 'compound' flag set to true or not.
702
+ */
703
+ isCompound() {
704
+ return this._isCompound;
705
+ }
706
+
707
+ /**
708
+ * Sets the label of the graph.
709
+ */
710
+ setGraph(label) {
711
+ this._label = label;
712
+ return this;
713
+ }
714
+
715
+ /**
716
+ * Gets the graph label.
717
+ */
718
+ graph() {
719
+ return this._label;
720
+ }
721
+
722
+
723
+ /* === Node functions ========== */
724
+
725
+ /**
726
+ * Sets the default node label. If newDefault is a function, it will be
727
+ * invoked ach time when setting a label for a node. Otherwise, this label
728
+ * will be assigned as default label in case if no label was specified while
729
+ * setting a node.
730
+ * Complexity: O(1).
731
+ */
732
+ setDefaultNodeLabel(newDefault) {
733
+ this._defaultNodeLabelFn = newDefault;
734
+ if (typeof newDefault !== 'function') {
735
+ this._defaultNodeLabelFn = () => newDefault;
736
+ }
737
+
738
+ return this;
739
+ }
740
+
741
+ /**
742
+ * Gets the number of nodes in the graph.
743
+ * Complexity: O(1).
744
+ */
745
+ nodeCount() {
746
+ return this._nodeCount;
747
+ }
748
+
749
+ /**
750
+ * Gets all nodes of the graph. Note, the in case of compound graph subnodes are
751
+ * not included in list.
752
+ * Complexity: O(1).
753
+ */
754
+ nodes() {
755
+ return Object.keys(this._nodes);
756
+ }
757
+
758
+ /**
759
+ * Gets list of nodes without in-edges.
760
+ * Complexity: O(|V|).
761
+ */
762
+ sources() {
763
+ var self = this;
764
+ return this.nodes().filter(v => Object.keys(self._in[v]).length === 0);
765
+ }
766
+
767
+ /**
768
+ * Gets list of nodes without out-edges.
769
+ * Complexity: O(|V|).
770
+ */
771
+ sinks() {
772
+ var self = this;
773
+ return this.nodes().filter(v => Object.keys(self._out[v]).length === 0);
774
+ }
775
+
776
+ /**
777
+ * Invokes setNode method for each node in names list.
778
+ * Complexity: O(|names|).
779
+ */
780
+ setNodes(vs, value) {
781
+ var args = arguments;
782
+ var self = this;
783
+ vs.forEach(function(v) {
784
+ if (args.length > 1) {
785
+ self.setNode(v, value);
786
+ } else {
787
+ self.setNode(v);
788
+ }
789
+ });
790
+ return this;
791
+ }
792
+
793
+ /**
794
+ * Creates or updates the value for the node v in the graph. If label is supplied
795
+ * it is set as the value for the node. If label is not supplied and the node was
796
+ * created by this call then the default node label will be assigned.
797
+ * Complexity: O(1).
798
+ */
799
+ setNode(v, value) {
800
+ if (Object.hasOwn(this._nodes, v)) {
801
+ if (arguments.length > 1) {
802
+ this._nodes[v] = value;
803
+ }
804
+ return this;
805
+ }
806
+
807
+ this._nodes[v] = arguments.length > 1 ? value : this._defaultNodeLabelFn(v);
808
+ if (this._isCompound) {
809
+ this._parent[v] = GRAPH_NODE;
810
+ this._children[v] = {};
811
+ this._children[GRAPH_NODE][v] = true;
812
+ }
813
+ this._in[v] = {};
814
+ this._preds[v] = {};
815
+ this._out[v] = {};
816
+ this._sucs[v] = {};
817
+ ++this._nodeCount;
818
+ return this;
819
+ }
820
+
821
+ /**
822
+ * Gets the label of node with specified name.
823
+ * Complexity: O(|V|).
824
+ */
825
+ node(v) {
826
+ return this._nodes[v];
827
+ }
828
+
829
+ /**
830
+ * Detects whether graph has a node with specified name or not.
831
+ */
832
+ hasNode(v) {
833
+ return Object.hasOwn(this._nodes, v);
834
+ }
835
+
836
+ /**
837
+ * Remove the node with the name from the graph or do nothing if the node is not in
838
+ * the graph. If the node was removed this function also removes any incident
839
+ * edges.
840
+ * Complexity: O(1).
841
+ */
842
+ removeNode(v) {
843
+ var self = this;
844
+ if (Object.hasOwn(this._nodes, v)) {
845
+ var removeEdge = e => self.removeEdge(self._edgeObjs[e]);
846
+ delete this._nodes[v];
847
+ if (this._isCompound) {
848
+ this._removeFromParentsChildList(v);
849
+ delete this._parent[v];
850
+ this.children(v).forEach(function(child) {
851
+ self.setParent(child);
852
+ });
853
+ delete this._children[v];
854
+ }
855
+ Object.keys(this._in[v]).forEach(removeEdge);
856
+ delete this._in[v];
857
+ delete this._preds[v];
858
+ Object.keys(this._out[v]).forEach(removeEdge);
859
+ delete this._out[v];
860
+ delete this._sucs[v];
861
+ --this._nodeCount;
862
+ }
863
+ return this;
864
+ }
865
+
866
+ /**
867
+ * Sets node p as a parent for node v if it is defined, or removes the
868
+ * parent for v if p is undefined. Method throws an exception in case of
869
+ * invoking it in context of noncompound graph.
870
+ * Average-case complexity: O(1).
871
+ */
872
+ setParent(v, parent) {
873
+ if (!this._isCompound) {
874
+ throw new Error("Cannot set parent in a non-compound graph");
875
+ }
876
+
877
+ if (parent === undefined) {
878
+ parent = GRAPH_NODE;
879
+ } else {
880
+ // Coerce parent to string
881
+ parent += "";
882
+ for (var ancestor = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) {
883
+ if (ancestor === v) {
884
+ throw new Error("Setting " + parent+ " as parent of " + v +
885
+ " would create a cycle");
886
+ }
887
+ }
888
+
889
+ this.setNode(parent);
890
+ }
891
+
892
+ this.setNode(v);
893
+ this._removeFromParentsChildList(v);
894
+ this._parent[v] = parent;
895
+ this._children[parent][v] = true;
896
+ return this;
897
+ }
898
+
899
+ _removeFromParentsChildList(v) {
900
+ delete this._children[this._parent[v]][v];
901
+ }
902
+
903
+ /**
904
+ * Gets parent node for node v.
905
+ * Complexity: O(1).
906
+ */
907
+ parent(v) {
908
+ if (this._isCompound) {
909
+ var parent = this._parent[v];
910
+ if (parent !== GRAPH_NODE) {
911
+ return parent;
912
+ }
913
+ }
914
+ }
915
+
916
+ /**
917
+ * Gets list of direct children of node v.
918
+ * Complexity: O(1).
919
+ */
920
+ children(v = GRAPH_NODE) {
921
+ if (this._isCompound) {
922
+ var children = this._children[v];
923
+ if (children) {
924
+ return Object.keys(children);
925
+ }
926
+ } else if (v === GRAPH_NODE) {
927
+ return this.nodes();
928
+ } else if (this.hasNode(v)) {
929
+ return [];
930
+ }
931
+ }
932
+
933
+ /**
934
+ * Return all nodes that are predecessors of the specified node or undefined if node v is not in
935
+ * the graph. Behavior is undefined for undirected graphs - use neighbors instead.
936
+ * Complexity: O(|V|).
937
+ */
938
+ predecessors(v) {
939
+ var predsV = this._preds[v];
940
+ if (predsV) {
941
+ return Object.keys(predsV);
942
+ }
943
+ }
944
+
945
+ /**
946
+ * Return all nodes that are successors of the specified node or undefined if node v is not in
947
+ * the graph. Behavior is undefined for undirected graphs - use neighbors instead.
948
+ * Complexity: O(|V|).
949
+ */
950
+ successors(v) {
951
+ var sucsV = this._sucs[v];
952
+ if (sucsV) {
953
+ return Object.keys(sucsV);
954
+ }
955
+ }
956
+
957
+ /**
958
+ * Return all nodes that are predecessors or successors of the specified node or undefined if
959
+ * node v is not in the graph.
960
+ * Complexity: O(|V|).
961
+ */
962
+ neighbors(v) {
963
+ var preds = this.predecessors(v);
964
+ if (preds) {
965
+ const union = new Set(preds);
966
+ for (var succ of this.successors(v)) {
967
+ union.add(succ);
968
+ }
969
+
970
+ return Array.from(union.values());
971
+ }
972
+ }
973
+
974
+ isLeaf(v) {
975
+ var neighbors;
976
+ if (this.isDirected()) {
977
+ neighbors = this.successors(v);
978
+ } else {
979
+ neighbors = this.neighbors(v);
980
+ }
981
+ return neighbors.length === 0;
982
+ }
983
+
984
+ /**
985
+ * Creates new graph with nodes filtered via filter. Edges incident to rejected node
986
+ * are also removed. In case of compound graph, if parent is rejected by filter,
987
+ * than all its children are rejected too.
988
+ * Average-case complexity: O(|E|+|V|).
989
+ */
990
+ filterNodes(filter) {
991
+ var copy = new this.constructor({
992
+ directed: this._isDirected,
993
+ multigraph: this._isMultigraph,
994
+ compound: this._isCompound
995
+ });
996
+
997
+ copy.setGraph(this.graph());
998
+
999
+ var self = this;
1000
+ Object.entries(this._nodes).forEach(function([v, value]) {
1001
+ if (filter(v)) {
1002
+ copy.setNode(v, value);
1003
+ }
1004
+ });
1005
+
1006
+ Object.values(this._edgeObjs).forEach(function(e) {
1007
+ if (copy.hasNode(e.v) && copy.hasNode(e.w)) {
1008
+ copy.setEdge(e, self.edge(e));
1009
+ }
1010
+ });
1011
+
1012
+ var parents = {};
1013
+ function findParent(v) {
1014
+ var parent = self.parent(v);
1015
+ if (parent === undefined || copy.hasNode(parent)) {
1016
+ parents[v] = parent;
1017
+ return parent;
1018
+ } else if (parent in parents) {
1019
+ return parents[parent];
1020
+ } else {
1021
+ return findParent(parent);
1022
+ }
1023
+ }
1024
+
1025
+ if (this._isCompound) {
1026
+ copy.nodes().forEach(v => copy.setParent(v, findParent(v)));
1027
+ }
1028
+
1029
+ return copy;
1030
+ }
1031
+
1032
+ /* === Edge functions ========== */
1033
+
1034
+ /**
1035
+ * Sets the default edge label or factory function. This label will be
1036
+ * assigned as default label in case if no label was specified while setting
1037
+ * an edge or this function will be invoked each time when setting an edge
1038
+ * with no label specified and returned value * will be used as a label for edge.
1039
+ * Complexity: O(1).
1040
+ */
1041
+ setDefaultEdgeLabel(newDefault) {
1042
+ this._defaultEdgeLabelFn = newDefault;
1043
+ if (typeof newDefault !== 'function') {
1044
+ this._defaultEdgeLabelFn = () => newDefault;
1045
+ }
1046
+
1047
+ return this;
1048
+ }
1049
+
1050
+ /**
1051
+ * Gets the number of edges in the graph.
1052
+ * Complexity: O(1).
1053
+ */
1054
+ edgeCount() {
1055
+ return this._edgeCount;
1056
+ }
1057
+
1058
+ /**
1059
+ * Gets edges of the graph. In case of compound graph subgraphs are not considered.
1060
+ * Complexity: O(|E|).
1061
+ */
1062
+ edges() {
1063
+ return Object.values(this._edgeObjs);
1064
+ }
1065
+
1066
+ /**
1067
+ * Establish an edges path over the nodes in nodes list. If some edge is already
1068
+ * exists, it will update its label, otherwise it will create an edge between pair
1069
+ * of nodes with label provided or default label if no label provided.
1070
+ * Complexity: O(|nodes|).
1071
+ */
1072
+ setPath(vs, value) {
1073
+ var self = this;
1074
+ var args = arguments;
1075
+ vs.reduce(function(v, w) {
1076
+ if (args.length > 1) {
1077
+ self.setEdge(v, w, value);
1078
+ } else {
1079
+ self.setEdge(v, w);
1080
+ }
1081
+ return w;
1082
+ });
1083
+ return this;
1084
+ }
1085
+
1086
+ /**
1087
+ * Creates or updates the label for the edge (v, w) with the optionally supplied
1088
+ * name. If label is supplied it is set as the value for the edge. If label is not
1089
+ * supplied and the edge was created by this call then the default edge label will
1090
+ * be assigned. The name parameter is only useful with multigraphs.
1091
+ */
1092
+ setEdge() {
1093
+ var v, w, name, value;
1094
+ var valueSpecified = false;
1095
+ var arg0 = arguments[0];
1096
+
1097
+ if (typeof arg0 === "object" && arg0 !== null && "v" in arg0) {
1098
+ v = arg0.v;
1099
+ w = arg0.w;
1100
+ name = arg0.name;
1101
+ if (arguments.length === 2) {
1102
+ value = arguments[1];
1103
+ valueSpecified = true;
1104
+ }
1105
+ } else {
1106
+ v = arg0;
1107
+ w = arguments[1];
1108
+ name = arguments[3];
1109
+ if (arguments.length > 2) {
1110
+ value = arguments[2];
1111
+ valueSpecified = true;
1112
+ }
1113
+ }
1114
+
1115
+ v = "" + v;
1116
+ w = "" + w;
1117
+ if (name !== undefined) {
1118
+ name = "" + name;
1119
+ }
1120
+
1121
+ var e = edgeArgsToId(this._isDirected, v, w, name);
1122
+ if (Object.hasOwn(this._edgeLabels, e)) {
1123
+ if (valueSpecified) {
1124
+ this._edgeLabels[e] = value;
1125
+ }
1126
+ return this;
1127
+ }
1128
+
1129
+ if (name !== undefined && !this._isMultigraph) {
1130
+ throw new Error("Cannot set a named edge when isMultigraph = false");
1131
+ }
1132
+
1133
+ // It didn't exist, so we need to create it.
1134
+ // First ensure the nodes exist.
1135
+ this.setNode(v);
1136
+ this.setNode(w);
1137
+
1138
+ this._edgeLabels[e] = valueSpecified ? value : this._defaultEdgeLabelFn(v, w, name);
1139
+
1140
+ var edgeObj = edgeArgsToObj(this._isDirected, v, w, name);
1141
+ // Ensure we add undirected edges in a consistent way.
1142
+ v = edgeObj.v;
1143
+ w = edgeObj.w;
1144
+
1145
+ Object.freeze(edgeObj);
1146
+ this._edgeObjs[e] = edgeObj;
1147
+ incrementOrInitEntry(this._preds[w], v);
1148
+ incrementOrInitEntry(this._sucs[v], w);
1149
+ this._in[w][e] = edgeObj;
1150
+ this._out[v][e] = edgeObj;
1151
+ this._edgeCount++;
1152
+ return this;
1153
+ }
1154
+
1155
+ /**
1156
+ * Gets the label for the specified edge.
1157
+ * Complexity: O(1).
1158
+ */
1159
+ edge(v, w, name) {
1160
+ var e = (arguments.length === 1
1161
+ ? edgeObjToId(this._isDirected, arguments[0])
1162
+ : edgeArgsToId(this._isDirected, v, w, name));
1163
+ return this._edgeLabels[e];
1164
+ }
1165
+
1166
+ /**
1167
+ * Gets the label for the specified edge and converts it to an object.
1168
+ * Complexity: O(1)
1169
+ */
1170
+ edgeAsObj() {
1171
+ const edge = this.edge(...arguments);
1172
+ if (typeof edge !== "object") {
1173
+ return {label: edge};
1174
+ }
1175
+
1176
+ return edge;
1177
+ }
1178
+
1179
+ /**
1180
+ * Detects whether the graph contains specified edge or not. No subgraphs are considered.
1181
+ * Complexity: O(1).
1182
+ */
1183
+ hasEdge(v, w, name) {
1184
+ var e = (arguments.length === 1
1185
+ ? edgeObjToId(this._isDirected, arguments[0])
1186
+ : edgeArgsToId(this._isDirected, v, w, name));
1187
+ return Object.hasOwn(this._edgeLabels, e);
1188
+ }
1189
+
1190
+ /**
1191
+ * Removes the specified edge from the graph. No subgraphs are considered.
1192
+ * Complexity: O(1).
1193
+ */
1194
+ removeEdge(v, w, name) {
1195
+ var e = (arguments.length === 1
1196
+ ? edgeObjToId(this._isDirected, arguments[0])
1197
+ : edgeArgsToId(this._isDirected, v, w, name));
1198
+ var edge = this._edgeObjs[e];
1199
+ if (edge) {
1200
+ v = edge.v;
1201
+ w = edge.w;
1202
+ delete this._edgeLabels[e];
1203
+ delete this._edgeObjs[e];
1204
+ decrementOrRemoveEntry(this._preds[w], v);
1205
+ decrementOrRemoveEntry(this._sucs[v], w);
1206
+ delete this._in[w][e];
1207
+ delete this._out[v][e];
1208
+ this._edgeCount--;
1209
+ }
1210
+ return this;
1211
+ }
1212
+
1213
+ /**
1214
+ * Return all edges that point to the node v. Optionally filters those edges down to just those
1215
+ * coming from node u. Behavior is undefined for undirected graphs - use nodeEdges instead.
1216
+ * Complexity: O(|E|).
1217
+ */
1218
+ inEdges(v, u) {
1219
+ var inV = this._in[v];
1220
+ if (inV) {
1221
+ var edges = Object.values(inV);
1222
+ if (!u) {
1223
+ return edges;
1224
+ }
1225
+ return edges.filter(edge => edge.v === u);
1226
+ }
1227
+ }
1228
+
1229
+ /**
1230
+ * Return all edges that are pointed at by node v. Optionally filters those edges down to just
1231
+ * those point to w. Behavior is undefined for undirected graphs - use nodeEdges instead.
1232
+ * Complexity: O(|E|).
1233
+ */
1234
+ outEdges(v, w) {
1235
+ var outV = this._out[v];
1236
+ if (outV) {
1237
+ var edges = Object.values(outV);
1238
+ if (!w) {
1239
+ return edges;
1240
+ }
1241
+ return edges.filter(edge => edge.w === w);
1242
+ }
1243
+ }
1244
+
1245
+ /**
1246
+ * Returns all edges to or from node v regardless of direction. Optionally filters those edges
1247
+ * down to just those between nodes v and w regardless of direction.
1248
+ * Complexity: O(|E|).
1249
+ */
1250
+ nodeEdges(v, w) {
1251
+ var inEdges = this.inEdges(v, w);
1252
+ if (inEdges) {
1253
+ return inEdges.concat(this.outEdges(v, w));
1254
+ }
1255
+ }
1256
+ }
1257
+
1258
+ function incrementOrInitEntry(map, k) {
1259
+ if (map[k]) {
1260
+ map[k]++;
1261
+ } else {
1262
+ map[k] = 1;
1263
+ }
1264
+ }
1265
+
1266
+ function decrementOrRemoveEntry(map, k) {
1267
+ if (!--map[k]) { delete map[k]; }
1268
+ }
1269
+
1270
+ function edgeArgsToId(isDirected, v_, w_, name) {
1271
+ var v = "" + v_;
1272
+ var w = "" + w_;
1273
+ if (!isDirected && v > w) {
1274
+ var tmp = v;
1275
+ v = w;
1276
+ w = tmp;
1277
+ }
1278
+ return v + EDGE_KEY_DELIM + w + EDGE_KEY_DELIM +
1279
+ (name === undefined ? DEFAULT_EDGE_NAME : name);
1280
+ }
1281
+
1282
+ function edgeArgsToObj(isDirected, v_, w_, name) {
1283
+ var v = "" + v_;
1284
+ var w = "" + w_;
1285
+ if (!isDirected && v > w) {
1286
+ var tmp = v;
1287
+ v = w;
1288
+ w = tmp;
1289
+ }
1290
+ var edgeObj = { v: v, w: w };
1291
+ if (name) {
1292
+ edgeObj.name = name;
1293
+ }
1294
+ return edgeObj;
1295
+ }
1296
+
1297
+ function edgeObjToId(isDirected, edgeObj) {
1298
+ return edgeArgsToId(isDirected, edgeObj.v, edgeObj.w, edgeObj.name);
1299
+ }
1300
+
1301
+ module.exports = Graph;
1302
+
1303
+ },{}],17:[function(require,module,exports){
1304
+ // Includes only the "core" of graphlib
1305
+ module.exports = {
1306
+ Graph: require("./graph"),
1307
+ version: require("./version")
1308
+ };
1309
+
1310
+ },{"./graph":16,"./version":19}],18:[function(require,module,exports){
1311
+ var Graph = require("./graph");
1312
+
1313
+ module.exports = {
1314
+ write: write,
1315
+ read: read
1316
+ };
1317
+
1318
+ /**
1319
+ * Creates a JSON representation of the graph that can be serialized to a string with
1320
+ * JSON.stringify. The graph can later be restored using json.read.
1321
+ */
1322
+ function write(g) {
1323
+ var json = {
1324
+ options: {
1325
+ directed: g.isDirected(),
1326
+ multigraph: g.isMultigraph(),
1327
+ compound: g.isCompound()
1328
+ },
1329
+ nodes: writeNodes(g),
1330
+ edges: writeEdges(g)
1331
+ };
1332
+
1333
+ if (g.graph() !== undefined) {
1334
+ json.value = structuredClone(g.graph());
1335
+ }
1336
+ return json;
1337
+ }
1338
+
1339
+ function writeNodes(g) {
1340
+ return g.nodes().map(function(v) {
1341
+ var nodeValue = g.node(v);
1342
+ var parent = g.parent(v);
1343
+ var node = { v: v };
1344
+ if (nodeValue !== undefined) {
1345
+ node.value = nodeValue;
1346
+ }
1347
+ if (parent !== undefined) {
1348
+ node.parent = parent;
1349
+ }
1350
+ return node;
1351
+ });
1352
+ }
1353
+
1354
+ function writeEdges(g) {
1355
+ return g.edges().map(function(e) {
1356
+ var edgeValue = g.edge(e);
1357
+ var edge = { v: e.v, w: e.w };
1358
+ if (e.name !== undefined) {
1359
+ edge.name = e.name;
1360
+ }
1361
+ if (edgeValue !== undefined) {
1362
+ edge.value = edgeValue;
1363
+ }
1364
+ return edge;
1365
+ });
1366
+ }
1367
+
1368
+ /**
1369
+ * Takes JSON as input and returns the graph representation.
1370
+ *
1371
+ * @example
1372
+ * var g2 = graphlib.json.read(JSON.parse(str));
1373
+ * g2.nodes();
1374
+ * // ['a', 'b']
1375
+ * g2.edges()
1376
+ * // [ { v: 'a', w: 'b' } ]
1377
+ */
1378
+ function read(json) {
1379
+ var g = new Graph(json.options).setGraph(json.value);
1380
+ json.nodes.forEach(function(entry) {
1381
+ g.setNode(entry.v, entry.value);
1382
+ if (entry.parent) {
1383
+ g.setParent(entry.v, entry.parent);
1384
+ }
1385
+ });
1386
+ json.edges.forEach(function(entry) {
1387
+ g.setEdge({ v: entry.v, w: entry.w, name: entry.name }, entry.value);
1388
+ });
1389
+ return g;
1390
+ }
1391
+
1392
+ },{"./graph":16}],19:[function(require,module,exports){
1393
+ module.exports = '2.2.4';
1394
+
1395
+ },{}]},{},[1])(1)
1396
+ });