Sub topic 11
Finding the Minimum Cut
This chapter assumes you have read the earlier chapters on the residual graph, the max-flow min-cut theorem, and Edmonds-Karp. You should also be comfortable with breadth-first search (BFS). The code here reuses the edmonds_karp function and the six-node capacity matrix from the Edmonds-Karp chapter.
Why the Cut Is Already There
The max-flow min-cut theorem says that when no augmenting path remains, some s-t cut has capacity equal to the flow. The algorithm stops at exactly that point, so the final residual graph already holds a minimum cut. You only need to read it out.
The working model:
- Run max flow to completion and keep the final residual graph.
- Run one more BFS from the source
s, following only edges with residual capacity greater than zero. - Every vertex the BFS reaches goes in the source side
S. Every other vertex goes in the sink sideT. - The cut edges are the original edges that go from
StoT.
Why this works
The sink t is not reachable, because otherwise there would still be an augmenting path. So (S, T) is a valid s-t cut.
Take any original edge u -> v with u in S and v in T. Its residual capacity must be zero, since otherwise BFS would have reached v. That means the edge is saturated: its flow equals its capacity.
Now take any original edge v -> u going from T back into S. If it carried flow, the reverse residual edge u -> v would have positive capacity and BFS would reach v. So that edge carries zero flow.
The net flow across the cut is therefore the sum of the capacities leaving S minus zero. That is the cut's capacity, and it equals the maximum flow.
The Code
from collections import deque
def min_cut(capacity, residual, s):
n = len(capacity)
reachable = [False] * n
reachable[s] = True
queue = deque([s])
while queue:
u = queue.popleft()
for v in range(n):
if not reachable[v] and residual[u][v] > 0:
reachable[v] = True
queue.append(v)
cut_edges = [(u, v) for u in range(n) for v in range(n)
if reachable[u] and not reachable[v] and capacity[u][v] > 0]
return reachable, cut_edges
# Uses capacity and residual from the Edmonds-Karp example
flow, residual = edmonds_karp(capacity, 0, 5)
side, edges = min_cut(capacity, residual, 0)
print(edges) # [(1, 3), (4, 3), (4, 5)]
print(sum(capacity[u][v] for u, v in edges)) # 23, equal to the max flowThe BFS costs O(V^2) on a matrix and O(V + E) on an adjacency list, which is small next to the max-flow computation itself.
Common Errors
| Mistake | Effect | Fix |
|---|---|---|
| Searching the original graph instead of the residual graph | `S` includes vertices beyond the cut, and the search may even reach `t` | Follow `residual[u][v] > 0` |
| Ignoring reverse residual edges | `S` comes out too small, and the cut is not minimum | Search every residual edge, including reverse ones |
| Summing residual capacities of cut edges | The total is 0, because cut edges are saturated | Sum the original `capacity[u][v]` |
| Counting edges from `T` to `S` | The cut capacity is overstated | Count only edges from `S` to `T` |
| Stopping max flow early | `t` is still reachable and there is no valid cut | Run until no augmenting path exists |
Exercise
After running max flow, a vertex x is unreachable from s in the residual graph, and the original edge s -> x has capacity 5. What must the flow on s -> x be, and does this edge count toward the cut capacity?
Show answer
The flow must be 5. s is in S and x is in T, so residual[s][x] must be 0, which means the edge is saturated. It crosses from S to T, so its full capacity of 5 counts toward the cut capacity.
Exercise
A student sums capacity[u][v] over every pair where exactly one of u and v is reachable, and gets a number larger than the max flow. What went wrong?
Show answer
The student also counted edges going from T into S. Cut capacity includes only edges from S to T. Edges from T to S carry zero flow in a maximum flow and must be left out.