Sub topic 9
Edmonds-Karp: Choosing the Shortest Augmenting Path
This chapter assumes you know breadth-first search (BFS), the residual graph, augmenting paths, and the Ford-Fulkerson method from the earlier chapters. It also assumes you have seen how an unlucky choice of paths can make Ford-Fulkerson slow.
The one rule Edmonds-Karp adds
Ford-Fulkerson says "find any augmenting path and push flow along it." It does not say which path to pick. That freedom is the problem. With a bad choice, the number of iterations can grow with the size of the capacities. With irrational capacities, the algorithm may never stop at all.
Edmonds-Karp removes the freedom with one rule:
- Always augment along a shortest augmenting path, meaning the path with the fewest edges in the residual graph.
BFS finds exactly this path. Every residual edge with positive capacity counts as length 1, and capacities play no part in choosing the path. They only set the bottleneck.
Why the shortest path helps
Augmenting along shortest paths gives a monotonic property: the BFS distance from s to every vertex in the residual graph never decreases. Every augmentation also saturates at least one edge on the path, and each time an edge becomes critical again, the distance to its tail has grown by at least 2. As a result, the algorithm does at most O(VE) augmentations. Each BFS costs O(E) on an adjacency list, so the total is O(VE²). The bound depends only on the size of the graph, never on the capacity values. A later chapter proves it.
A working implementation
This version uses an adjacency matrix for clarity, so each BFS here costs O(V²). The paired-edge chapter gives the adjacency-list form.
from collections import deque
def edmonds_karp(capacity, s, t):
n = len(capacity)
residual = [row[:] for row in capacity]
max_flow = 0
while True:
parent = [-1] * n
parent[s] = s
queue = deque([s])
while queue and parent[t] == -1:
u = queue.popleft()
for v in range(n):
if parent[v] == -1 and residual[u][v] > 0:
parent[v] = u
queue.append(v)
if parent[t] == -1:
return max_flow, residual
bottleneck = float('inf')
v = t
while v != s:
u = parent[v]
bottleneck = min(bottleneck, residual[u][v])
v = u
v = t
while v != s:
u = parent[v]
residual[u][v] -= bottleneck
residual[v][u] += bottleneck
v = u
max_flow += bottleneck
capacity = [
[0, 16, 13, 0, 0, 0],
[0, 0, 0, 12, 0, 0],
[0, 4, 0, 0, 14, 0],
[0, 0, 9, 0, 0, 20],
[0, 0, 0, 7, 0, 4],
[0, 0, 0, 0, 0, 0],
]
flow, residual = edmonds_karp(capacity, 0, 5)
print(flow) # 23On this network, the run makes three augmentations:
| Iteration | BFS path | Edges | Bottleneck | Total flow |
|---|---|---|---|---|
| 1 | 0 → 1 → 3 → 5 | 3 | 12 | 12 |
| 2 | 0 → 2 → 4 → 5 | 3 | 4 | 16 |
| 3 | 0 → 2 → 4 → 3 → 5 | 4 | 7 | 23 |
On the fourth BFS, t cannot be reached, so the loop returns 23.
Common errors
- Searching the original graph. BFS must follow residual edges, including reverse edges, which carry the flow already sent. Skip the reverse edges and you can get stuck below the maximum.
- Using DFS "because it's simpler." The result is still a correct Ford-Fulkerson, but you lose the O(VE²) guarantee.
- Reusing parent arrays. Reset
parentbefore each BFS. Stale entries create fake paths. - Updating only one direction. Every augmentation must reduce
residual[u][v]and increaseresidual[v][u]by the same amount.
Exercise
In the trace above, the augmenting path lengths were 3, 3, then 4. Is it possible for Edmonds-Karp to later pick a path of length 3 again on some network after picking one of length 4? Why or why not?
Show answer
No. Shortest-path distances from s in the residual graph never decrease under Edmonds-Karp, so the lengths of the chosen augmenting paths form a nondecreasing sequence. Once the shortest s-t path has length 4, no later path can be shorter.
Exercise
What is the running time of the adjacency-matrix version above, and why does it differ from O(VE²)?
Show answer
Each BFS scans a full row for every vertex it dequeues, which costs O(V²) instead of O(E). With O(VE) augmentations, the total is O(V³E). An adjacency-list representation brings each BFS down to O(E) and the total to O(VE²).