Dijkstra's Shortest-Path Algorithm
Dijkstra's algorithm computes shortest-path distances from a single source to all other vertices in a weighted graph with nonnegative edge weights. It repeatedly extracts the unsettled vertex with the smallest tentative distance from a min-heap priority queue, then relaxes its outgoing edges. With predecessor pointers, it also reconstructs the actual shortest path.
Tutorial
The Algorithm
Dijkstra's shortest-path algorithm computes the shortest distance from a source vertex to every other vertex in a weighted graph with nonnegative edge weights.
The algorithm maintains a tentative distance for every vertex and a set of settled vertices whose distances are finalized.
Algorithm:
- Initialize and for all .
- While an unsettled vertex remains:
- Extract the unsettled vertex with the smallest and mark it settled.
- For each edge of weight , relax: if , set .
The extract-min step is implemented with a min-heap keyed on , giving per extraction.
Tiny trace. Consider three vertices with edges , , , starting from :
Final distances from : .