forked from perliedman/geojson-path-finder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
isochrone.js
38 lines (30 loc) · 1.04 KB
/
isochrone.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
31
32
33
34
35
36
37
38
let Queue = require('tinyqueue');
if (typeof Queue !== 'function' && Queue.default && typeof Queue.default === 'function') {
Queue = Queue.default;
}
module.exports = function(graph, start, max_cost) {
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];
explored[node] = 1;
var neighbours = graph[node];
if (!neighbours) continue;
Object.keys(neighbours).forEach(function(n) {
var newCost = cost + neighbours[n];
if (newCost < max_cost && (!(n in costs) || newCost < costs[n])) {
costs[n] = newCost;
if (!explored[n]) {
var newState = [newCost, state[1].concat([n]), n];
queue.push(newState);
}
}
});
}
return costs;
};