-
Notifications
You must be signed in to change notification settings - Fork 1
/
dijkstras.ts
60 lines (52 loc) · 1.38 KB
/
dijkstras.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
interface IGraphNode {
[key: string]: number;
}
interface ISimpleGraph {
[key: string]: IGraphNode;
}
export function shortPath(graph: ISimpleGraph, start: string, end: string) {
const costs = {};
const processed = [];
let neighbors = {};
Object.keys(graph).forEach((node) => {
if (node !== start) {
let value = graph[start][node];
costs[node] = value || Infinity;
}
});
let node = findNodeLowestCost(costs, processed);
while (node) {
const cost = costs[node];
neighbors = graph[node];
Object.keys(neighbors).forEach((neighbor) => {
let newCost = cost + neighbors[neighbor];
if (newCost < costs[neighbor]) {
costs[neighbor] = newCost;
}
});
processed.push(node);
node = findNodeLowestCost(costs, processed);
}
return costs;
}
function findNodeLowestCost(costs: IGraphNode, processed: string[]) {
let lowestCost = Infinity;
let lowestNode: string;
Object.keys(costs).forEach((node) => {
let cost = costs[node];
if (cost < lowestCost && !processed.includes(node)) {
lowestCost = cost;
lowestNode = node;
}
});
return lowestNode;
}
// const graph: ISimpleGraph = {};
// graph.a = { b: 2, c: 1 };
// graph.b = { f: 7 };
// graph.c = { d: 5, e: 2 };
// graph.d = { f: 2 };
// graph.e = { f: 1 };
// graph.f = { g: 1 };
// graph.g = {};
// console.log("first", shortPath(graph, "a", "g"));