graphology-shortest-path
Advanced tools
Comparing version 1.4.2 to 1.5.0
import Graph from 'graphology-types'; | ||
type SingleSourceDijkstraResult = {[key: string]: string[];} | ||
type SingleSourceDijkstraResult = {[key: string]: string[]}; | ||
type BidirectionalDijstraResult = string[]; | ||
@@ -11,6 +11,14 @@ type BrandesResult = [ | ||
interface IDijkstra { | ||
bidirectional(graph: Graph, source: string, target: string, weightAttribute: string): BidirectionalDijstraResult; | ||
singleSource(graph: Graph, source: string, weightAttribute: string): SingleSourceDijkstraResult; | ||
bidirectional( | ||
graph: Graph, | ||
source: string, | ||
target: string, | ||
weightAttribute: string | ||
): BidirectionalDijstraResult; | ||
singleSource( | ||
graph: Graph, | ||
source: string, | ||
weightAttribute: string | ||
): SingleSourceDijkstraResult; | ||
brandes(graph: Graph, source: string, weightAttribute: string): BrandesResult; | ||
@@ -17,0 +25,0 @@ } |
228
dijkstra.js
@@ -8,3 +8,3 @@ /** | ||
var isGraph = require('graphology-utils/is-graph'), | ||
Heap = require('mnemonist/heap'); | ||
Heap = require('mnemonist/heap'); | ||
@@ -19,16 +19,10 @@ /** | ||
function DIJKSTRA_HEAP_COMPARATOR(a, b) { | ||
if (a[0] > b[0]) | ||
return 1; | ||
if (a[0] < b[0]) | ||
return -1; | ||
if (a[0] > b[0]) return 1; | ||
if (a[0] < b[0]) return -1; | ||
if (a[1] > b[1]) | ||
return 1; | ||
if (a[1] < b[1]) | ||
return -1; | ||
if (a[1] > b[1]) return 1; | ||
if (a[1] < b[1]) return -1; | ||
if (a[2] > b[2]) | ||
return 1; | ||
if (a[2] < b[2]) | ||
return -1; | ||
if (a[2] > b[2]) return 1; | ||
if (a[2] < b[2]) return -1; | ||
@@ -39,21 +33,13 @@ return 0; | ||
function BRANDES_DIJKSTRA_HEAP_COMPARATOR(a, b) { | ||
if (a[0] > b[0]) | ||
return 1; | ||
if (a[0] < b[0]) | ||
return -1; | ||
if (a[0] > b[0]) return 1; | ||
if (a[0] < b[0]) return -1; | ||
if (a[1] > b[1]) | ||
return 1; | ||
if (a[1] < b[1]) | ||
return -1; | ||
if (a[1] > b[1]) return 1; | ||
if (a[1] < b[1]) return -1; | ||
if (a[2] > b[2]) | ||
return 1; | ||
if (a[2] < b[2]) | ||
return -1; | ||
if (a[2] > b[2]) return 1; | ||
if (a[2] < b[2]) return -1; | ||
if (a[3] > b[3]) | ||
return 1; | ||
if (a[3] < b[3]) | ||
return - 1; | ||
if (a[3] > b[3]) return 1; | ||
if (a[3] < b[3]) return -1; | ||
@@ -80,17 +66,26 @@ return 0; | ||
if (!isGraph(graph)) | ||
throw new Error('graphology-shortest-path/dijkstra: invalid graphology instance.'); | ||
throw new Error( | ||
'graphology-shortest-path/dijkstra: invalid graphology instance.' | ||
); | ||
if (source && !graph.hasNode(source)) | ||
throw new Error('graphology-shortest-path/dijkstra: the "' + source + '" source node does not exist in the given graph.'); | ||
throw new Error( | ||
'graphology-shortest-path/dijkstra: the "' + | ||
source + | ||
'" source node does not exist in the given graph.' | ||
); | ||
if (target && !graph.hasNode(target)) | ||
throw new Error('graphology-shortest-path/dijkstra: the "' + target + '" target node does not exist in the given graph.'); | ||
throw new Error( | ||
'graphology-shortest-path/dijkstra: the "' + | ||
target + | ||
'" target node does not exist in the given graph.' | ||
); | ||
weightAttribute = weightAttribute || DEFAULTS.weightAttribute; | ||
var getWeight = function(edge) { | ||
var getWeight = function (edge) { | ||
var weight = graph.getEdgeAttribute(edge, weightAttribute); | ||
if (typeof weight !== 'number' || isNaN(weight)) | ||
return 1; | ||
if (typeof weight !== 'number' || isNaN(weight)) return 1; | ||
@@ -100,9 +95,11 @@ return weight; | ||
if (source === target) | ||
return [0, [source]]; | ||
if (source === target) return [0, [source]]; | ||
var distances = [{}, {}], | ||
paths = [{}, {}], | ||
fringe = [new Heap(DIJKSTRA_HEAP_COMPARATOR), new Heap(DIJKSTRA_HEAP_COMPARATOR)], | ||
seen = [{}, {}]; | ||
paths = [{}, {}], | ||
fringe = [ | ||
new Heap(DIJKSTRA_HEAP_COMPARATOR), | ||
new Heap(DIJKSTRA_HEAP_COMPARATOR) | ||
], | ||
seen = [{}, {}]; | ||
@@ -116,15 +113,15 @@ paths[0][source] = [source]; | ||
var finalPath = [], | ||
finalDistance = Infinity; | ||
finalDistance = Infinity; | ||
var count = 0, | ||
dir = 1, | ||
item, | ||
edges, | ||
cost, | ||
d, | ||
v, | ||
u, | ||
e, | ||
i, | ||
l; | ||
dir = 1, | ||
item, | ||
edges, | ||
cost, | ||
d, | ||
v, | ||
u, | ||
e, | ||
i, | ||
l; | ||
@@ -135,3 +132,2 @@ fringe[0].push([0, count++, source]); | ||
while (fringe[0].size && fringe[1].size) { | ||
// Swapping direction | ||
@@ -144,4 +140,3 @@ dir = 1 - dir; | ||
if (v in distances[dir]) | ||
continue; | ||
if (v in distances[dir]) continue; | ||
@@ -151,8 +146,5 @@ distances[dir][v] = d; | ||
// Shortest path is found? | ||
if (v in distances[1 - dir]) | ||
return [finalDistance, finalPath]; | ||
if (v in distances[1 - dir]) return [finalDistance, finalPath]; | ||
edges = dir === 1 ? | ||
graph.inboundEdges(v) : | ||
graph.outboundEdges(v); | ||
edges = dir === 1 ? graph.inboundEdges(v) : graph.outboundEdges(v); | ||
@@ -165,5 +157,6 @@ for (i = 0, l = edges.length; i < l; i++) { | ||
if (u in distances[dir] && cost < distances[dir][u]) { | ||
throw Error('graphology-shortest-path/dijkstra: contradictory paths found. Do some of your edges have a negative weight?'); | ||
} | ||
else if (!(u in seen[dir]) || cost < seen[dir][u]) { | ||
throw Error( | ||
'graphology-shortest-path/dijkstra: contradictory paths found. Do some of your edges have a negative weight?' | ||
); | ||
} else if (!(u in seen[dir]) || cost < seen[dir][u]) { | ||
seen[dir][u] = cost; | ||
@@ -213,8 +206,13 @@ fringe[dir].push([cost, count++, u]); | ||
) { | ||
if (!isGraph(graph)) | ||
throw new Error('graphology-shortest-path/dijkstra: invalid graphology instance.'); | ||
throw new Error( | ||
'graphology-shortest-path/dijkstra: invalid graphology instance.' | ||
); | ||
if (target && !graph.hasNode(target)) | ||
throw new Error('graphology-shortest-path/dijkstra: the "' + target + '" target node does not exist in the given graph.'); | ||
throw new Error( | ||
'graphology-shortest-path/dijkstra: the "' + | ||
target + | ||
'" target node does not exist in the given graph.' | ||
); | ||
@@ -224,7 +222,6 @@ weightAttribute = weightAttribute || DEFAULTS.weightAttribute; | ||
// Building necessary functions | ||
var getWeight = function(edge) { | ||
var getWeight = function (edge) { | ||
var weight = graph.getEdgeAttribute(edge, weightAttribute); | ||
if (typeof weight !== 'number' || isNaN(weight)) | ||
return 1; | ||
if (typeof weight !== 'number' || isNaN(weight)) return 1; | ||
@@ -235,17 +232,17 @@ return weight; | ||
var distances = {}, | ||
seen = {}, | ||
fringe = new Heap(DIJKSTRA_HEAP_COMPARATOR); | ||
seen = {}, | ||
fringe = new Heap(DIJKSTRA_HEAP_COMPARATOR); | ||
var count = 0, | ||
edges, | ||
item, | ||
cost, | ||
v, | ||
u, | ||
e, | ||
d, | ||
i, | ||
j, | ||
l, | ||
m; | ||
edges, | ||
item, | ||
cost, | ||
v, | ||
u, | ||
e, | ||
d, | ||
i, | ||
j, | ||
l, | ||
m; | ||
@@ -257,4 +254,3 @@ for (i = 0, l = sources.length; i < l; i++) { | ||
if (paths) | ||
paths[v] = [v]; | ||
if (paths) paths[v] = [v]; | ||
} | ||
@@ -267,9 +263,7 @@ | ||
if (v in distances) | ||
continue; | ||
if (v in distances) continue; | ||
distances[v] = d; | ||
if (v === target) | ||
break; | ||
if (v === target) break; | ||
@@ -283,15 +277,13 @@ edges = graph.outboundEdges(v); | ||
if (cutoff && cost > cutoff) | ||
continue; | ||
if (cutoff && cost > cutoff) continue; | ||
if (u in distances && cost < distances[u]) { | ||
throw Error('graphology-shortest-path/dijkstra: contradictory paths found. Do some of your edges have a negative weight?'); | ||
} | ||
else if (!(u in seen) || cost < seen[u]) { | ||
throw Error( | ||
'graphology-shortest-path/dijkstra: contradictory paths found. Do some of your edges have a negative weight?' | ||
); | ||
} else if (!(u in seen) || cost < seen[u]) { | ||
seen[u] = cost; | ||
fringe.push([cost, count++, u]); | ||
if (paths) | ||
paths[u] = paths[v].concat(u); | ||
if (paths) paths[u] = paths[v].concat(u); | ||
} | ||
@@ -316,10 +308,3 @@ } | ||
abstractDijkstraMultisource( | ||
graph, | ||
[source], | ||
weightAttribute, | ||
0, | ||
null, | ||
paths | ||
); | ||
abstractDijkstraMultisource(graph, [source], weightAttribute, 0, null, paths); | ||
@@ -330,3 +315,8 @@ return paths; | ||
function bidirectionalDijkstra(graph, source, target, weightAttribute) { | ||
return abstractBidirectionalDijkstra(graph, source, target, weightAttribute)[1]; | ||
return abstractBidirectionalDijkstra( | ||
graph, | ||
source, | ||
target, | ||
weightAttribute | ||
)[1]; | ||
} | ||
@@ -352,16 +342,16 @@ | ||
var S = [], | ||
P = {}, | ||
sigma = {}; | ||
P = {}, | ||
sigma = {}; | ||
var nodes = graph.nodes(), | ||
edges, | ||
item, | ||
pred, | ||
dist, | ||
cost, | ||
v, | ||
w, | ||
e, | ||
i, | ||
l; | ||
edges, | ||
item, | ||
pred, | ||
dist, | ||
cost, | ||
v, | ||
w, | ||
e, | ||
i, | ||
l; | ||
@@ -392,4 +382,3 @@ for (i = 0, l = nodes.length; i < l; i++) { | ||
if (v in D) | ||
continue; | ||
if (v in D) continue; | ||
@@ -412,4 +401,3 @@ sigma[v] += sigma[pred]; | ||
P[w] = [v]; | ||
} | ||
else if (cost === seen[w]) { | ||
} else if (cost === seen[w]) { | ||
sigma[w] += sigma[v]; | ||
@@ -416,0 +404,0 @@ P[w].push(v); |
import Graph from 'graphology-types'; | ||
import FixedStack from 'mnemonist/fixed-stack'; | ||
import {OutboundNeighborhoodIndex, WeightedOutboundNeighborhoodIndex} from 'graphology-indices/neighborhood/outbound'; | ||
import { | ||
OutboundNeighborhoodIndex, | ||
WeightedOutboundNeighborhoodIndex | ||
} from 'graphology-indices/neighborhood/outbound'; | ||
@@ -26,5 +29,2 @@ type IndexedBrandesResult = [ | ||
export { | ||
createUnweightedIndexedBrandes, | ||
createDijkstraIndexedBrandes | ||
}; | ||
export {createUnweightedIndexedBrandes, createDijkstraIndexedBrandes}; |
@@ -9,9 +9,10 @@ /** | ||
var FixedDeque = require('mnemonist/fixed-deque'), | ||
FixedStack = require('mnemonist/fixed-stack'), | ||
Heap = require('mnemonist/heap'), | ||
typed = require('mnemonist/utils/typed-arrays'), | ||
neighborhoodIndices = require('graphology-indices/neighborhood/outbound'); | ||
FixedStack = require('mnemonist/fixed-stack'), | ||
Heap = require('mnemonist/heap'), | ||
typed = require('mnemonist/utils/typed-arrays'), | ||
neighborhoodIndices = require('graphology-indices/neighborhood/outbound'); | ||
var OutboundNeighborhoodIndex = neighborhoodIndices.OutboundNeighborhoodIndex, | ||
WeightedOutboundNeighborhoodIndex = neighborhoodIndices.WeightedOutboundNeighborhoodIndex; | ||
WeightedOutboundNeighborhoodIndex = | ||
neighborhoodIndices.WeightedOutboundNeighborhoodIndex; | ||
@@ -28,11 +29,12 @@ /** | ||
*/ | ||
exports.createUnweightedIndexedBrandes = function createUnweightedIndexedBrandes(graph) { | ||
var neighborhoodIndex = new OutboundNeighborhoodIndex(graph); | ||
exports.createUnweightedIndexedBrandes = | ||
function createUnweightedIndexedBrandes(graph) { | ||
var neighborhoodIndex = new OutboundNeighborhoodIndex(graph); | ||
var neighborhood = neighborhoodIndex.neighborhood, | ||
var neighborhood = neighborhoodIndex.neighborhood, | ||
starts = neighborhoodIndex.starts; | ||
var order = graph.order; | ||
var order = graph.order; | ||
var S = new FixedStack(typed.getPointerArray(order), order), | ||
var S = new FixedStack(typed.getPointerArray(order), order), | ||
sigma = new Uint32Array(order), | ||
@@ -42,77 +44,63 @@ P = new Array(order), | ||
var Q = new FixedDeque(Uint32Array, order); | ||
var Q = new FixedDeque(Uint32Array, order); | ||
var brandes = function(sourceIndex) { | ||
var Dv, | ||
sigmav, | ||
start, | ||
stop, | ||
j, | ||
v, | ||
w; | ||
var brandes = function (sourceIndex) { | ||
var Dv, sigmav, start, stop, j, v, w; | ||
for (v = 0; v < order; v++) { | ||
P[v] = []; | ||
sigma[v] = 0; | ||
D[v] = -1; | ||
} | ||
for (v = 0; v < order; v++) { | ||
P[v] = []; | ||
sigma[v] = 0; | ||
D[v] = -1; | ||
} | ||
sigma[sourceIndex] = 1; | ||
D[sourceIndex] = 0; | ||
sigma[sourceIndex] = 1; | ||
D[sourceIndex] = 0; | ||
Q.push(sourceIndex); | ||
Q.push(sourceIndex); | ||
while (Q.size !== 0) { | ||
v = Q.shift(); | ||
S.push(v); | ||
while (Q.size !== 0) { | ||
v = Q.shift(); | ||
S.push(v); | ||
Dv = D[v]; | ||
sigmav = sigma[v]; | ||
Dv = D[v]; | ||
sigmav = sigma[v]; | ||
start = starts[v]; | ||
stop = starts[v + 1]; | ||
start = starts[v]; | ||
stop = starts[v + 1]; | ||
for (j = start; j < stop; j++) { | ||
w = neighborhood[j]; | ||
for (j = start; j < stop; j++) { | ||
w = neighborhood[j]; | ||
if (D[w] === -1) { | ||
Q.push(w); | ||
D[w] = Dv + 1; | ||
} | ||
if (D[w] === -1) { | ||
Q.push(w); | ||
D[w] = Dv + 1; | ||
} | ||
if (D[w] === Dv + 1) { | ||
sigma[w] += sigmav; | ||
P[w].push(v); | ||
if (D[w] === Dv + 1) { | ||
sigma[w] += sigmav; | ||
P[w].push(v); | ||
} | ||
} | ||
} | ||
} | ||
return [S, P, sigma]; | ||
}; | ||
return [S, P, sigma]; | ||
}; | ||
brandes.index = neighborhoodIndex; | ||
brandes.index = neighborhoodIndex; | ||
return brandes; | ||
}; | ||
return brandes; | ||
}; | ||
function BRANDES_DIJKSTRA_HEAP_COMPARATOR(a, b) { | ||
if (a[0] > b[0]) | ||
return 1; | ||
if (a[0] < b[0]) | ||
return -1; | ||
if (a[0] > b[0]) return 1; | ||
if (a[0] < b[0]) return -1; | ||
if (a[1] > b[1]) | ||
return 1; | ||
if (a[1] < b[1]) | ||
return -1; | ||
if (a[1] > b[1]) return 1; | ||
if (a[1] < b[1]) return -1; | ||
if (a[2] > b[2]) | ||
return 1; | ||
if (a[2] < b[2]) | ||
return -1; | ||
if (a[2] > b[2]) return 1; | ||
if (a[2] < b[2]) return -1; | ||
if (a[3] > b[3]) | ||
return 1; | ||
if (a[3] < b[3]) | ||
return - 1; | ||
if (a[3] > b[3]) return 1; | ||
if (a[3] < b[3]) return -1; | ||
@@ -133,8 +121,14 @@ return 0; | ||
*/ | ||
exports.createDijkstraIndexedBrandes = function createDijkstraIndexedBrandes(graph, weightAttribute) { | ||
var neighborhoodIndex = new WeightedOutboundNeighborhoodIndex(graph, weightAttribute); | ||
exports.createDijkstraIndexedBrandes = function createDijkstraIndexedBrandes( | ||
graph, | ||
weightAttribute | ||
) { | ||
var neighborhoodIndex = new WeightedOutboundNeighborhoodIndex( | ||
graph, | ||
weightAttribute | ||
); | ||
var neighborhood = neighborhoodIndex.neighborhood, | ||
weights = neighborhoodIndex.weights, | ||
starts = neighborhoodIndex.starts; | ||
weights = neighborhoodIndex.weights, | ||
starts = neighborhoodIndex.starts; | ||
@@ -144,6 +138,6 @@ var order = graph.order; | ||
var S = new FixedStack(typed.getPointerArray(order), order), | ||
sigma = new Uint32Array(order), | ||
P = new Array(order), | ||
D = new Float64Array(order), | ||
seen = new Float64Array(order); | ||
sigma = new Uint32Array(order), | ||
P = new Array(order), | ||
D = new Float64Array(order), | ||
seen = new Float64Array(order); | ||
@@ -153,12 +147,4 @@ // TODO: use fixed-size heap | ||
var brandes = function(sourceIndex) { | ||
var start, | ||
stop, | ||
item, | ||
dist, | ||
pred, | ||
cost, | ||
j, | ||
v, | ||
w; | ||
var brandes = function (sourceIndex) { | ||
var start, stop, item, dist, pred, cost, j, v, w; | ||
@@ -185,4 +171,3 @@ var count = 0; | ||
if (D[v] !== -1) | ||
continue; | ||
if (D[v] !== -1) continue; | ||
@@ -205,4 +190,3 @@ S.push(v); | ||
P[w] = [v]; | ||
} | ||
else if (cost === seen[w]) { | ||
} else if (cost === seen[w]) { | ||
sigma[w] += sigma[v]; | ||
@@ -209,0 +193,0 @@ P[w].push(v); |
{ | ||
"name": "graphology-shortest-path", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"description": "Shortest path functions for graphology.", | ||
@@ -15,4 +15,3 @@ "main": "index.js", | ||
"scripts": { | ||
"lint": "eslint *.js", | ||
"prepublish": "npm run lint && npm test", | ||
"prepublishOnly": "npm test", | ||
"test": "mocha" | ||
@@ -22,3 +21,3 @@ }, | ||
"type": "git", | ||
"url": "git+https://github.com/graphology/graphology-shortest-path.git" | ||
"url": "git+https://github.com/graphology/graphology.git" | ||
}, | ||
@@ -38,5 +37,5 @@ "keywords": [ | ||
"bugs": { | ||
"url": "https://github.com/graphology/graphology-shortest-path/issues" | ||
"url": "https://github.com/graphology/graphology/issues" | ||
}, | ||
"homepage": "https://github.com/graphology/graphology-shortest-path#readme", | ||
"homepage": "https://github.com/graphology/graphology#readme", | ||
"peerDependencies": { | ||
@@ -47,9 +46,6 @@ "graphology-types": ">=0.20.0" | ||
"@yomguithereal/helpers": "^1.1.1", | ||
"graphology-indices": "^0.13.3", | ||
"graphology-utils": "^2.0.0", | ||
"graphology-indices": "^0.14.0", | ||
"graphology-utils": "^2.1.2", | ||
"mnemonist": "^0.38.1" | ||
}, | ||
"eslintConfig": { | ||
"extends": "@yomguithereal/eslint-config" | ||
} | ||
} |
@@ -1,3 +0,1 @@ | ||
[](https://travis-ci.org/graphology/graphology-shortest-path) | ||
# Graphology Shortest Path | ||
@@ -15,3 +13,3 @@ | ||
* [Unweighted](#unweighted) | ||
- [Unweighted](#unweighted) | ||
- [shortestPath](#shortestpath) | ||
@@ -23,3 +21,3 @@ - [bidirectional](#bidirectional) | ||
- [brandes](#brandes) | ||
* [Dijkstra](#dijkstra) | ||
- [Dijkstra](#dijkstra) | ||
- [bidirectional](#dijkstra-bidirectional) | ||
@@ -49,7 +47,7 @@ - [singleSource](#dijkstra-singlesource) | ||
*Arguments* | ||
_Arguments_ | ||
* **graph** *Graph*: a `graphology` instance. | ||
* **source** *any*: source node. | ||
* **target** *?any*: optionally, target node. | ||
- **graph** _Graph_: a `graphology` instance. | ||
- **source** _any_: source node. | ||
- **target** _?any_: optionally, target node. | ||
@@ -69,7 +67,7 @@ ### bidirectional | ||
*Arguments* | ||
_Arguments_ | ||
* **graph** *Graph*: a `graphology` instance. | ||
* **source** *any*: source node. | ||
* **target** *any*: target node. | ||
- **graph** _Graph_: a `graphology` instance. | ||
- **source** _any_: source node. | ||
- **target** _any_: target node. | ||
@@ -89,6 +87,6 @@ ### singleSource | ||
*Arguments* | ||
_Arguments_ | ||
* **graph** *Graph*: a `graphology` instance. | ||
* **source** *any*: source node. | ||
- **graph** _Graph_: a `graphology` instance. | ||
- **source** _any_: source node. | ||
@@ -108,6 +106,6 @@ ### singleSourceLength | ||
*Arguments* | ||
_Arguments_ | ||
* **graph** *Graph*: a `graphology` instance. | ||
* **source** *any*: source node. | ||
- **graph** _Graph_: a `graphology` instance. | ||
- **source** _any_: source node. | ||
@@ -127,6 +125,6 @@ ### undirectedSingleSourceLength | ||
*Arguments* | ||
_Arguments_ | ||
* **graph** *Graph*: a `graphology` instance. | ||
* **source** *any*: source node. | ||
- **graph** _Graph_: a `graphology` instance. | ||
- **source** _any_: source node. | ||
@@ -146,6 +144,6 @@ ### brandes | ||
*Arguments* | ||
_Arguments_ | ||
* **graph** *Graph*: a `graphology` instance. | ||
* **source** *any*: source node. | ||
- **graph** _Graph_: a `graphology` instance. | ||
- **source** _any_: source node. | ||
@@ -170,8 +168,8 @@ ## Dijkstra | ||
*Arguments* | ||
_Arguments_ | ||
* **graph** *Graph*: a `graphology` instance. | ||
* **source** *any*: source node. | ||
* **target** *any*: target node. | ||
* **weightAttribute** *?string* [`weight`]: name of the weight attribute. | ||
- **graph** _Graph_: a `graphology` instance. | ||
- **source** _any_: source node. | ||
- **target** _any_: target node. | ||
- **weightAttribute** _?string_ [`weight`]: name of the weight attribute. | ||
@@ -194,7 +192,7 @@ <h3 id="dijkstra-singlesource">singleSource</h3> | ||
*Arguments* | ||
_Arguments_ | ||
* **graph** *Graph*: a `graphology` instance. | ||
* **source** *any*: source node. | ||
* **weightAttribute** *?string* [`weight`]: name of the weight attribute. | ||
- **graph** _Graph_: a `graphology` instance. | ||
- **source** _any_: source node. | ||
- **weightAttribute** _?string_ [`weight`]: name of the weight attribute. | ||
@@ -217,6 +215,6 @@ <h3 id="dijkstra-brandes">brandes</h3> | ||
*Arguments* | ||
_Arguments_ | ||
* **graph** *Graph*: a `graphology` instance. | ||
* **source** *any*: source node. | ||
* **weightAttribute** *?string* [`weight`]: name of the weight attribute. | ||
- **graph** _Graph_: a `graphology` instance. | ||
- **source** _any_: source node. | ||
- **weightAttribute** _?string_ [`weight`]: name of the weight attribute. |
@@ -17,3 +17,7 @@ import Graph from 'graphology-types'; | ||
bidirectional(graph: Graph, source: unknown, target: unknown): ShortestPath | null; | ||
bidirectional( | ||
graph: Graph, | ||
source: unknown, | ||
target: unknown | ||
): ShortestPath | null; | ||
singleSource(graph: Graph, source: unknown): ShortestPathMapping; | ||
@@ -20,0 +24,0 @@ singleSourceLength(graph: Graph, source: unknown): ShortestPathLengthMapping; |
@@ -9,4 +9,4 @@ /** | ||
var isGraph = require('graphology-utils/is-graph'), | ||
Queue = require('mnemonist/queue'), | ||
extend = require('@yomguithereal/helpers/extend'); | ||
Queue = require('mnemonist/queue'), | ||
extend = require('@yomguithereal/helpers/extend'); | ||
@@ -27,9 +27,19 @@ /** | ||
if (arguments.length < 3) | ||
throw new Error('graphology-shortest-path: invalid number of arguments. Expecting at least 3.'); | ||
throw new Error( | ||
'graphology-shortest-path: invalid number of arguments. Expecting at least 3.' | ||
); | ||
if (!graph.hasNode(source)) | ||
throw new Error('graphology-shortest-path: the "' + source + '" source node does not exist in the given graph.'); | ||
throw new Error( | ||
'graphology-shortest-path: the "' + | ||
source + | ||
'" source node does not exist in the given graph.' | ||
); | ||
if (!graph.hasNode(target)) | ||
throw new Error('graphology-shortest-path: the "' + target + '" target node does not exist in the given graph.'); | ||
throw new Error( | ||
'graphology-shortest-path: the "' + | ||
target + | ||
'" target node does not exist in the given graph.' | ||
); | ||
@@ -46,6 +56,6 @@ source = '' + source; | ||
var getPredecessors = graph.inboundNeighbors.bind(graph), | ||
getSuccessors = graph.outboundNeighbors.bind(graph); | ||
getSuccessors = graph.outboundNeighbors.bind(graph); | ||
var predecessor = {}, | ||
successor = {}; | ||
successor = {}; | ||
@@ -58,16 +68,15 @@ // Predecessor & successor | ||
var forwardFringe = [source], | ||
reverseFringe = [target], | ||
currentFringe, | ||
node, | ||
neighbors, | ||
neighbor, | ||
i, | ||
j, | ||
l, | ||
m; | ||
reverseFringe = [target], | ||
currentFringe, | ||
node, | ||
neighbors, | ||
neighbor, | ||
i, | ||
j, | ||
l, | ||
m; | ||
var found = false; | ||
outer: | ||
while (forwardFringe.length && reverseFringe.length) { | ||
outer: while (forwardFringe.length && reverseFringe.length) { | ||
if (forwardFringe.length <= reverseFringe.length) { | ||
@@ -90,3 +99,2 @@ currentFringe = forwardFringe; | ||
if (neighbor in successor) { | ||
// Path is found! | ||
@@ -98,4 +106,3 @@ found = true; | ||
} | ||
} | ||
else { | ||
} else { | ||
currentFringe = reverseFringe; | ||
@@ -117,3 +124,2 @@ reverseFringe = []; | ||
if (neighbor in predecessor) { | ||
// Path is found! | ||
@@ -128,4 +134,3 @@ found = true; | ||
if (!found) | ||
return null; | ||
if (!found) return null; | ||
@@ -164,6 +169,12 @@ var path = []; | ||
if (arguments.length < 2) | ||
throw new Error('graphology-shortest-path: invalid number of arguments. Expecting at least 2.'); | ||
throw new Error( | ||
'graphology-shortest-path: invalid number of arguments. Expecting at least 2.' | ||
); | ||
if (!graph.hasNode(source)) | ||
throw new Error('graphology-shortest-path: the "' + source + '" source node does not exist in the given graph.'); | ||
throw new Error( | ||
'graphology-shortest-path: the "' + | ||
source + | ||
'" source node does not exist in the given graph.' | ||
); | ||
@@ -173,9 +184,9 @@ source = '' + source; | ||
var nextLevel = {}, | ||
paths = {}, | ||
currentLevel, | ||
neighbors, | ||
v, | ||
w, | ||
i, | ||
l; | ||
paths = {}, | ||
currentLevel, | ||
neighbors, | ||
v, | ||
w, | ||
i, | ||
l; | ||
@@ -222,3 +233,7 @@ nextLevel[source] = true; | ||
if (!graph.hasNode(source)) | ||
throw new Error('graphology-shortest-path: the "' + source + '" source node does not exist in the given graph.'); | ||
throw new Error( | ||
'graphology-shortest-path: the "' + | ||
source + | ||
'" source node does not exist in the given graph.' | ||
); | ||
@@ -231,3 +246,3 @@ source = '' + source; | ||
var lengths = {}, | ||
level = 0; | ||
level = 0; | ||
@@ -246,4 +261,3 @@ lengths[source] = 0; | ||
if (seen.has(node)) | ||
continue; | ||
if (seen.has(node)) continue; | ||
@@ -263,4 +277,10 @@ seen.add(node); | ||
var singleSourceLength = asbtractSingleSourceLength.bind(null, 'outboundNeighbors'); | ||
var undirectedSingleSourceLength = asbtractSingleSourceLength.bind(null, 'neighbors'); | ||
var singleSourceLength = asbtractSingleSourceLength.bind( | ||
null, | ||
'outboundNeighbors' | ||
); | ||
var undirectedSingleSourceLength = asbtractSingleSourceLength.bind( | ||
null, | ||
'neighbors' | ||
); | ||
@@ -277,4 +297,3 @@ /** | ||
function shortestPath(graph, source, target) { | ||
if (arguments.length < 3) | ||
return singleSource(graph, source); | ||
if (arguments.length < 3) return singleSource(graph, source); | ||
@@ -300,15 +319,15 @@ return bidirectional(graph, source, target); | ||
var S = [], | ||
P = {}, | ||
sigma = {}; | ||
P = {}, | ||
sigma = {}; | ||
var nodes = graph.nodes(), | ||
Dv, | ||
sigmav, | ||
neighbors, | ||
v, | ||
w, | ||
i, | ||
j, | ||
l, | ||
m; | ||
Dv, | ||
sigmav, | ||
neighbors, | ||
v, | ||
w, | ||
i, | ||
j, | ||
l, | ||
m; | ||
@@ -315,0 +334,0 @@ for (i = 0, l = nodes.length; i < l; i++) { |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
851
0
0
33928
210
+ Addedgraphology-indices@0.14.0(transitive)
- Removedgraphology-indices@0.13.4(transitive)
- Removedgraphology-utils@1.8.0(transitive)
Updatedgraphology-indices@^0.14.0
Updatedgraphology-utils@^2.1.2