Part III · trunk B · combinatorial algorithms · spine

14

Network flows

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 route

1 · max flow, min cut

The duality, augmenting paths, Edmonds–Karp, Dinic, push–relabel, and today's frontier.

2 · costs

Min-cost flow, negative cycles, successive shortest paths with potentials.

3 · reductions

Matching, project selection, baseball elimination, segmentation: flows in disguise.

getting around

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

Max flow and min cut

The first min–max theorem of combinatorial optimization.

14 · max flow

Flows, cuts, and weak duality

s a b t 32 1 32 source side S

Lab step 1's textbook network. Max flow 5. The cut $S = \{s, a, b\}$ has capacity $3 + 2 = 5$, which proves it.

definitions

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$.

weak duality

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

Max-flow min-cut

Ford & Fulkerson 1956

The maximum flow value equals the minimum cut capacity. With integer capacities there is an integral maximum flow.

why, via the residual graph

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:

  1. While an augmenting path exists, push its bottleneck (the Ford–Fulkerson method).
  2. At the end, the reachable set is a minimum cut. That is lab step 1's 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

Augmenting, on the example

s a b t 33 212 dashed: backward arcs, the flow that can be undone

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.

stepBFS findsbottleneckvalue
1$s \to a \to t$$\min(3, 3) = 3$3
2$s \to b \to t$$\min(2, 2) = 2$5
3no path5
reading off the certificate

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

Choosing paths: Ford–Fulkerson to Edmonds–Karp to Dinic

any augmenting path

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).

Edmonds–Karp (1972)

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)

Dinic (1970)

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, and the heuristics that make it fast

Push–relabel, Goldberg & Tarjan (1988). No augmenting paths. Maintain a preflow (nodes may have excess) and heights:

  • push excess from $u$ along a residual arc to $v$ with $h(u) = h(v) + 1$;
  • relabel $u$ to $1 + \min h(v)$ over residual neighbours when it can't push.

$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:

familyDinicpush–relabelOR-Tools
layered 40×400.0573.7060.003
random n = 15000.0360.0270.004
bipartite 300×3000.0060.0070.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

Dinic and push–relabel, on the example

Dinic: one phase is enough

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$: undo4, 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

The theoretical frontier moved recently

  • 1956 Ford–Fulkerson: augmenting paths.
  • 1970–72 Dinic, Edmonds–Karp: polynomial.
  • 1988 Goldberg–Tarjan: push–relabel.
  • 1998 Goldberg–Rao: $\tilde O(\min(E^{1/2}, V^{2/3})\, E \log U)$.
  • 2013 Orlin: $O(VE)$, strongly polynomial.
  • 2022 Chen, Kyng, Liu, Peng, Probst Gutenberg & Sachdeva: almost-linear time, $E^{1+o(1)}$, for max flow and min-cost flow.
why the 2022 result matters here

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

Costs

The cheapest way to send a given amount.

14 · costs

Min-cost flow

min-cost flow

Send $d$ units from $s$ to $t$ minimizing $\sum_e \text{cost}_e f_e$, subject to capacities.

optimality

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).

successive shortest paths with potentials
  1. Bellman–Ford once for potentials $\pi$.
  2. Repeat: Dijkstra on reduced costs $c_\pi(u,v) = c(u,v) + \pi(u) - \pi(v) \ge 0$; update $\pi \mathrel{+}= \text{dist}$; augment along the shortest path.

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

Successive shortest paths, on a small network

arccapacitycost
$s \to a$21
$s \to b$24
$a \to b$11
$a \to t$15
$b \to t$31

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).

two rounds

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

Reductions

Flows in disguise, which is the skill the checkpoint asks for.

14 · reductions

Project selection: a maximum closure is a minimum cut

s t p₁ p₂ q 85 ∞∞ 6 profit +8 profit +5 cost 6

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".

  • $s \to p$ with capacity profit, for positive profits;
  • $p \to t$ with capacity $-$profit, for negative ones;
  • $p \to q$ with capacity $\infty$ if $p$ requires $q$.

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

More flows in disguise

bipartite matching

$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.

baseball elimination (Schwartz 1966)

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.

image segmentation

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).

scheduling with deadlines

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

Lab 14 — what you build

about 3 hours · uv run co lab 14
  1. Edmonds–Karp, and the minimum cut that certifies it.
  2. Dinic: level graphs and blocking flows.
  3. FIFO push–relabel.
  4. Min-cost flow by successive shortest paths with potentials.
  5. Project selection as a minimum cut.
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

Done when

checkpoint
  • All five lab steps are ✓.
  • Handed a problem you have not seen, you can decide whether it is a flow in disguise and write the reduction, including the capacities that encode the objective.
  • You can explain integrality of maximum flows two ways: augmenting paths, and total unimodularity.

Reading

Korte & Vygen, ch. 8–9.
Kleinberg & Tardos, Algorithm Design, ch. 7.
Goldberg & Tarjan, JACM 35 (1988) (optional).

Next

15 matching: Hopcroft–Karp, the Hungarian algorithm, blossoms · 18 uses this unit's ideas for alldifferent filtering.