Network Flow & Matching
Maximum flow, the max-flow min-cut theorem, Ford-Fulkerson / Edmonds-Karp, and bipartite matching.
A flow network is a directed graph where each edge has a capacity, with a source s and a sink t. The maximum flow problem asks: how much can flow from s to t without exceeding any capacity, with flow conserved at every intermediate vertex? Think water pipes, network bandwidth, or traffic.
Ford-Fulkerson and the residual graph
- Start with zero flow.
- Find an augmenting path from s to t in the residual graph — edges with leftover capacity, plus reverse edges carrying the flow already sent (which lets the algorithm "undo" earlier bad choices).
- Push the bottleneck amount along it; update residual capacities.
- Repeat until no augmenting path exists.
Edmonds-Karp finds augmenting paths with BFS (shortest first), guaranteeing O(V · E²). Dinic's algorithm (BFS level graph + blocking flows) achieves O(V² · E) and is the competitive-programming favourite.
int maxFlow(int[][] cap, int s, int t) { // adjacency-matrix capacities
int n = cap.length, flow = 0;
int[][] res = new int[n][];
for (int i = 0; i < n; i++) res[i] = cap[i].clone();
int[] parent = new int[n];
while (true) {
Arrays.fill(parent, -1);
parent[s] = s;
Deque<Integer> q = new ArrayDeque<>(List.of(s));
while (!q.isEmpty() && parent[t] == -1) {
int u = q.poll();
for (int v = 0; v < n; v++)
if (parent[v] == -1 && res[u][v] > 0) { parent[v] = u; q.offer(v); }
}
if (parent[t] == -1) return flow; // no augmenting path
int push = Integer.MAX_VALUE;
for (int v = t; v != s; v = parent[v]) push = Math.min(push, res[parent[v]][v]);
for (int v = t; v != s; v = parent[v]) {
res[parent[v]][v] -= push;
res[v][parent[v]] += push; // reverse edge
}
flow += push;
}
}from collections import deque
def max_flow(cap, s, t):
n = len(cap)
res = [row[:] for row in cap]
flow = 0
while True:
parent = [-1] * n
parent[s] = s
q = deque([s])
while q and parent[t] == -1:
u = q.popleft()
for v in range(n):
if parent[v] == -1 and res[u][v] > 0:
parent[v] = u
q.append(v)
if parent[t] == -1:
return flow
push, v = float("inf"), t
while v != s:
push = min(push, res[parent[v]][v]); v = parent[v]
v = t
while v != s:
res[parent[v]][v] -= push
res[v][parent[v]] += push # reverse edge
v = parent[v]
flow += pushfunction maxFlow(cap: number[][], s: number, t: number): number {
const n = cap.length, res = cap.map((r) => [...r]);
let flow = 0;
for (;;) {
const parent = new Array(n).fill(-1);
parent[s] = s;
const q = [s];
for (let h = 0; h < q.length && parent[t] === -1; h++)
for (let v = 0; v < n; v++)
if (parent[v] === -1 && res[q[h]][v] > 0) { parent[v] = q[h]; q.push(v); }
if (parent[t] === -1) return flow;
let push = Infinity;
for (let v = t; v !== s; v = parent[v]) push = Math.min(push, res[parent[v]][v]);
for (let v = t; v !== s; v = parent[v]) { res[parent[v]][v] -= push; res[v][parent[v]] += push; }
flow += push;
}
}Max-flow min-cut theorem
An s-t cut splits the vertices into a side containing s and a side containing t; its capacity is the total capacity of edges crossing from the s-side to the t-side. The theorem:
The maximum flow equals the minimum cut capacity.
When Ford-Fulkerson stops, the vertices reachable from s in the residual graph form the s-side of a minimum cut. So max flow also answers "what's the cheapest set of links to cut to disconnect s from t?" — network reliability, image segmentation (graph cuts), and project selection.
Bipartite matching
Match workers to jobs (each at most once, only compatible pairs). Add a source connected to every worker and a sink connected from every job, all capacity 1; the max flow is the maximum matching. Simpler dedicated algorithms: augmenting-path DFS (Kuhn's, O(V·E)) and Hopcroft-Karp (O(E√V)). Weighted assignment is solved by the Hungarian algorithm.
- Max flow pushes as much as possible from source to sink under capacity limits.
- Augment along paths in the residual graph; reverse edges allow undoing flow.
- Edmonds-Karp (BFS paths) is O(VE²); Dinic is faster in practice.
- Max flow = min cut. Bipartite matching reduces to max flow with unit capacities.
QWhy are reverse edges necessary in the residual graph?hard
A greedy first path might route flow through an edge that blocks a better overall solution. Reverse edges let a later augmenting path cancel some of that flow and reroute it, which is what makes the algorithm reach the true maximum.