-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.js
More file actions
68 lines (58 loc) · 1.76 KB
/
Dijkstra.js
File metadata and controls
68 lines (58 loc) · 1.76 KB
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
61
62
63
64
65
66
67
68
// Dijkstra's Shortest Path Algorithm
// Dijkstra's shortest path algorithm is an algorithm for
// finding the shortest path between nodes in a graph
/*
1. Assign a tentative distance value to every node.
Set the initial node as the current node, and mark its
tentative distance as zero.
2. For the current node, consider all of its neighbors and
calculate their tentative distances. Compare the newly
calculated tentative distance to the current assigned
value and assign the smaller one.
3. When we're done considering all of the neighbors of the
current node, nark the current node as visited.
A visited node will never be checked again.
4. Select the unvisited node that is marked with the smallest
tentative distance, and set it as the new "current node"
then go back to step 2.
*/
function dijkstra(graph, start) {
const visited = new Set();
const distances = new Map();
const prev = new Map();
for (const node in graph) {
distances.set(node, Infinity);
prev.set(node, null);
}
distances.set(start, 0);
while (visited.size !== Object.keys(graph).length) {
let currNode = null;
let currDist = Infinity;
for (const [node, dist] of distances) {
if (!visited.has(node) && dist < currDist) {
currNode = node;
currDist = dist;
}
}
for (const [neighbor, weight] of graph[currNode]) {
const dist = currDist + weight;
if (dist < distances.get(neighbor)) {
distances.set(neighbor, dist);
prev.set(neighbor, currNode);
}
}
visited.add(currNode);
}
return { distances, prev };
}
const graph = {
A: { B: 2, C: 3 },
B: { D: 4, E: 5 },
C: { F: 6 },
D: { G: 7 },
E: { G: 8, H: 9 },
F: { H: 10 },
G: {},
H: {},
};
console.log(dijkstra(graph, "A"));