Dijkstra's Algorithm
1Introduction
This lecture explains how Dijkstra's algorithm computes single-source shortest-path distances in a graph with nonnegative edge weights. It defines tentative distances, settled vertices, and relaxation, then proves correctness through a loop invariant and a cut argument.
2Problem setting
Let be a finite directed or undirected graph, let be the source, and let be the weight function. The length of a path is the sum of its edge weights. Write for the shortest-path distance from to , with when is unreachable from .
data/lecture/information/algorithm/graph/shortest-path-basics.lecture.n.md3Terminology
- The tentative distance is the minimum length among the currently discovered paths from to . It is if no such path has been discovered. Consequently, always holds.
- The settled set consists of vertices for which has been proved. Such a vertex is called settled.
- To relax an edge is to assign whenever .
- A priority queue extracts a vertex with minimum among unsettled candidates.
4Algorithm
Initialize , for , and . Insert into a priority queue. Repeat the following steps until the queue is empty.
- Extract a pair with minimum distance key.
- If or , discard this obsolete pair.
- Otherwise, add to and relax every outgoing edge for which . Whenever decreases, insert the new pair .
An implementation that leaves old distance keys in the queue can contain several entries for one vertex. Such an obsolete pair is a stale entry. The test in step 2 permits a correct heap implementation without a decrease-key operation.
5Loop invariant
At the beginning of every iteration, the following statements hold.
- For every , .
- For every , is the minimum length of an -to- path whose vertices other than belong to . When , this is equivalent to requiring that its internal vertices lie in and its final edge cross from to . If no such path exists, then .
Initially, only the length-zero path to is a candidate, so the invariant holds. Relaxing every outgoing edge of a newly settled vertex records exactly the new candidate paths made available by using vertices of internally. Hence statement 2 is preserved.
6Correctness of settling
Let be an unsettled vertex with minimum . If , then . Otherwise, suppose that is reachable, and choose a shortest path from to . Let be the first edge of that leaves . Reasoning across this boundary is the cut argument.
Let denote the length of the prefix of from to . Because , the invariant and the relaxation of imply
All edge weights are nonnegative, so adding the suffix of after cannot decrease its length. Thus
The choice of gives , while a tentative distance is the length of an actual path and therefore . Consequently,
which proves . Settling therefore preserves statement 1 of the invariant.
7Unreachable vertices
When the queue becomes empty, every vertex reachable from has been settled. An unreachable vertex is never inserted, so its value remains . There is no need to extract or settle candidates whose keys are infinite.
8Why negative edges are forbidden
Consider edges of weight 2, of weight 5, and of weight -4. Dijkstra's algorithm settles with before with . However, the path has length 1, so the true distance is . A negative edge can decrease a path length after the path crosses the cut, invalidating the inequality in the proof that depends on nonnegativity.
9Complexity
With adjacency lists and a binary heap, each directed edge is relaxed at most once. An undirected edge occurs in the adjacency lists of both endpoints and is examined at most once in each direction. Each successful relaxation inserts at most one pair. The running time is
which is commonly written as when every vertex is reachable from the source. An implementation that retains stale entries uses auxiliary space. An implementation with decrease-key can keep the queue size at .
10Summary
- Dijkstra's algorithm requires every edge weight to be nonnegative.
- The loop invariant and cut argument justify settling a vertex with minimum tentative distance.
- Stale entries, unreachable vertices, and the supported heap operations must be stated as part of the implementation contract.