forked from perliedman/geojson-path-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.js
30 lines (26 loc) · 857 Bytes
/
dijkstra.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var Queue = require('tinyqueue');
module.exports = function(graph, start, end) {
var costs = {};
costs[start] = 0;
var initialState = [0, [start], start];
var queue = new Queue([initialState], function(a, b) { return a[0] - b[0]; });
var explored = {};
while (queue.length) {
var state = queue.pop();
var cost = state[0];
var node = state[2];
if (node === end) {
return state.slice(0, 2);
}
var neighbours = graph[node];
Object.keys(neighbours).forEach(function(n) {
var newCost = cost + neighbours[n];
if (!(n in costs) || newCost < costs[n]) {
costs[n] = newCost;
var newState = [newCost, state[1].concat([n]), n];
queue.push(newState);
}
});
}
return null;
}