Skip to content

Commit 6d68d93

Browse files
committed
add dijkstra
1 parent 216fba0 commit 6d68d93

4 files changed

+267
-0
lines changed

dijkstras-algorithm-shortest-path.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const graph = {
2+
start: { A: 5, B: 2 },
3+
A: { C: 4, D: 2, start: 5, B: 8, },
4+
B: { A: 8, D: 7, start: 2, },
5+
C: { D: 6, finish: 3, A: 4 },
6+
D: { finish: 1, C: 6, A: 2, B: 7 },
7+
finish: { C: 3, D: 1 },
8+
};
9+
10+
// const graph = {
11+
// start: { A: 5, B: 2 },
12+
// A: { B: 1, finish: 2 },
13+
// B: { A: 1, finish: 8 },
14+
// finish: {},
15+
// };
16+
17+
// const graph = {
18+
// A: { B: 5, C: 2 },
19+
// B: { C: 1, D: 4, A: 5 },
20+
// C: { B: 1, D: 6, A: 2},
21+
// D: { B: 4, C: 6}
22+
// };
23+
24+
const lowestCostNode = (costs, processed) => {
25+
let lowest = null;
26+
27+
for (let key in costs) {
28+
if (processed.includes(key)) {
29+
continue;
30+
}
31+
32+
if (lowest === null || costs[key] < costs[lowest]) {
33+
lowest = key;
34+
}
35+
}
36+
37+
return lowest;
38+
};
39+
40+
const dijkstra = (graph, start, end) => {
41+
const costs = {
42+
finish: Infinity,
43+
...graph[start],
44+
};
45+
46+
const parents = {
47+
[end]: null,
48+
};
49+
50+
for (let child in graph[start]) {
51+
parents[child] = start;
52+
}
53+
54+
const processed = [];
55+
56+
let node = lowestCostNode(costs, processed);
57+
58+
while (node) {
59+
let cost = costs[node];
60+
61+
let children = graph[node];
62+
63+
for (let n in children) {
64+
let newCost = cost + children[n];
65+
66+
if (!costs[n] || newCost < costs[n]) {
67+
costs[n] = newCost;
68+
parents[n] = node;
69+
}
70+
}
71+
72+
processed.push(node);
73+
74+
node = lowestCostNode(costs, processed);
75+
}
76+
77+
let optimalPath = [end];
78+
79+
let parent = parents[end];
80+
81+
while (parent) {
82+
optimalPath.unshift(parent);
83+
84+
if (parent === start) {
85+
break;
86+
}
87+
88+
parent = parents[parent];
89+
}
90+
91+
const results = {
92+
distance: costs[end],
93+
path: optimalPath,
94+
};
95+
96+
return results;
97+
};
98+
99+
console.log(dijkstra(graph, 'start', 'finish'));
100+
// console.log(dijkstra(graph, 'A', 'D'));

package-lock.json

+82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@
2222
"@babel/register": "^7.7.0",
2323
"chai": "^4.2.0",
2424
"mocha": "^6.1.4"
25+
},
26+
"devDependencies": {
27+
"@babel/node": "^7.10.1"
2528
}
2629
}

shortest-path-dijkstra-algorith.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const graph = {
2+
start: { A: 5, B: 2 },
3+
A: { C: 4, D: 2, start: 5, B: 8, },
4+
B: { A: 8, D: 7, start: 2, },
5+
C: { D: 6, finish: 3, A: 4 },
6+
D: { finish: 1, C: 6, A: 2, B: 7 },
7+
finish: { C: 3, D: 1 },
8+
};
9+
10+
const getLowestCostNode = (costs, visited) => {
11+
let lowest = null;
12+
13+
for (const key in costs) {
14+
if (!visited[key] && (lowest === null || costs[key] < costs[lowest])) {
15+
lowest = key;
16+
}
17+
}
18+
19+
return lowest;
20+
};
21+
22+
const shortestPath = (graph, start, finish) => {
23+
24+
// START OR FINISH DOES NOT EXISTS
25+
if (graph[start] === undefined || graph[finish] === undefined) {
26+
return;
27+
}
28+
29+
const costs = {
30+
[finish]: Infinity,
31+
...graph[start],
32+
};
33+
34+
const parents = {
35+
[finish]: null,
36+
};
37+
38+
for (const child in graph[start]) {
39+
parents[child] = start;
40+
}
41+
42+
const visited = { [start]: true, [finish]: true };
43+
44+
let node = getLowestCostNode(costs, visited);
45+
46+
while (node !== null) {
47+
let currentCost = costs[node];
48+
const children = graph[node];
49+
50+
for (const key in children) {
51+
let newCost = currentCost + children[key];
52+
if (costs[key] === undefined || newCost < costs[key]) {
53+
costs[key] = newCost;
54+
parents[key] = node;
55+
}
56+
}
57+
58+
visited[node] = true;
59+
node = getLowestCostNode(costs, visited);
60+
}
61+
62+
const path = [];
63+
let child = finish;
64+
65+
while (child) {
66+
path.unshift(child);
67+
68+
if (child === start) {
69+
break;
70+
}
71+
child = parents[child];
72+
}
73+
74+
return {
75+
distance: costs[finish],
76+
path,
77+
};
78+
};
79+
80+
81+
console.log(shortestPath(graph, 'start', 'finish'));
82+
// console.log(dijkstra(graph, 'A', 'D'));

0 commit comments

Comments
 (0)