All-Pairs Shortest Paths and Maximum Flow
Algorithms · Advanced Placement
All-Pairs Shortest Paths and Maximum Flow: the big picture
Finding shortest paths between every pair of vertices can be done by running a single-source algorithm from each vertex, but a dynamic programming formulation is more direct for dense graphs. The Floyd–Warshall algorithm considers each vertex in turn as a possible intermediate point and asks whether routing through it improves any pair's distance, filling a matrix in triply nested loops for a cubic bound that is independent of the edge count, and it handles negative weights provided there is no negative cycle. Johnson's algorithm reweights the graph using Bellman–Ford so that all weights become non-negative while preserving shortest paths, then runs Dijkstra from each vertex, which is better for sparse graphs. The maximum flow problem asks for the greatest rate at which a commodity can be sent from a source to a sink through a network with capacity limits on each edge. The Ford–Fulkerson method repeatedly finds an augmenting path in the residual network and pushes flow along it; the Edmonds–Karp refinement chooses the shortest augmenting path and gives a polynomial bound. The max-flow min-cut theorem states that the maximum flow equals the minimum capacity of a cut separating source from sink, which gives both a certificate of optimality and a reduction from bipartite matching.
Floyd–Warshall, Johnson's Algorithm & Transitive Closure
Floyd–Warshall is one of the shortest useful algorithms ever written: three nested loops asking, for each possible intermediate vertex, whether routing through it shortens any pair's distance. Its cubic cost is independent of the edge count, which makes it the right choice for dense graphs and a poor one for sparse. Johnson's algorithm is the sparse-graph answer — reweight with Bellman–Ford so every weight is non-negative, then run Dijkstra from every vertex.
{{illustration:algos-l17-f1}}
{{illustration:algos-l17-f2}}
Key relations
- Floyd–Warshall's recurrence. d^((k))ᵢⱼ = min(d^((k−1))ᵢⱼ, d^((k−1))ᵢₖ + d^((k−1))ₖⱼ)
Maximum Flow, Augmenting Paths & Max-Flow Min-Cut
Maximum flow asks how much can be pushed from a source to a sink given capacity limits, and the method is to repeatedly find a path with spare capacity and push flow along it. The subtlety is the residual network, which includes backward edges allowing earlier decisions to be undone — without them the greedy approach gets stuck. The max-flow min-cut theorem is the beautiful part: the maximum flow exactly equals the smallest total capacity of any cut separating source from sink, which gives you a certificate that your answer is optimal.
{{illustration:algos-l17-f3}}
{{illustration:algos-l17-f4}}
Key relations
- Max-flow min-cut. |f|ₘₐₓ = min_((S,T)) c(S,T) ; c_f(u,v) = c(u,v) − f(u,v)