This is the main part of the algorithm. An unvisited
node with the shortest distance from the source node is selected. All the neighboring
nodes from that node are then checked, and distances are updated. Nodes are added to the path list whenever the distance is updated. This process is repeated until all the nodes in the graph are visited
.
xxxxxxxxxx
53
}
const graph = {
a: { b: 5, c: 2 },
b: { a: 5, c: 7, d: 8 },
c: { a: 2, b: 7, d: 4, e: 8 },
d: { b: 8, c: 4, e: 6, f: 4 },
e: { c: 8, d: 6, f: 3 },
f: { e: 3, d: 4 },
};
​
const formatGraph = (g) => {
const tmp = {};
Object.keys(g).forEach((k) => {
const obj = g[k];
const arr = [];
Object.keys(obj).forEach((v) => arr.push({ vertex: v, cost: obj[v] }));
tmp[k] = arr;
});
return tmp;
};
​
const dijkstra = (graph, start, end) => {
var map = formatGraph(graph);
​
var visited = [];
var unvisited = [start];
var shortestDistances = { [start]: { vertex: start, cost: 0 } };
​
var vertex;
while ((vertex = unvisited.shift())) {
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment