Sub topic 12
Proving the O(V E^2) Bound
The earlier chapter on Ford-Fulkerson showed that a bad choice of augmenting paths can make the algorithm take time proportional to the size of the maximum flow, and that irrational capacities can stop it from terminating at all. Edmonds-Karp fixes this with one rule: always augment along a shortest path, measured in number of edges, in the residual graph. This chapter proves that this rule limits the number of augmentations to O(V E), whatever the capacities are. Since each augmentation costs one BFS, the total running time is O(V E^2).
This chapter assumes you already know:
- The residual graph G_f, residual capacities, and reverse edges.
- Augmenting paths and the bottleneck capacity.
- How BFS computes shortest-path distances in an unweighted graph.
- Proof by contradiction.
Notation and the shape of the proof
Write δf(s, v) for the number of edges on a shortest path from s to v in the residual graph Gf. If v cannot be reached, δ_f(s, v) = ∞. An edge (u, v) on an augmenting path p is critical if its residual capacity equals the bottleneck of p. After the augmentation it has zero residual capacity, so it disappears from the residual graph. Every augmentation has at least one critical edge.
The proof has three steps:
- Monotonicity lemma. No vertex's distance from s ever decreases during Edmonds-Karp.
- Critical-edge lemma. Each edge can be critical at most about V/2 times.
- Counting. There are O(E) edges that can be critical, so there are O(V E) augmentations, and each one costs O(E).
Lemma 1: distances never decrease
Claim. Let f be the flow before an augmentation and f' the flow after it. For every vertex v, δf'(s, v) ≥ δf(s, v).
Proof. Suppose the claim is false. Among all vertices whose distance decreased, pick the one, v, with the smallest new distance δf'(s, v). So δf'(s, v) < δf(s, v). Since v ≠ s, take a shortest path from s to v in Gf' and let u be the vertex just before v. Then:
- δf'(s, v) = δf'(s, u) + 1.
- δf'(s, u) ≥ δf(s, u), because u is closer to s than v in G_f', and v was chosen as the closest vertex whose distance decreased.
The edge (u, v) is in Gf'. There are two cases, depending on whether it was already in Gf.
Case 1: (u, v) was in G_f. Then a path to u followed by (u, v) gives
δf(s, v) ≤ δf(s, u) + 1 ≤ δf'(s, u) + 1 = δf'(s, v),
which contradicts δf'(s, v) < δf(s, v).
Case 2: (u, v) was not in G_f. A residual edge appears only when the augmentation pushes flow in the opposite direction. So the augmenting path used the edge (v, u). That path is a shortest path in Gf, so its vertices sit at consecutive distances: δf(s, u) = δ_f(s, v) + 1. Then
δf(s, v) = δf(s, u) − 1 ≤ δf'(s, u) − 1 = δf'(s, v) − 2,
which again contradicts δf'(s, v) < δf(s, v).
Both cases are impossible, so no distance decreases. ∎
Lemma 2: each edge is critical O(V) times
Claim. For each pair of vertices u and v, the residual edge (u, v) becomes critical at most V/2 times.
Proof. Suppose (u, v) is critical while the flow is f. It lies on a shortest augmenting path, so
δf(s, v) = δf(s, u) + 1.
The augmentation removes (u, v) from the residual graph, because its residual capacity drops to zero. Before (u, v) can be critical again, it has to reappear. That only happens when some later augmentation, at flow f', pushes flow along (v, u). That path is also a shortest path, so
δf'(s, u) = δf'(s, v) + 1.
By Lemma 1, δf'(s, v) ≥ δf(s, v). Putting the pieces together:
δf'(s, u) = δf'(s, v) + 1 ≥ δf(s, v) + 1 = δf(s, u) + 2.
So between two consecutive times that (u, v) is critical, u's distance from s goes up by at least 2. It starts at 0 or more. While u is still on an augmenting path it cannot be t, because the path stops at t, and a simple path has at most V − 1 edges, so δ(s, u) ≤ V − 2 at those moments. The distance can rise by 2 from 0 to V − 2 at most (V − 2)/2 times. That gives at most (V − 2)/2 + 1 = V/2 critical events. ∎
Counting the total work
The residual graph has at most 2E edges: each original edge plus its reverse. Each is critical at most V/2 times, and every augmentation makes at least one edge critical. So the number of augmentations is at most
2E · V/2 = V E, which is O(V E).
Each iteration runs one BFS and walks back along the path it found. With an adjacency-list representation that costs O(V + E). Vertices that cannot be reached from s never take part, so you may assume E ≥ V − 1, and O(V + E) = O(E). The total is
O(V E) augmentations × O(E) per augmentation = O(V E^2).
The argument never uses the capacity values. It only counts edges and distances. That is why Edmonds-Karp runs in polynomial time even when capacities are huge, and why it terminates even with irrational capacities, where plain Ford-Fulkerson might not.
Gotchas when applying the bound
| Situation | Effect on the bound | Column | Column |
|---|---|---|---|
| Adjacency matrix BFS (scans all V vertices per node) | Each BFS costs O(V^2), so the total becomes O(V^3 E) | ||
| Adjacency list with paired reverse edges | Each BFS costs O(E), matching the proof | ||
| Parallel edges stored separately | E counts every stored edge, so the bound grows with duplicates | ||
| DFS instead of BFS for path search | Lemma 1 fails; only the O(E · \ | f*\ | ) Ford-Fulkerson bound holds |
| Dense graphs, E ≈ V^2 | O(V^5) in the worst case, so prefer Dinic or push-relabel |
The bound is a worst case. On many real inputs Edmonds-Karp needs far fewer than V E augmentations, but you should not count on that for adversarial or very dense graphs.
Watching the lemma hold in practice
The program below runs Edmonds-Karp on the network used throughout this tutorial. It records the BFS distance labels before each augmentation and counts critical edges. It then checks that no distance ever decreased, which is Lemma 1, and that no edge was critical more than V/2 times, which is Lemma 2.
from collections import deque
INF = float('inf')
def bfs(residual, s):
n = len(residual)
dist = [INF] * n
parent = [-1] * n
dist[s] = 0
queue = deque([s])
while queue:
u = queue.popleft()
for v in range(n):
if dist[v] == INF and residual[u][v] > 0:
dist[v] = dist[u] + 1
parent[v] = u
queue.append(v)
return dist, parent
def edmonds_karp_traced(capacity, s, t):
n = len(capacity)
residual = [row[:] for row in capacity]
flow = 0
history = []
critical = {}
while True:
dist, parent = bfs(residual, s)
history.append(dist)
if dist[t] == INF:
return flow, history, critical
path = []
v = t
while v != s:
path.append((parent[v], v))
v = parent[v]
bottleneck = min(residual[u][v] for u, v in path)
for u, v in path:
if residual[u][v] == bottleneck:
critical[(u, v)] = critical.get((u, v), 0) + 1
residual[u][v] -= bottleneck
residual[v][u] += bottleneck
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, history, critical = edmonds_karp_traced(capacity, 0, 5)
n = len(capacity)
monotone = all(
history[i + 1][v] >= history[i][v]
for i in range(len(history) in range(n)
)
print("max flow:", flow) # 23
print("augmentations:", len(history) - 1) # 3
print("distancesone) # True
print("max critical count:", max(critical.values()), "<=", n // 2)Treating an distance ∞ fits the lemma: once a vertex is cut off from s, it stays cut off. Try the check on larger random graphs too. It should never print False.
Exercise
The adjacency-matrix implementation from the Edmonds-Karp chapter scans all n vertices for each vertex it dequeues. What is its worst-case running time, and why does the number of augmentations stay the same?
Show answer
Each BFS costs O(V^2), so the total is O(V E) × O(V^2) = O(V^3 E). The count of augmentations comes from Lemmas 1 and 2, which depend only on shortest-path structure, not on how the graph is stored, so it stays O(V E).
Exercise
In Lemma 2, why can the distance δ(s, u) of the tail of a critical edge (u, v) never exceed V − 2?
Show answer
The edge (u, v) lies on a simple augmenting path from s to t, so u is not t, and v comes after u on that path. A simple path has at most V − 1 edges, and at least one edge (u, v) follows u, so u is at most V − 2 edges from s.
Exercise
Someone replaces BFS with DFS but keeps everything else the same. Which step of the proof fails first, and what bound still holds for integer capacities?
Show answer
Case 2 of Lemma 1 fails. A DFS path is not a shortest path, so δf(s, u) = δf(s, v) + 1 need not hold for an edge (v, u) on it, and a new reverse edge can lower a distance. Without Lemma 1, Lemma 2 also fails. Only the generic Ford-Fulkerson bound O(E · |f|) remains, where |f| is the maximum flow value.