Connectivity, flow and routes · 31 / 53
Max-flow, residual graphs and matching
Understand → trace → implement → justify
3 min reading · allow extra time for practiceFlow obeys 0≤f≤capacity and conservation outside source/sink. Residual edges represent remaining forward capacity and the ability to undo prior flow. Reverse edges model rerouting, not physical extra pipes.
Ford–Fulkerson augments a path by its bottleneck. Integer capacities ensure termination, while general irrational capacities with poor path choice can cause problems. Edmonds–Karp uses shortest residual paths by BFS, bounding time by O(VE²). Dinic uses level graphs and blocking flows.
Max-flow=min-cut supplies a certificate using residual reachability. Bipartite matching reduces to unit-capacity source-left-right-sink flow; Hopcroft–Karp runs in O(E√V). Min-cost flow is distinct: maximum volume need not be cheapest.
Why reverse edges are essential
A flow decision may need revision. Sending f units along u→v decreases forward residual capacity and increases reverse v→u capacity. The reverse edge is permission to undo an earlier choice, not a new physical pipe. Augment by the path’s minimum residual capacity and update every edge.
Flow conservation holds away from source and sink, with 0≤f≤c on each edge. Parallel edges and undirected capacities need explicit representation; one matrix cell can conflate real and reverse-edge semantics.
The max-flow/min-cut theorem connects optimal flow with a cut. When no augmenting path remains, vertices reachable from the source in the residual graph define a cut whose capacity equals the flow. Edmonds-Karp chooses paths with BFS and has O(VE²) complexity based on path-length behaviour. Arbitrary Ford-Fulkerson paths can fail to terminate under irrational-capacity models.
Foundation: Verify conservation after every augmentation. Intermediate: Construct an improvement requiring a reverse edge. Advanced: Reduce bipartite matching to unit-capacity flow and return matching witnesses and min-cut certificates separately.
A worked trace
s→a3,a→t2,s→t1 gives bottleneck2 on s-a-t and1 directly. Cut {s,a} has capacity3, certifying maximality.
Cost and assumptions
Edmonds–Karp O(VE²); integer Ford–Fulkerson has a simple O(E·|f*|) bound, not polynomial in capacity bit length.
Executable example
Run with Python 3. Assertions check a small example; they do not constitute a general correctness proof. Some advanced examples are explicitly small models or reference oracles, not full production implementations.
# A residual augmentation along s -> a -> t.
r = {("s","a"):3, ("a","t"):2, ("a","s"):0, ("t","a"):0}
path = [("s","a"),("a","t")]
bottleneck = min(r[e] for e in path)
for u,v in path:
r[u,v] -= bottleneck
r[v,u] += bottleneck
assert r["a","s"] == 2 and r["a","t"] == 0TEST YOUR UNDERSTANDING
Your turn
What happens if reverse residual edges are omitted?
Reveal the reasoning
You cannot undo earlier poor choices and may stop below maximum flow. Both residual directions must be updated.
Take it further
- Write down the input contract and the invariant before changing the example.
- Add empty, boundary, duplicate and adversarial cases where they are relevant. Compare with a simple oracle.
- Derive the cost under the stated model; measure several input sizes and explain discrepancies.
Primary reading & lecture notes
Original explanations in this course; these references provide proofs and further reading. Source links may cover the broader module.
Jeff Erickson · Algorithms ↗MIT 6.046J · Design and Analysis ↗