graphbase-js 2.1.4
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 +93 -0
- package/dist/graph-alg.min.js +1 -0
- package/dist/graph-settings.min.js +1 -0
- package/dist/graphlib.base.min.js +1 -0
- package/dist/graphlib.core.js +1238 -0
- package/dist/graphlib.core.min.js +143 -0
- package/dist/graphlib.js +6905 -0
- package/dist/graphlib.min.js +2522 -0
- package/index.js +38 -0
- package/lib/alg/components.js +27 -0
- package/lib/alg/dfs.js +42 -0
- package/lib/alg/dijkstra-all.js +10 -0
- package/lib/alg/dijkstra.js +54 -0
- package/lib/alg/find-cycles.js +10 -0
- package/lib/alg/floyd-warshall.js +50 -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 +52 -0
- package/lib/alg/tarjan.js +47 -0
- package/lib/alg/topsort.js +35 -0
- package/lib/data/priority-queue.js +152 -0
- package/lib/graph.js +561 -0
- package/lib/index.js +5 -0
- package/lib/json.js +66 -0
- package/lib/lodash.js +34 -0
- package/lib/version.js +1 -0
- package/package.json +53 -0
package/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2014, Chris Pettitt
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
|
6
|
+
* modification, are permitted provided that the following conditions are met:
|
|
7
|
+
*
|
|
8
|
+
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
* list of conditions and the following disclaimer.
|
|
10
|
+
*
|
|
11
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
* this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
* and/or other materials provided with the distribution.
|
|
14
|
+
*
|
|
15
|
+
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
16
|
+
* may be used to endorse or promote products derived from this software without
|
|
17
|
+
* specific prior written permission.
|
|
18
|
+
*
|
|
19
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
20
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
21
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
var lib = require("./lib");
|
|
32
|
+
|
|
33
|
+
module.exports = {
|
|
34
|
+
Graph: lib.Graph,
|
|
35
|
+
json: require("./lib/json"),
|
|
36
|
+
alg: require("./lib/alg"),
|
|
37
|
+
version: lib.version
|
|
38
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
var _ = require("../lodash");
|
|
2
|
+
|
|
3
|
+
module.exports = components;
|
|
4
|
+
|
|
5
|
+
function components(g) {
|
|
6
|
+
var visited = {};
|
|
7
|
+
var cmpts = [];
|
|
8
|
+
var cmpt;
|
|
9
|
+
|
|
10
|
+
function dfs(v) {
|
|
11
|
+
if (_.has(visited, v)) return;
|
|
12
|
+
visited[v] = true;
|
|
13
|
+
cmpt.push(v);
|
|
14
|
+
_.each(g.successors(v), dfs);
|
|
15
|
+
_.each(g.predecessors(v), dfs);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
_.each(g.nodes(), function(v) {
|
|
19
|
+
cmpt = [];
|
|
20
|
+
dfs(v);
|
|
21
|
+
if (cmpt.length) {
|
|
22
|
+
cmpts.push(cmpt);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return cmpts;
|
|
27
|
+
}
|
package/lib/alg/dfs.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
var _ = require("../lodash");
|
|
2
|
+
|
|
3
|
+
module.exports = dfs;
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* A helper that preforms a pre- or post-order traversal on the input graph
|
|
7
|
+
* and returns the nodes in the order they were visited. If the graph is
|
|
8
|
+
* undirected then this algorithm will navigate using neighbors. If the graph
|
|
9
|
+
* is directed then this algorithm will navigate using successors.
|
|
10
|
+
*
|
|
11
|
+
* Order must be one of "pre" or "post".
|
|
12
|
+
*/
|
|
13
|
+
function dfs(g, vs, order) {
|
|
14
|
+
if (!_.isArray(vs)) {
|
|
15
|
+
vs = [vs];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
var navigation = (g.isDirected() ? g.successors : g.neighbors).bind(g);
|
|
19
|
+
|
|
20
|
+
var acc = [];
|
|
21
|
+
var visited = {};
|
|
22
|
+
_.each(vs, function(v) {
|
|
23
|
+
if (!g.hasNode(v)) {
|
|
24
|
+
throw new Error("Graph does not have node: " + v);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
doDfs(g, v, order === "post", visited, navigation, acc);
|
|
28
|
+
});
|
|
29
|
+
return acc;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function doDfs(g, v, postorder, visited, navigation, acc) {
|
|
33
|
+
if (!_.has(visited, v)) {
|
|
34
|
+
visited[v] = true;
|
|
35
|
+
|
|
36
|
+
if (!postorder) { acc.push(v); }
|
|
37
|
+
_.each(navigation(v), function(w) {
|
|
38
|
+
doDfs(g, w, postorder, visited, navigation, acc);
|
|
39
|
+
});
|
|
40
|
+
if (postorder) { acc.push(v); }
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
var dijkstra = require("./dijkstra");
|
|
2
|
+
var _ = require("../lodash");
|
|
3
|
+
|
|
4
|
+
module.exports = dijkstraAll;
|
|
5
|
+
|
|
6
|
+
function dijkstraAll(g, weightFunc, edgeFunc) {
|
|
7
|
+
return _.transform(g.nodes(), function(acc, v) {
|
|
8
|
+
acc[v] = dijkstra(g, v, weightFunc, edgeFunc);
|
|
9
|
+
}, {});
|
|
10
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
var _ = require("../lodash");
|
|
2
|
+
var PriorityQueue = require("../data/priority-queue");
|
|
3
|
+
|
|
4
|
+
module.exports = dijkstra;
|
|
5
|
+
|
|
6
|
+
var DEFAULT_WEIGHT_FUNC = _.constant(1);
|
|
7
|
+
|
|
8
|
+
function dijkstra(g, source, weightFn, edgeFn) {
|
|
9
|
+
return runDijkstra(g, String(source),
|
|
10
|
+
weightFn || DEFAULT_WEIGHT_FUNC,
|
|
11
|
+
edgeFn || function(v) { return g.outEdges(v); });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function runDijkstra(g, source, weightFn, edgeFn) {
|
|
15
|
+
var results = {};
|
|
16
|
+
var pq = new PriorityQueue();
|
|
17
|
+
var v, vEntry;
|
|
18
|
+
|
|
19
|
+
var updateNeighbors = function(edge) {
|
|
20
|
+
var w = edge.v !== v ? edge.v : edge.w;
|
|
21
|
+
var wEntry = results[w];
|
|
22
|
+
var weight = weightFn(edge);
|
|
23
|
+
var distance = vEntry.distance + weight;
|
|
24
|
+
|
|
25
|
+
if (weight < 0) {
|
|
26
|
+
throw new Error("dijkstra does not allow negative edge weights. " +
|
|
27
|
+
"Bad edge: " + edge + " Weight: " + weight);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (distance < wEntry.distance) {
|
|
31
|
+
wEntry.distance = distance;
|
|
32
|
+
wEntry.predecessor = v;
|
|
33
|
+
pq.decrease(w, distance);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
g.nodes().forEach(function(v) {
|
|
38
|
+
var distance = v === source ? 0 : Number.POSITIVE_INFINITY;
|
|
39
|
+
results[v] = { distance: distance };
|
|
40
|
+
pq.add(v, distance);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
while (pq.size() > 0) {
|
|
44
|
+
v = pq.removeMin();
|
|
45
|
+
vEntry = results[v];
|
|
46
|
+
if (vEntry.distance === Number.POSITIVE_INFINITY) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
edgeFn(v).forEach(updateNeighbors);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return results;
|
|
54
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
var _ = require("../lodash");
|
|
2
|
+
var tarjan = require("./tarjan");
|
|
3
|
+
|
|
4
|
+
module.exports = findCycles;
|
|
5
|
+
|
|
6
|
+
function findCycles(g) {
|
|
7
|
+
return _.filter(tarjan(g), function(cmpt) {
|
|
8
|
+
return cmpt.length > 1 || (cmpt.length === 1 && g.hasEdge(cmpt[0], cmpt[0]));
|
|
9
|
+
});
|
|
10
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
var _ = require("../lodash");
|
|
2
|
+
|
|
3
|
+
module.exports = floydWarshall;
|
|
4
|
+
|
|
5
|
+
var DEFAULT_WEIGHT_FUNC = _.constant(1);
|
|
6
|
+
|
|
7
|
+
function floydWarshall(g, weightFn, edgeFn) {
|
|
8
|
+
return runFloydWarshall(g,
|
|
9
|
+
weightFn || DEFAULT_WEIGHT_FUNC,
|
|
10
|
+
edgeFn || function(v) { return g.outEdges(v); });
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function runFloydWarshall(g, weightFn, edgeFn) {
|
|
14
|
+
var results = {};
|
|
15
|
+
var nodes = g.nodes();
|
|
16
|
+
|
|
17
|
+
nodes.forEach(function(v) {
|
|
18
|
+
results[v] = {};
|
|
19
|
+
results[v][v] = { distance: 0 };
|
|
20
|
+
nodes.forEach(function(w) {
|
|
21
|
+
if (v !== w) {
|
|
22
|
+
results[v][w] = { distance: Number.POSITIVE_INFINITY };
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
edgeFn(v).forEach(function(edge) {
|
|
26
|
+
var w = edge.v === v ? edge.w : edge.v;
|
|
27
|
+
var d = weightFn(edge);
|
|
28
|
+
results[v][w] = { distance: d, predecessor: v };
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
nodes.forEach(function(k) {
|
|
33
|
+
var rowK = results[k];
|
|
34
|
+
nodes.forEach(function(i) {
|
|
35
|
+
var rowI = results[i];
|
|
36
|
+
nodes.forEach(function(j) {
|
|
37
|
+
var ik = rowI[k];
|
|
38
|
+
var kj = rowK[j];
|
|
39
|
+
var ij = rowI[j];
|
|
40
|
+
var altDistance = ik.distance + kj.distance;
|
|
41
|
+
if (altDistance < ij.distance) {
|
|
42
|
+
ij.distance = altDistance;
|
|
43
|
+
ij.predecessor = kj.predecessor;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return results;
|
|
50
|
+
}
|
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,52 @@
|
|
|
1
|
+
var _ = require("../lodash");
|
|
2
|
+
var Graph = require("../graph");
|
|
3
|
+
var PriorityQueue = require("../data/priority-queue");
|
|
4
|
+
|
|
5
|
+
module.exports = prim;
|
|
6
|
+
|
|
7
|
+
function prim(g, weightFunc) {
|
|
8
|
+
var result = new Graph();
|
|
9
|
+
var parents = {};
|
|
10
|
+
var pq = new PriorityQueue();
|
|
11
|
+
var v;
|
|
12
|
+
|
|
13
|
+
function updateNeighbors(edge) {
|
|
14
|
+
var w = edge.v === v ? edge.w : edge.v;
|
|
15
|
+
var pri = pq.priority(w);
|
|
16
|
+
if (pri !== undefined) {
|
|
17
|
+
var edgeWeight = weightFunc(edge);
|
|
18
|
+
if (edgeWeight < pri) {
|
|
19
|
+
parents[w] = v;
|
|
20
|
+
pq.decrease(w, edgeWeight);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (g.nodeCount() === 0) {
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
_.each(g.nodes(), function(v) {
|
|
30
|
+
pq.add(v, Number.POSITIVE_INFINITY);
|
|
31
|
+
result.setNode(v);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Start from an arbitrary node
|
|
35
|
+
pq.decrease(g.nodes()[0], 0);
|
|
36
|
+
|
|
37
|
+
var init = false;
|
|
38
|
+
while (pq.size() > 0) {
|
|
39
|
+
v = pq.removeMin();
|
|
40
|
+
if (_.has(parents, v)) {
|
|
41
|
+
result.setEdge(v, parents[v]);
|
|
42
|
+
} else if (init) {
|
|
43
|
+
throw new Error("Input graph is not connected: " + g);
|
|
44
|
+
} else {
|
|
45
|
+
init = true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
g.nodeEdges(v).forEach(updateNeighbors);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
var _ = require("../lodash");
|
|
2
|
+
|
|
3
|
+
module.exports = tarjan;
|
|
4
|
+
|
|
5
|
+
function tarjan(g) {
|
|
6
|
+
var index = 0;
|
|
7
|
+
var stack = [];
|
|
8
|
+
var visited = {}; // node id -> { onStack, lowlink, index }
|
|
9
|
+
var results = [];
|
|
10
|
+
|
|
11
|
+
function dfs(v) {
|
|
12
|
+
var entry = visited[v] = {
|
|
13
|
+
onStack: true,
|
|
14
|
+
lowlink: index,
|
|
15
|
+
index: index++
|
|
16
|
+
};
|
|
17
|
+
stack.push(v);
|
|
18
|
+
|
|
19
|
+
g.successors(v).forEach(function(w) {
|
|
20
|
+
if (!_.has(visited, w)) {
|
|
21
|
+
dfs(w);
|
|
22
|
+
entry.lowlink = Math.min(entry.lowlink, visited[w].lowlink);
|
|
23
|
+
} else if (visited[w].onStack) {
|
|
24
|
+
entry.lowlink = Math.min(entry.lowlink, visited[w].index);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
if (entry.lowlink === entry.index) {
|
|
29
|
+
var cmpt = [];
|
|
30
|
+
var w;
|
|
31
|
+
do {
|
|
32
|
+
w = stack.pop();
|
|
33
|
+
visited[w].onStack = false;
|
|
34
|
+
cmpt.push(w);
|
|
35
|
+
} while (v !== w);
|
|
36
|
+
results.push(cmpt);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
g.nodes().forEach(function(v) {
|
|
41
|
+
if (!_.has(visited, v)) {
|
|
42
|
+
dfs(v);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return results;
|
|
47
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
var _ = require("../lodash");
|
|
2
|
+
|
|
3
|
+
module.exports = topsort;
|
|
4
|
+
topsort.CycleException = CycleException;
|
|
5
|
+
|
|
6
|
+
function topsort(g) {
|
|
7
|
+
var visited = {};
|
|
8
|
+
var stack = {};
|
|
9
|
+
var results = [];
|
|
10
|
+
|
|
11
|
+
function visit(node) {
|
|
12
|
+
if (_.has(stack, node)) {
|
|
13
|
+
throw new CycleException();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!_.has(visited, node)) {
|
|
17
|
+
stack[node] = true;
|
|
18
|
+
visited[node] = true;
|
|
19
|
+
_.each(g.predecessors(node), visit);
|
|
20
|
+
delete stack[node];
|
|
21
|
+
results.push(node);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
_.each(g.sinks(), visit);
|
|
26
|
+
|
|
27
|
+
if (_.size(visited) !== g.nodeCount()) {
|
|
28
|
+
throw new CycleException();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return results;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function CycleException() {}
|
|
35
|
+
CycleException.prototype = new Error(); // must be an instance of Error to pass testing
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
var _ = require("../lodash");
|
|
2
|
+
|
|
3
|
+
module.exports = PriorityQueue;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A min-priority queue data structure. This algorithm is derived from Cormen,
|
|
7
|
+
* et al., "Introduction to Algorithms". The basic idea of a min-priority
|
|
8
|
+
* queue is that you can efficiently (in O(1) time) get the smallest key in
|
|
9
|
+
* the queue. Adding and removing elements takes O(log n) time. A key can
|
|
10
|
+
* have its priority decreased in O(log n) time.
|
|
11
|
+
*/
|
|
12
|
+
function PriorityQueue() {
|
|
13
|
+
this._arr = [];
|
|
14
|
+
this._keyIndices = {};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns the number of elements in the queue. Takes `O(1)` time.
|
|
19
|
+
*/
|
|
20
|
+
PriorityQueue.prototype.size = function() {
|
|
21
|
+
return this._arr.length;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Returns the keys that are in the queue. Takes `O(n)` time.
|
|
26
|
+
*/
|
|
27
|
+
PriorityQueue.prototype.keys = function() {
|
|
28
|
+
return this._arr.map(function(x) { return x.key; });
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns `true` if **key** is in the queue and `false` if not.
|
|
33
|
+
*/
|
|
34
|
+
PriorityQueue.prototype.has = function(key) {
|
|
35
|
+
return _.has(this._keyIndices, key);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Returns the priority for **key**. If **key** is not present in the queue
|
|
40
|
+
* then this function returns `undefined`. Takes `O(1)` time.
|
|
41
|
+
*
|
|
42
|
+
* @param {Object} key
|
|
43
|
+
*/
|
|
44
|
+
PriorityQueue.prototype.priority = function(key) {
|
|
45
|
+
var index = this._keyIndices[key];
|
|
46
|
+
if (index !== undefined) {
|
|
47
|
+
return this._arr[index].priority;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Returns the key for the minimum element in this queue. If the queue is
|
|
53
|
+
* empty this function throws an Error. Takes `O(1)` time.
|
|
54
|
+
*/
|
|
55
|
+
PriorityQueue.prototype.min = function() {
|
|
56
|
+
if (this.size() === 0) {
|
|
57
|
+
throw new Error("Queue underflow");
|
|
58
|
+
}
|
|
59
|
+
return this._arr[0].key;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Inserts a new key into the priority queue. If the key already exists in
|
|
64
|
+
* the queue this function returns `false`; otherwise it will return `true`.
|
|
65
|
+
* Takes `O(n)` time.
|
|
66
|
+
*
|
|
67
|
+
* @param {Object} key the key to add
|
|
68
|
+
* @param {Number} priority the initial priority for the key
|
|
69
|
+
*/
|
|
70
|
+
PriorityQueue.prototype.add = function(key, priority) {
|
|
71
|
+
var keyIndices = this._keyIndices;
|
|
72
|
+
key = String(key);
|
|
73
|
+
if (!_.has(keyIndices, key)) {
|
|
74
|
+
var arr = this._arr;
|
|
75
|
+
var index = arr.length;
|
|
76
|
+
keyIndices[key] = index;
|
|
77
|
+
arr.push({key: key, priority: priority});
|
|
78
|
+
this._decrease(index);
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Removes and returns the smallest key in the queue. Takes `O(log n)` time.
|
|
86
|
+
*/
|
|
87
|
+
PriorityQueue.prototype.removeMin = function() {
|
|
88
|
+
this._swap(0, this._arr.length - 1);
|
|
89
|
+
var min = this._arr.pop();
|
|
90
|
+
delete this._keyIndices[min.key];
|
|
91
|
+
this._heapify(0);
|
|
92
|
+
return min.key;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Decreases the priority for **key** to **priority**. If the new priority is
|
|
97
|
+
* greater than the previous priority, this function will throw an Error.
|
|
98
|
+
*
|
|
99
|
+
* @param {Object} key the key for which to raise priority
|
|
100
|
+
* @param {Number} priority the new priority for the key
|
|
101
|
+
*/
|
|
102
|
+
PriorityQueue.prototype.decrease = function(key, priority) {
|
|
103
|
+
var index = this._keyIndices[key];
|
|
104
|
+
if (priority > this._arr[index].priority) {
|
|
105
|
+
throw new Error("New priority is greater than current priority. " +
|
|
106
|
+
"Key: " + key + " Old: " + this._arr[index].priority + " New: " + priority);
|
|
107
|
+
}
|
|
108
|
+
this._arr[index].priority = priority;
|
|
109
|
+
this._decrease(index);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
PriorityQueue.prototype._heapify = function(i) {
|
|
113
|
+
var arr = this._arr;
|
|
114
|
+
var l = 2 * i;
|
|
115
|
+
var r = l + 1;
|
|
116
|
+
var largest = i;
|
|
117
|
+
if (l < arr.length) {
|
|
118
|
+
largest = arr[l].priority < arr[largest].priority ? l : largest;
|
|
119
|
+
if (r < arr.length) {
|
|
120
|
+
largest = arr[r].priority < arr[largest].priority ? r : largest;
|
|
121
|
+
}
|
|
122
|
+
if (largest !== i) {
|
|
123
|
+
this._swap(i, largest);
|
|
124
|
+
this._heapify(largest);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
PriorityQueue.prototype._decrease = function(index) {
|
|
130
|
+
var arr = this._arr;
|
|
131
|
+
var priority = arr[index].priority;
|
|
132
|
+
var parent;
|
|
133
|
+
while (index !== 0) {
|
|
134
|
+
parent = index >> 1;
|
|
135
|
+
if (arr[parent].priority < priority) {
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
this._swap(index, parent);
|
|
139
|
+
index = parent;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
PriorityQueue.prototype._swap = function(i, j) {
|
|
144
|
+
var arr = this._arr;
|
|
145
|
+
var keyIndices = this._keyIndices;
|
|
146
|
+
var origArrI = arr[i];
|
|
147
|
+
var origArrJ = arr[j];
|
|
148
|
+
arr[i] = origArrJ;
|
|
149
|
+
arr[j] = origArrI;
|
|
150
|
+
keyIndices[origArrJ.key] = i;
|
|
151
|
+
keyIndices[origArrI.key] = j;
|
|
152
|
+
};
|