Primero, el cuestionario del capítulo 5Primero, repasa el capítulo 5
Haz el cuestionario del capítulo 5: 10 preguntas, se aprueba con 7 correctas. Puedes repetirlo cuantas veces quieras.
Pipes and Bottlenecks
Capítulo 6, lección 1 de 2
Maximum flow and minimum cut: how much a network can carry, and what limits it.
Narrado en inglés. Activa arriba los subtítulos en inglés, árabe, alemán, español o francés.
Lección escrita
10 min de lecturaEscrita en inglés.
In October 1955, two researchers at the RAND Corporation finished a secret report for the United States Air Force. Theodore Harris and General Frank Ross had modelled the railways from the Soviet Union into Eastern Europe as a network: 44 regions joined by 105 links, each with a capacity. They found the most the network could carry, 163,000 tons, and a set of links whose capacities added up to exactly the same amount. Cut those, and nothing gets through.
The report stayed secret until 1999, but its question became one of the great results of graph theory: the biggest flow always equals the smallest cut. This lesson covers flow networks, the paths that fill them, the undo trick that makes them work, why the biggest flow equals the smallest cut, and where flows appear today.
Flow networks
A flow network is a directed graph with a source s, where everything starts, and a sink t, where everything ends. Each edge is a pipe with a capacity c(u, v): the most it can carry.
A flow puts an amount f(u, v) on every edge, with two rules:
- Capacity. No edge carries more than its capacity: 0 ≤ f(u, v) ≤ c(u, v).
- Conservation. At every vertex except the source and the sink, what flows in must flow out.
The value of the flow is how much leaves the source, which is the same as how much reaches the sink. The maximum flow problem asks for the largest value.
Water mains, oil pipelines, rail lines and data links all fit this picture: things move from one place to another, and every link has a limit.
Our example is the network flow guide's: six vertices and seven pipes.
| Pipe | S → A | S → B | A → C | A → D | B → D | C → T | D → T |
|---|---|---|---|---|---|---|---|
| Capacity | 10 | 10 | 9 | 6 | 10 | 10 | 10 |
Six vertices and seven pipes, from S to T.
Push along paths
The natural idea is to push flow along a path, as much as its narrowest pipe allows. That narrowest pipe is the bottleneck.
- S, A, D, T: the pipe A → D holds 6, so 6 units go through.
- S, B, D, T: the pipe D → T has only 4 left, so 4 more.
- S, A, C, T: the pipe S → A has 4 left, so 4 more. That makes 14.
Now every path from S to T has a full pipe on it: S → A (10/10), A → D (6/6) or D → T (10/10). We are stuck at 14, but 14 is not the maximum.
Stuck at 14: every path has a full pipe.
The mistake was the first path. Six units took the pipe from A to D and blocked the way out of D for the flow coming from B. Pushing greedily can paint you into a corner.
The undo trick
The fix is to allow undo. The residual graph shows what can still change:
- forward, the capacity left on each pipe, c(u, v) − f(u, v);
- backward, the flow already on it, f(u, v), which we may send back.
In the residual graph at 14 there is a new path: S → B (6 left), B → D (6 left), then backward from D to A (6 on A → D), then A → C (5 left) and C → T (6 left).
A path in the residual graph uses the backward edge D → A.
Going backward from D to A means taking flow off the pipe A → D. The flow from B takes over the way out of D, and the flow from A goes up to C instead. The bottleneck is 5; push it, and the flow reaches 19.
This is the Ford-Fulkerson method:
It is the chain trick from the matching lesson again. There, flipping an alternating path reassigned the jobs; here, a backward edge reroutes flow that was already placed.
The smallest cut
How do we know 19 is the best? We need a cut.
A cut splits the vertices into two sides, S on one and T on the other. Its capacity is the total of the pipes that cross from the source side to the sink side. For example, with S and A on the source side, S → B, A → C and A → D cross: 10 + 9 + 6 = 25.
Put S, A, B and D on one side, and C and T on the other. Only two pipes cross: A → C with 9, and D → T with 10. That is 19.
The minimum cut: A → C and D → T, 9 + 10 = 19.
With four vertices in the middle there are 2⁴ = 16 ways to cut this network. Cutting right at the source costs 20, and no cut is smaller than 19.
A flow of 19 and a cut of 19: each proves the other is the best possible. This is the max-flow min-cut theorem, proved by Lester Ford and Delbert Fulkerson in 1956, and at the same time by Peter Elias, Amiel Feinstein and Claude Shannon.
The method hands you the cut for free. When no augmenting path is left, the vertices you can still reach from the source in the residual graph form the source side of a minimum cut. Here they are S, A, B and D. Why do the two numbers match?
- Every pipe leaving that side is full, or you could reach further.
- Every pipe coming back into it is empty, or you could undo it.
So the flow equals the capacity of the cut. That is exactly what Harris and Ross reported: a flow of 163,000 tons, and a cut of the same size.
Choosing paths
Ford-Fulkerson lets you pick any augmenting path, and bad picks can be very slow. Take a diamond where four outer pipes hold 1,000 and a middle pipe holds 1. Each path through the middle pipe adds just one unit: pick badly every time, and you need 2,000 rounds. Pick well, and you need 2.
Bad picks: 2,000 rounds instead of 2.
In 1970 Yefim Dinitz, and in 1972 Jack Edmonds and Richard Karp, gave a simple rule: always take a shortest path, found by breadth-first search.
- With shortest paths, the distance from the source to the sink in the residual graph never shrinks.
- Each pipe can be the bottleneck only a few times before that distance grows.
So the number of rounds depends only on the size of the network, not on the capacities: Edmonds-Karp runs in O(V · E²). On our network it needs three rounds: S, A, C, T (+9), S, A, D, T (+1) and S, B, D, T (+9). And research goes on: in 2022, six researchers found an algorithm that runs in almost linear time.
from collections import deque
def max_flow(cap, s, t):
# cap: dict {u: {v: capacity}}; returns (value, source side of a min cut)
res = {u: dict(vs) for u, vs in cap.items()}
for u, vs in cap.items():
for v in vs:
res.setdefault(v, {}).setdefault(u, 0) # backward edges start at 0
value = 0
while True:
prev = {s: None}
queue = deque([s])
while queue and t not in prev: # BFS: a shortest augmenting path
u = queue.popleft()
for v, r in res[u].items():
if r > 0 and v not in prev:
prev[v] = u
queue.append(v)
if t not in prev:
return value, set(prev) # reachable vertices = min cut side
path, v = [], t
while prev[v] is not None:
path.append((prev[v], v))
v = prev[v]
b = min(res[u][v] for u, v in path) # the bottleneck
for u, v in path:
res[u][v] -= b
res[v][u] += b # the undo edge
value += b
cap = {"S": {"A": 10, "B": 10}, "A": {"C": 9, "D": 6}, "B": {"D": 10},
"C": {"T": 10}, "D": {"T": 10}, "T": {}}
value, side = max_flow(cap, "S", "T")
(value, sorted(side)) # (19, ['A', 'B', 'D', 'S'])
Matching as flow
Flow also solves the matching problem from the last lesson. Put a source before the technicians and a sink after the jobs, and give every pipe a capacity of one.
There is a bonus, integrality: when every capacity is a whole number, the method only ever pushes whole amounts, so some maximum flow uses whole numbers only. Half a technician never happens.
A maximum flow is then a maximum matching: each unit of flow is one technician paired with one job. And the undo edges are exactly the alternating paths of lesson 18.
Matching as flow: every pipe holds 1, and 5 units give 5 pairs.
In the world
Baseball elimination. In 1966 Benjamin Schwartz used flow to decide when a baseball team can no longer finish first. Say the Wolves have 78 wins and 4 games left, so they can reach at most 82. The Lions have 81, the Bears and the Hawks 80 each, and those three still play each other 6 times.
No single team is out of reach. But someone must win those six games: the three teams would end with 81 + 80 + 80 + 6 = 247 wins between them, more than 3 × 82 = 246. One of them must pass the Wolves.
A flow network finds this automatically. Games flow to teams, and each team's pipe to the sink holds the wins it can still afford: 1 for the Lions, 2 for the Bears, 2 for the Hawks. The flow can carry only 5 of the 6 games, so the Wolves are out.
Baseball elimination: only 5 of 6 games get through.
More uses. Airlines use flows to decide which planes and crews can cover which flights. The same cuts expose the weak points of power grids and computer networks, and measure what pipelines, roads and railways can carry.
Cutting a photo. In a photo, each pixel becomes a vertex. Pixels that look like the object get a pipe from the source, pixels that look like background get a pipe to the sink, and similar neighbours are joined. The minimum cut traces the outline.
Segmentation: the minimum cut traces the object's outline.
Key ideas
- A flow respects every capacity and balances at every vertex. Paths in the residual graph increase it, and backward edges undo bad choices.
- No flow can be larger than any cut, and the maximum flow equals the minimum cut. When no path is left, the reachable vertices give the cut.
- Taking shortest paths, as Edmonds and Karp did, keeps the number of rounds small, whatever the capacities.
- With whole-number capacities there is a whole-number maximum flow, which is why flow solves matching.
Check yourself
1. In the example, the capacity of A → C rises from 9 to 12. What is the maximum flow now?
2. A network has a cut with capacity 40 and a flow of value 40. What can you conclude?
3. Why does Ford-Fulkerson need backward edges at all?
Practice on the platform
The network flow guide walks through this same network, its residual graph and its minimum cut. In the maximum flow visualizer, import the network, choose S as the source and T as the sink, and run it: it finds a flow of 19 with three augmenting paths, exactly the size of our cut.
A continuación