Shortest paths: the relaxation framework
◈ 4 cardsINITIALIZE-SINGLE-SOURCE and RELAX — the one primitive behind every shortest-path algorithm, plus the δ cases and key properties.
The single-source shortest-paths problem
Given a weighted directed graph and a source , find a minimum-weight path from to every vertex. The shortest-path weight is The third case matters: if you can loop through a negative cycle on the way to , you can make the path arbitrarily cheap, so is .
The two primitives
Every algorithm in this and the next two lessons is built from exactly two operations. Each vertex carries v.d (a shortest-path estimate — an upper bound on ) and v.π (its predecessor).
INITIALIZE-SINGLE-SOURCE(G, s): set v.d = ∞, v.π = NIL for every vertex, then s.d = 0.
RELAX(u, v, w): can we reach more cheaply by going through ?
That is the whole toolkit. Bellman-Ford, DAG-shortest-paths, and Dijkstra differ only in the order and number of times they relax edges.
Worked example of relaxation
Suppose u.d = 5, w(u,v) = 2, and v.d = 10. Then u.d + w(u,v) = 7 < 10, so RELAX sets v.d = 7 and v.π = u. If instead v.d were already 6, then and RELAX does nothing — estimates only ever decrease.
The properties that make it all work
- Upper-bound:
v.d ≥ δ(s,v)always, and oncev.d = δ(s,v)it never changes again. - Triangle inequality: .
- Convergence: if is a shortest path and
u.d = δ(s,u)beforeRELAX(u,v,w), thenv.d = δ(s,v)after it. - Path-relaxation: if you relax the edges of a shortest path in order — even with arbitrary other relaxations mixed in between — then
v_k.d = δ(s,v_k).
The path-relaxation property is the linchpin of Bellman-Ford's correctness: after passes over all edges, every shortest path of edges has its correct value.