function edmondsKarp(graph: Graph, source: Node, sink: Node) {const flow = new Map<Edge, number>(); let total = 0;
while (true) {const parent = bfs(graph, flow, source, sink); // shortest augmenting path in the residual graph
if (!parent.has(sink)) break;
let bottleneck = Infinity;
for (let node = sink; node !== source; node = parent.get(node)!.from) bottleneck = Math.min(bottleneck, residual(parent.get(node)!, flow));
for (let node = sink; node !== source; node = parent.get(node)!.from) push(parent.get(node)!, bottleneck, flow);
total += bottleneck;
}
return total;
}
Source A, sink J. Every edge shows flow / capacity, all flows 0.
Understanding Edmonds-Karp
The idea
A flow sends units from a source to a sink through edges with capacities. Ford and Fulkerson's method finds any path with spare capacity, pushes as much as its tightest edge allows, and repeats; when no such path is left the flow is maximum. Pushing flow also creates reverse residual capacity, so a later path can undo an earlier bad choice.
Edmonds-Karp fixes the one thing Ford-Fulkerson left open: which path to take. Choosing the shortest augmenting path, by BFS, bounds the number of augmentations by V · E regardless of the capacities. When the search from the source finally stalls, the reachable nodes form a minimum cut whose capacity equals the flow.
The four stages
Complexity
Versus its siblings
Pseudocode
EDMONDSKARP(graph, source, sink)
flow ← 0 on every edge
repeat
BFS from source over edges with residual capacity (capacity − flow forward, flow backward)
if the sink was not reached: stop, the flow is maximum
bottleneck ← the smallest residual capacity on the path; add it along the path (subtract on backward edges)
When to use
Pitfalls
History
Ford and Fulkerson published the augmenting path method in 1956, for a RAND study of rail capacity between the Soviet Union and Eastern Europe. Yefim Dinitz in 1970 and Jack Edmonds with Richard Karp in 1972 showed that shortest augmenting paths make it polynomial; the max-flow min-cut theorem is from the same 1956 paper.