Part III · trunk B · combinatorial algorithms · spine
Max-flow min-cut, three algorithms and why each exists, costs and potentials, and the skill that matters most: recognizing a flow in disguise.
needs 13~2 h slides + 3 h lablab: uv run co lab 14
14 · overview
The duality, augmenting paths, Edmonds–Karp, Dinic, push–relabel, and today's frontier.
Min-cost flow, negative cycles, successive shortest paths with potentials.
Matching, project selection, baseball elimination, segmentation: flows in disguise.
Words with a dotted underline are glossary terms: click one for its definition. G lists every term in the unit, N shows the notes. Optional extra: interactive figures, linked from the slides they support by a small ▸ chip; skip them freely.
section 1
The first min–max theorem of combinatorial optimization.
14 · max flow
Lab step 1's textbook network. Max flow 5. The cut $S = \{s, a, b\}$ has capacity $3 + 2 = 5$, which proves it.
A flow on a network respects capacities ($0 \le f \le c$) and conservation at every node except $s$ and $t$. Its value is the net flow out of $s$.
An $s$–$t$ cut is a set $S \ni s$, $t \notin S$. Its capacity is the total capacity of arcs leaving $S$.
Every flow's value is at most every cut's capacity.
Why: value = (flow out of $S$) − (flow into $S$) ≤ capacity out of $S$.
On the figure: the cut $\{s\}$ has capacity 5, $\{s, a\}$ has $2 + 1 + 3 = 6$, $\{s, a, b\}$ has 5. Any flow is at most 5.
14 · max flow
The maximum flow value equals the minimum cut capacity. With integer capacities there is an integral maximum flow.
The residual graph has $u \to v$ with capacity $c - f$ (room to send more) and $v \to u$ with capacity $f$ (room to undo). If $t$ is reachable from $s$, an augmenting path improves the flow. If not, the reachable set $S$ has every leaving arc saturated and every entering arc empty, so the flow equals $S$'s capacity. The next slide does it on the network.
The proof is the algorithm, and it produces the certificate:
min_cut, and the test checks its capacity equals your flow.It is LP duality (unit 03) for the flow LP, whose dual is the cut LP, with integrality from total unimodularity (unit 06).
14 · max flow · example
Residual graph of the network after the first augmentation, 3 units on $s \to a \to t$. Arcs $s \to a$ and $a \to t$ are gone (saturated); their reverses appear.
| step | BFS finds | bottleneck | value |
|---|---|---|---|
| 1 | $s \to a \to t$ | $\min(3, 3) = 3$ | 3 |
| 2 | $s \to b \to t$ | $\min(2, 2) = 2$ | 5 |
| 3 | no path | 5 |
After step 2, from $s$ the residual graph reaches: $s$ itself; not $a$ ($s \to a$ saturated); not $b$ ($s \to b$ saturated). So $S = \{s\}$, and its leaving arcs $s \to a$, $s \to b$ carry $3 + 2 = 5$ = the flow.
The earlier cut $\{s, a, b\}$ is another minimum cut, also 5. The algorithm returns the one on the source side.
14 · max flow
With bad path choices, integer capacities can take time proportional to the flow value, so it is not polynomial. With irrational capacities it may never terminate, and may even converge to a non-maximum value (Zwick's golden-ratio example).
Shortest augmenting paths by BFS: $O(VE^2)$. The idea: distances from $s$ never shrink, so each arc can be the bottleneck only $O(V)$ times.
full argument in the notes (N)
Group all shortest paths of the same length into one phase: BFS levels, then a blocking flow in the level graph by DFS with current-edge pointers. $O(V)$ phases, $O(VE)$ each, so $O(V^2 E)$ overall.
On unit-capacity networks, $O(E\sqrt V)$. On bipartite matching networks this is Hopcroft–Karp (unit 15).
In lab then, on a 40×40 layered network, Edmonds–Karp took 0.24 s and Dinic 0.057 s.
14 · max flow
Push–relabel, Goldberg & Tarjan (1988). No augmenting paths. Maintain a preflow (nodes may have excess) and heights:
$O(V^2 E)$ generic, $O(V^3)$ FIFO, $O(V^2\sqrt E)$ with the highest-label rule.
Lab then, seconds, plain FIFO without heuristics:
| family | Dinic | push–relabel | OR-Tools |
|---|---|---|---|
| layered 40×40 | 0.057 | 3.706 | 0.003 |
| random n = 1500 | 0.036 | 0.027 | 0.004 |
| bipartite 300×300 | 0.006 | 0.007 | 0.001 |
Production push–relabel adds the gap heuristic (if no node has height $k$, lift everything above $k$ at once) and periodic global relabelling (exact heights by a backward BFS from $t$). Without them it climbs deep layered networks one height at a time.
14 · max flow · example
BFS levels on the network: $s$ 0, $a$ 1, $b$ 1, $t$ 2. The level graph keeps only arcs going one level up: $s \to a$, $s \to b$, $a \to t$, $b \to t$. Arc $a \to b$ joins two level-1 nodes and is dropped.
Blocking flow: DFS sends 3 on $s \to a \to t$, then 2 on $s \to b \to t$. Now every $s$–$t$ path in the level graph has a saturated arc. The next BFS can't reach $t$: done, value 5, in one phase.
On a deep layered network, phases take turns: each one raises the $s$–$t$ distance by at least 1.
FIFO push–relabel on the network, heights $h$ and excesses in order $(s, a, b, t)$:
| step | $h$ | excess |
|---|---|---|
| saturate $s \to a$, $s \to b$ | 4, 0, 0, 0 | −5, 3, 2, 0 |
| relabel $a$ (can't push downhill) | 4, 1, 0, 0 | −5, 3, 2, 0 |
| push 1 $a \to b$ | 4, 1, 0, 0 | −5, 2, 3, 0 |
| push 2 $a \to t$ | 4, 1, 0, 0 | −5, 0, 3, 2 |
| relabel $b$ | 4, 1, 1, 0 | −5, 0, 3, 2 |
| push 2 $b \to t$ | 4, 1, 1, 0 | −5, 0, 1, 4 |
| relabel $b$ ($b \to t$ full; lowest neighbour $a$) | 4, 1, 2, 0 | −5, 0, 1, 4 |
| push 1 $b \to a$: undo | 4, 1, 2, 0 | −5, 1, 0, 4 |
| push 1 $a \to t$ | 4, 1, 2, 0 | −5, 0, 0, 5 |
No excess left: a maximum flow of 5, the same flow as Edmonds–Karp.
14 · max flow
It solves the min-cost flow LP with an interior-point method (unit 04) whose linear systems are solved by specialized graph data structures. Continuous optimization produced the fastest known combinatorial flow algorithm.
Nobody runs it in practice yet: its constants are enormous. Push–relabel and network simplex remain what solvers ship.
section 2
The cheapest way to send a given amount.
14 · costs
Send $d$ units from $s$ to $t$ minimizing $\sum_e \text{cost}_e f_e$, subject to capacities.
A feasible flow is minimum-cost iff its residual graph has no negative-cost cycle.
Algorithms: cycle canceling (Klein 1967: push around negative cycles); network simplex (unit 02, specialized to trees); successive shortest paths (the lab).
Reverse arcs carry cost $-c$, but reduced costs stay nonnegative, so Dijkstra stays valid. The potentials are exactly the dual variables of the min-cost flow LP.
Strongly polynomial algorithms exist (Tardos 1985; Orlin 1993).
14 · costs · example
| arc | capacity | cost |
|---|---|---|
| $s \to a$ | 2 | 1 |
| $s \to b$ | 2 | 4 |
| $a \to b$ | 1 | 1 |
| $a \to t$ | 1 | 5 |
| $b \to t$ | 3 | 1 |
Send 3 units from $s$ to $t$ as cheaply as possible. The routes: $s\,a\,b\,t$ costs 3 per unit (room 1), $s\,b\,t$ costs 5 (room 2), $s\,a\,t$ costs 6 (room 1).
Start. Bellman–Ford from $s$: $\pi = (0, 1, 2, 3)$ for $(s, a, b, t)$. Reduced costs $c + \pi(u) - \pi(v)$: $sa$ 0, $ab$ 0, $bt$ 0, $sb$ 2, $at$ 3. All $\ge 0$.
Round 1. Dijkstra finds $s\,a\,b\,t$ at reduced length 0, i.e. true cost 3. Send its bottleneck, 1 unit ($a \to b$ fills). Cost so far 3.
Round 2. Distances $(0, 0, 2, 2)$, new $\pi = (0, 1, 4, 5)$. Now $sb$ and $bt$ have reduced cost 0, $at$ has 1: path $s\,b\,t$, true cost 5, send 2. Total $3 + 10 = 13$.
Each round takes the cheapest remaining route, exactly as the table of routes suggests, and Dijkstra never meets a negative arc.
section 3
Flows in disguise, which is the skill the checkpoint asks for.
14 · reductions
Two projects both need tool $q$. Min cut: cut $q \to t$ (6), with source side $\{s, p_1, p_2, q\}$. Best profit $= 13 - 6 = 7$.
Project selection: choose projects, each with a profit (possibly negative), closed under "requires".
A finite cut can't cut an $\infty$ arc, so its source side is a closed set. Its capacity is (profit forgone) + (costs paid), so
$$\text{best profit} = \textstyle\sum \text{positive profits} - \text{min cut}.$$Picard (1976): maximum-weight closure. Lab step 5.
14 · reductions
$s$ → left (capacity 1), left → right along edges (1), right → $t$ (1). Max flow = max matching, and the min cut gives König's vertex cover. Unit 15 does it faster.
Can team $x$ still finish first? Give $x$ all its remaining wins. For each remaining game between other teams, a node with capacity = games left, feeding into the two teams; each team → $t$ with capacity (wins $x$ can reach − wins so far). $x$ is still alive iff the max flow saturates every game arc.
Pixels as nodes; $s$/$t$ arcs weighted by how foreground/background each pixel looks; neighbour arcs weighted by similarity. The minimum cut is the best labelling (Greig, Porteous & Seheult 1989; graph cuts in vision).
Jobs → time slots within their windows, with capacities for processing time and machine count. A flow saturating all jobs is a feasible schedule.
14 · lab
uv run co test 14
uv run co then 14 # three families vs OR-Tools and networkx · min-cost flow vs OR-Tools
14 · checkpoint
Korte & Vygen, ch. 8–9.
Kleinberg & Tardos, Algorithm Design, ch. 7.
Goldberg & Tarjan, JACM 35 (1988) (optional).
15 matching: Hopcroft–Karp, the Hungarian algorithm, blossoms · 18 uses this unit's ideas for alldifferent filtering.