Search for a command to run...
Graph playground
Edit undirected edges and replay the algorithm from a new start node.
Initialize tentative distances
Set node 0 to distance 0 and every other node to infinity.
Dijkstra's Algorithm
1function dijkstra(graph, start) {2 const distances = new Map([[start, 0]]);3 const queue = [{ node: start, distance: 0 }];//start: 0target: 5distances: { 0: 0 }4 5 while (queue.length > 0) {6 const current = popNearest(queue);7 for (const edge of graph[current.node]) {8 const nextDistance = current.distance + edge.weight;9 if (nextDistance < (distances.get(edge.to) ?? Infinity)) {10 distances.set(edge.to, nextDistance);11 queue.push({ node: edge.to, distance: nextDistance });12 }13 }14 }15}