Maximum flow: Ford-Fulkerson and Edmonds-Karp
Maximum flow pushes as much as possible from source to sink without exceeding edge capacities. Augmenting paths push flow and residual capacities update.
The idea
Pipes carry water from s to t, and each pipe has a capacity per second. How much water can reach t at most?
The same question covers network bandwidth, road traffic, and matching workers to jobs.
Intuition
One path carries only as much as its narrowest edge. Here a→t holds 2, so s→a carries 2 of its 5.
Water that enters a node must leave it. Only s produces flow and only t absorbs it.
Separate paths add up. Push 2 along s→a→t and 2 along s→b→t, so 4 units reach t.
After a push, each path edge gets a dashed backward edge with the same amount. A later path can use it to undo flow.
Undoing flow lets the algorithm fix an early choice. The room left plus the backward edges form the residual graph.
Complexity
Each BFS costs O(E). Shortest paths force at most O(V · E) augmentations, so Edmonds-Karp runs in O(V · E²).
Every augmentation saturates some edge, and the BFS distance to that edge can only grow. Space is O(V + E) for the residual graph.
Pitfalls
Without backward edges the first path s→a→b→t blocks everything, and the flow stays at 1. A backward edge b→a reroutes that unit, so 2 fit.
Plain Ford-Fulkerson may pick paths by DFS. Here it can cross a→b and back 2,000 times, one unit each, while BFS needs two paths.
With BFS the number of augmentations depends only on V and E, never on the capacities.
Variations
When no path is left, the nodes reachable from s form one side of a minimum cut. Its edges a→b and d→t add up to the max flow, 4.
Dinic's algorithm pushes along many shortest paths per BFS, and bipartite matching is max flow with capacity 1 on every edge.
Code
Pseudocode
room[u][v] ← capacity of u → v, room[v][u] ← 0
flow ← 0
loop
parent ← BFS from s using only edges with room > 0
if t was not reached: return flow
b ← smallest room on the path s → … → t
for each edge (u, v) on the path
room[u][v] ← room[u][v] − b
room[v][u] ← room[v][u] + b
flow ← flow + b
Python
from collections import deque
def max_flow(n, edges, s, t):
room = [[0] * n for _ in range(n)]
for u, v, c in edges:
room[u][v] += c
flow = 0
while True:
parent = [-1] * n
parent[s] = s
q = deque([s])
while q and parent[t] < 0:
u = q.popleft()
for v in range(n):
if parent[v] < 0 and room[u][v] > 0:
parent[v] = u
q.append(v)
if parent[t] < 0:
return flow
b, v = float("inf"), t
while v != s:
b = min(b, room[parent[v]][v])
v = parent[v]
v = t
while v != s:
room[parent[v]][v] -= b
room[v][parent[v]] += b
v = parent[v]
flow += b
JavaScript
function maxFlow(n, edges, s, t) {
const room = Array.from({ length: n }, () => new Array(n).fill(0));
for (const [u, v, c] of edges) room[u][v] += c;
let flow = 0;
while (true) {
const parent = new Array(n).fill(-1);
parent[s] = s;
const q = [s];
while (q.length > 0 && parent[t] < 0) {
const u = q.shift();
for (let v = 0; v < n; v++) {
if (parent[v] < 0 && room[u][v] > 0) { parent[v] = u; q.push(v); }
}
}
if (parent[t] < 0) return flow;
let b = Infinity;
for (let v = t; v !== s; v = parent[v]) b = Math.min(b, room[parent[v]][v]);
for (let v = t; v !== s; v = parent[v]) {
room[parent[v]][v] -= b;
room[v][parent[v]] += b;
}
flow += b;
}
}
Cost at a glance
| Time | O(V · E²) |
|---|---|
| Space | O(V + E) |
| Input | Directed capacities, source and sink |
| Output | Maximum flow value and edge flows |
Practice problems (4)
Why backward edges Warm-up
Every edge has capacity 1. The first push sends 1 unit along s → a → b → t. Without backward edges no other path has room. What do backward edges add?
Augment by hand Warm-up
Follow Edmonds-Karp from s to t. Each time BFS finds a path, type how many units it pushes. Edge labels show the room left.
Bottleneck edge Standard
Edmonds-Karp runs from s to t. Each time BFS highlights a path, click the bottleneck edge: the path edge with the least room left.
Implement Edmonds-Karp Standard
Return the maximum flow from source to sink. graph.edges lists [u, v, capacity] for directed edges u → v. Parallel edges add their capacities.
Use BFS to find each augmenting path, as Edmonds-Karp does.
Next on the path: Hash maps for lookup and counting