graph-dynamic 1.0.0
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.
- package/LICENSE +19 -0
- package/README.md +102 -0
- package/dist/graph-alg.min.js +1 -0
- package/dist/graph-settings.min.js +1 -0
- package/dist/graphlib.core.js +1396 -0
- package/dist/graphlib.core.min.js +304 -0
- package/dist/graphlib.js +1396 -0
- package/dist/graphlib.min.js +304 -0
- package/index.d.ts +621 -0
- package/index.js +38 -0
- package/lib/alg/components.js +25 -0
- package/lib/alg/dfs.js +67 -0
- package/lib/alg/dijkstra-all.js +10 -0
- package/lib/alg/dijkstra.js +53 -0
- package/lib/alg/find-cycles.js +9 -0
- package/lib/alg/floyd-warshall.js +48 -0
- package/lib/alg/index.js +13 -0
- package/lib/alg/is-acyclic.js +15 -0
- package/lib/alg/postorder.js +7 -0
- package/lib/alg/preorder.js +7 -0
- package/lib/alg/prim.js +51 -0
- package/lib/alg/tarjan.js +45 -0
- package/lib/alg/topsort.js +36 -0
- package/lib/data/priority-queue.js +150 -0
- package/lib/graph.js +703 -0
- package/lib/index.js +5 -0
- package/lib/json.js +80 -0
- package/lib/version.js +1 -0
- package/package.json +50 -0
package/lib/alg/dfs.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
module.exports = dfs;
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* A helper that preforms a pre- or post-order traversal on the input graph
|
|
5
|
+
* and returns the nodes in the order they were visited. If the graph is
|
|
6
|
+
* undirected then this algorithm will navigate using neighbors. If the graph
|
|
7
|
+
* is directed then this algorithm will navigate using successors.
|
|
8
|
+
*
|
|
9
|
+
* If the order is not "post", it will be treated as "pre".
|
|
10
|
+
*/
|
|
11
|
+
function dfs(g, vs, order) {
|
|
12
|
+
if (!Array.isArray(vs)) {
|
|
13
|
+
vs = [vs];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
var navigation = g.isDirected() ? v => g.successors(v) : v => g.neighbors(v);
|
|
17
|
+
var orderFunc = order === "post" ? postOrderDfs : preOrderDfs;
|
|
18
|
+
|
|
19
|
+
var acc = [];
|
|
20
|
+
var visited = {};
|
|
21
|
+
vs.forEach(v => {
|
|
22
|
+
if (!g.hasNode(v)) {
|
|
23
|
+
throw new Error("Graph does not have node: " + v);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
orderFunc(v, navigation, visited, acc);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return acc;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function postOrderDfs(v, navigation, visited, acc) {
|
|
33
|
+
var stack = [[v, false]];
|
|
34
|
+
while (stack.length > 0) {
|
|
35
|
+
var curr = stack.pop();
|
|
36
|
+
if (curr[1]) {
|
|
37
|
+
acc.push(curr[0]);
|
|
38
|
+
} else {
|
|
39
|
+
if (!Object.hasOwn(visited, curr[0])) {
|
|
40
|
+
visited[curr[0]] = true;
|
|
41
|
+
stack.push([curr[0], true]);
|
|
42
|
+
forEachRight(navigation(curr[0]), w => stack.push([w, false]));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function preOrderDfs(v, navigation, visited, acc) {
|
|
49
|
+
var stack = [v];
|
|
50
|
+
while (stack.length > 0) {
|
|
51
|
+
var curr = stack.pop();
|
|
52
|
+
if (!Object.hasOwn(visited, curr)) {
|
|
53
|
+
visited[curr] = true;
|
|
54
|
+
acc.push(curr);
|
|
55
|
+
forEachRight(navigation(curr), w => stack.push(w));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function forEachRight(array, iteratee) {
|
|
61
|
+
var length = array.length;
|
|
62
|
+
while (length--) {
|
|
63
|
+
iteratee(array[length], length, array);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return array;
|
|
67
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
var PriorityQueue = require("../data/priority-queue");
|
|
2
|
+
|
|
3
|
+
module.exports = dijkstra;
|
|
4
|
+
|
|
5
|
+
var DEFAULT_WEIGHT_FUNC = () => 1;
|
|
6
|
+
|
|
7
|
+
function dijkstra(g, source, weightFn, edgeFn) {
|
|
8
|
+
return runDijkstra(g, String(source),
|
|
9
|
+
weightFn || DEFAULT_WEIGHT_FUNC,
|
|
10
|
+
edgeFn || function(v) { return g.outEdges(v); });
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function runDijkstra(g, source, weightFn, edgeFn) {
|
|
14
|
+
var results = {};
|
|
15
|
+
var pq = new PriorityQueue();
|
|
16
|
+
var v, vEntry;
|
|
17
|
+
|
|
18
|
+
var updateNeighbors = function(edge) {
|
|
19
|
+
var w = edge.v !== v ? edge.v : edge.w;
|
|
20
|
+
var wEntry = results[w];
|
|
21
|
+
var weight = weightFn(edge);
|
|
22
|
+
var distance = vEntry.distance + weight;
|
|
23
|
+
|
|
24
|
+
if (weight < 0) {
|
|
25
|
+
throw new Error("dijkstra does not allow negative edge weights. " +
|
|
26
|
+
"Bad edge: " + edge + " Weight: " + weight);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (distance < wEntry.distance) {
|
|
30
|
+
wEntry.distance = distance;
|
|
31
|
+
wEntry.predecessor = v;
|
|
32
|
+
pq.decrease(w, distance);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
g.nodes().forEach(function(v) {
|
|
37
|
+
var distance = v === source ? 0 : Number.POSITIVE_INFINITY;
|
|
38
|
+
results[v] = { distance: distance };
|
|
39
|
+
pq.add(v, distance);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
while (pq.size() > 0) {
|
|
43
|
+
v = pq.removeMin();
|
|
44
|
+
vEntry = results[v];
|
|
45
|
+
if (vEntry.distance === Number.POSITIVE_INFINITY) {
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
edgeFn(v).forEach(updateNeighbors);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return results;
|
|
53
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
module.exports = floydWarshall;
|
|
2
|
+
|
|
3
|
+
var DEFAULT_WEIGHT_FUNC = () => 1;
|
|
4
|
+
|
|
5
|
+
function floydWarshall(g, weightFn, edgeFn) {
|
|
6
|
+
return runFloydWarshall(g,
|
|
7
|
+
weightFn || DEFAULT_WEIGHT_FUNC,
|
|
8
|
+
edgeFn || function(v) { return g.outEdges(v); });
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function runFloydWarshall(g, weightFn, edgeFn) {
|
|
12
|
+
var results = {};
|
|
13
|
+
var nodes = g.nodes();
|
|
14
|
+
|
|
15
|
+
nodes.forEach(function(v) {
|
|
16
|
+
results[v] = {};
|
|
17
|
+
results[v][v] = { distance: 0 };
|
|
18
|
+
nodes.forEach(function(w) {
|
|
19
|
+
if (v !== w) {
|
|
20
|
+
results[v][w] = { distance: Number.POSITIVE_INFINITY };
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
edgeFn(v).forEach(function(edge) {
|
|
24
|
+
var w = edge.v === v ? edge.w : edge.v;
|
|
25
|
+
var d = weightFn(edge);
|
|
26
|
+
results[v][w] = { distance: d, predecessor: v };
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
nodes.forEach(function(k) {
|
|
31
|
+
var rowK = results[k];
|
|
32
|
+
nodes.forEach(function(i) {
|
|
33
|
+
var rowI = results[i];
|
|
34
|
+
nodes.forEach(function(j) {
|
|
35
|
+
var ik = rowI[k];
|
|
36
|
+
var kj = rowK[j];
|
|
37
|
+
var ij = rowI[j];
|
|
38
|
+
var altDistance = ik.distance + kj.distance;
|
|
39
|
+
if (altDistance < ij.distance) {
|
|
40
|
+
ij.distance = altDistance;
|
|
41
|
+
ij.predecessor = kj.predecessor;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return results;
|
|
48
|
+
}
|
package/lib/alg/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
components: require("./components"),
|
|
3
|
+
dijkstra: require("./dijkstra"),
|
|
4
|
+
dijkstraAll: require("./dijkstra-all"),
|
|
5
|
+
findCycles: require("./find-cycles"),
|
|
6
|
+
floydWarshall: require("./floyd-warshall"),
|
|
7
|
+
isAcyclic: require("./is-acyclic"),
|
|
8
|
+
postorder: require("./postorder"),
|
|
9
|
+
preorder: require("./preorder"),
|
|
10
|
+
prim: require("./prim"),
|
|
11
|
+
tarjan: require("./tarjan"),
|
|
12
|
+
topsort: require("./topsort")
|
|
13
|
+
};
|
package/lib/alg/prim.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var Graph = require("../graph");
|
|
2
|
+
var PriorityQueue = require("../data/priority-queue");
|
|
3
|
+
|
|
4
|
+
module.exports = prim;
|
|
5
|
+
|
|
6
|
+
function prim(g, weightFunc) {
|
|
7
|
+
var result = new Graph();
|
|
8
|
+
var parents = {};
|
|
9
|
+
var pq = new PriorityQueue();
|
|
10
|
+
var v;
|
|
11
|
+
|
|
12
|
+
function updateNeighbors(edge) {
|
|
13
|
+
var w = edge.v === v ? edge.w : edge.v;
|
|
14
|
+
var pri = pq.priority(w);
|
|
15
|
+
if (pri !== undefined) {
|
|
16
|
+
var edgeWeight = weightFunc(edge);
|
|
17
|
+
if (edgeWeight < pri) {
|
|
18
|
+
parents[w] = v;
|
|
19
|
+
pq.decrease(w, edgeWeight);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (g.nodeCount() === 0) {
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
g.nodes().forEach(function(v) {
|
|
29
|
+
pq.add(v, Number.POSITIVE_INFINITY);
|
|
30
|
+
result.setNode(v);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Start from an arbitrary node
|
|
34
|
+
pq.decrease(g.nodes()[0], 0);
|
|
35
|
+
|
|
36
|
+
var init = false;
|
|
37
|
+
while (pq.size() > 0) {
|
|
38
|
+
v = pq.removeMin();
|
|
39
|
+
if (Object.hasOwn(parents, v)) {
|
|
40
|
+
result.setEdge(v, parents[v]);
|
|
41
|
+
} else if (init) {
|
|
42
|
+
throw new Error("Input graph is not connected: " + g);
|
|
43
|
+
} else {
|
|
44
|
+
init = true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
g.nodeEdges(v).forEach(updateNeighbors);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
module.exports = tarjan;
|
|
2
|
+
|
|
3
|
+
function tarjan(g) {
|
|
4
|
+
var index = 0;
|
|
5
|
+
var stack = [];
|
|
6
|
+
var visited = {}; // node id -> { onStack, lowlink, index }
|
|
7
|
+
var results = [];
|
|
8
|
+
|
|
9
|
+
function dfs(v) {
|
|
10
|
+
var entry = visited[v] = {
|
|
11
|
+
onStack: true,
|
|
12
|
+
lowlink: index,
|
|
13
|
+
index: index++
|
|
14
|
+
};
|
|
15
|
+
stack.push(v);
|
|
16
|
+
|
|
17
|
+
g.successors(v).forEach(function(w) {
|
|
18
|
+
if (!Object.hasOwn(visited, w)) {
|
|
19
|
+
dfs(w);
|
|
20
|
+
entry.lowlink = Math.min(entry.lowlink, visited[w].lowlink);
|
|
21
|
+
} else if (visited[w].onStack) {
|
|
22
|
+
entry.lowlink = Math.min(entry.lowlink, visited[w].index);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
if (entry.lowlink === entry.index) {
|
|
27
|
+
var cmpt = [];
|
|
28
|
+
var w;
|
|
29
|
+
do {
|
|
30
|
+
w = stack.pop();
|
|
31
|
+
visited[w].onStack = false;
|
|
32
|
+
cmpt.push(w);
|
|
33
|
+
} while (v !== w);
|
|
34
|
+
results.push(cmpt);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
g.nodes().forEach(function(v) {
|
|
39
|
+
if (!Object.hasOwn(visited, v)) {
|
|
40
|
+
dfs(v);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return results;
|
|
45
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
function topsort(g) {
|
|
2
|
+
var visited = {};
|
|
3
|
+
var stack = {};
|
|
4
|
+
var results = [];
|
|
5
|
+
|
|
6
|
+
function visit(node) {
|
|
7
|
+
if (Object.hasOwn(stack, node)) {
|
|
8
|
+
throw new CycleException();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (!Object.hasOwn(visited, node)) {
|
|
12
|
+
stack[node] = true;
|
|
13
|
+
visited[node] = true;
|
|
14
|
+
g.predecessors(node).forEach(visit);
|
|
15
|
+
delete stack[node];
|
|
16
|
+
results.push(node);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
g.sinks().forEach(visit);
|
|
21
|
+
|
|
22
|
+
if (Object.keys(visited).length !== g.nodeCount()) {
|
|
23
|
+
throw new CycleException();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return results;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class CycleException extends Error {
|
|
30
|
+
constructor() {
|
|
31
|
+
super(...arguments);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = topsort;
|
|
36
|
+
topsort.CycleException = CycleException;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A min-priority queue data structure. This algorithm is derived from Cormen,
|
|
3
|
+
* et al., "Introduction to Algorithms". The basic idea of a min-priority
|
|
4
|
+
* queue is that you can efficiently (in O(1) time) get the smallest key in
|
|
5
|
+
* the queue. Adding and removing elements takes O(log n) time. A key can
|
|
6
|
+
* have its priority decreased in O(log n) time.
|
|
7
|
+
*/
|
|
8
|
+
class PriorityQueue {
|
|
9
|
+
_arr = [];
|
|
10
|
+
_keyIndices = {};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Returns the number of elements in the queue. Takes `O(1)` time.
|
|
14
|
+
*/
|
|
15
|
+
size() {
|
|
16
|
+
return this._arr.length;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Returns the keys that are in the queue. Takes `O(n)` time.
|
|
21
|
+
*/
|
|
22
|
+
keys() {
|
|
23
|
+
return this._arr.map(function(x) { return x.key; });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Returns `true` if **key** is in the queue and `false` if not.
|
|
28
|
+
*/
|
|
29
|
+
has(key) {
|
|
30
|
+
return Object.hasOwn(this._keyIndices, key);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Returns the priority for **key**. If **key** is not present in the queue
|
|
35
|
+
* then this function returns `undefined`. Takes `O(1)` time.
|
|
36
|
+
*
|
|
37
|
+
* @param {Object} key
|
|
38
|
+
*/
|
|
39
|
+
priority(key) {
|
|
40
|
+
var index = this._keyIndices[key];
|
|
41
|
+
if (index !== undefined) {
|
|
42
|
+
return this._arr[index].priority;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Returns the key for the minimum element in this queue. If the queue is
|
|
48
|
+
* empty this function throws an Error. Takes `O(1)` time.
|
|
49
|
+
*/
|
|
50
|
+
min() {
|
|
51
|
+
if (this.size() === 0) {
|
|
52
|
+
throw new Error("Queue underflow");
|
|
53
|
+
}
|
|
54
|
+
return this._arr[0].key;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Inserts a new key into the priority queue. If the key already exists in
|
|
59
|
+
* the queue this function returns `false`; otherwise it will return `true`.
|
|
60
|
+
* Takes `O(n)` time.
|
|
61
|
+
*
|
|
62
|
+
* @param {Object} key the key to add
|
|
63
|
+
* @param {Number} priority the initial priority for the key
|
|
64
|
+
*/
|
|
65
|
+
add(key, priority) {
|
|
66
|
+
var keyIndices = this._keyIndices;
|
|
67
|
+
key = String(key);
|
|
68
|
+
if (!Object.hasOwn(keyIndices, key)) {
|
|
69
|
+
var arr = this._arr;
|
|
70
|
+
var index = arr.length;
|
|
71
|
+
keyIndices[key] = index;
|
|
72
|
+
arr.push({key: key, priority: priority});
|
|
73
|
+
this._decrease(index);
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Removes and returns the smallest key in the queue. Takes `O(log n)` time.
|
|
81
|
+
*/
|
|
82
|
+
removeMin() {
|
|
83
|
+
this._swap(0, this._arr.length - 1);
|
|
84
|
+
var min = this._arr.pop();
|
|
85
|
+
delete this._keyIndices[min.key];
|
|
86
|
+
this._heapify(0);
|
|
87
|
+
return min.key;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Decreases the priority for **key** to **priority**. If the new priority is
|
|
92
|
+
* greater than the previous priority, this function will throw an Error.
|
|
93
|
+
*
|
|
94
|
+
* @param {Object} key the key for which to raise priority
|
|
95
|
+
* @param {Number} priority the new priority for the key
|
|
96
|
+
*/
|
|
97
|
+
decrease(key, priority) {
|
|
98
|
+
var index = this._keyIndices[key];
|
|
99
|
+
if (priority > this._arr[index].priority) {
|
|
100
|
+
throw new Error("New priority is greater than current priority. " +
|
|
101
|
+
"Key: " + key + " Old: " + this._arr[index].priority + " New: " + priority);
|
|
102
|
+
}
|
|
103
|
+
this._arr[index].priority = priority;
|
|
104
|
+
this._decrease(index);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
_heapify(i) {
|
|
108
|
+
var arr = this._arr;
|
|
109
|
+
var l = 2 * i;
|
|
110
|
+
var r = l + 1;
|
|
111
|
+
var largest = i;
|
|
112
|
+
if (l < arr.length) {
|
|
113
|
+
largest = arr[l].priority < arr[largest].priority ? l : largest;
|
|
114
|
+
if (r < arr.length) {
|
|
115
|
+
largest = arr[r].priority < arr[largest].priority ? r : largest;
|
|
116
|
+
}
|
|
117
|
+
if (largest !== i) {
|
|
118
|
+
this._swap(i, largest);
|
|
119
|
+
this._heapify(largest);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
_decrease(index) {
|
|
125
|
+
var arr = this._arr;
|
|
126
|
+
var priority = arr[index].priority;
|
|
127
|
+
var parent;
|
|
128
|
+
while (index !== 0) {
|
|
129
|
+
parent = index >> 1;
|
|
130
|
+
if (arr[parent].priority < priority) {
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
this._swap(index, parent);
|
|
134
|
+
index = parent;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
_swap(i, j) {
|
|
139
|
+
var arr = this._arr;
|
|
140
|
+
var keyIndices = this._keyIndices;
|
|
141
|
+
var origArrI = arr[i];
|
|
142
|
+
var origArrJ = arr[j];
|
|
143
|
+
arr[i] = origArrJ;
|
|
144
|
+
arr[j] = origArrI;
|
|
145
|
+
keyIndices[origArrJ.key] = i;
|
|
146
|
+
keyIndices[origArrI.key] = j;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
module.exports = PriorityQueue;
|