How does Dijkstra works?
Import
Export
1 function Dijkstra(Graph, source): 2 3 create vertex set Q 4 5 for each vertex v in Graph: // Initialization 6← INFINITY // Unknown distance from source to v 7 ← UNDEFINED // Previous node in optimal path from source 8 add v to // All nodes initially in Q (unvisited nodes) 9 10 ← 0 // Distance from source to source 11 12 while is not empty: 13 ← vertex in Q with min // Source node will be selected first 14 remove from 15 16 for each neighbor of : // where v is still in Q. 17 ← + 18 if < dist[v]: // A shorter path to v has been found 19 ← 20 ← 21 22 return dist[], prev[]
1 function Dijkstra(Graph, source): 2 3 create vertex set Q 4 5 for each vertex v in Graph: 6← INFINITY 7 ← UNDEFINED 8 add v to 9 10 ← 0 11 12 while is not empty: 13 ← vertex in Q with min 14 remove from 15 16 for each neighbor of : 17 ← + 18 if < dist[v]: 19 ← 20 ← 21 22 return dist[], prev[]
vertex | predecessor | distance from start |
---|---|---|
{{p.name}} | {{p.prev}} | {{p.dist}} |