This is an excellent question about the Edmonds-Karp algorithm, which is a specific implementation of the Ford-Fulkerson method that uses BFS to find augmenting paths. Let me provide a comprehensive explanation.
Edmonds-Karp Algorithm Overview
The Edmonds-Karp algorithm is an implementation of the Ford-Fulkerson method that uses BFS (Breadth-First Search) to find the shortest augmenting path in the residual graph at each iteration.
Algorithm Steps:
- Initialize flow: set all edges' flow to 0
- While there exists an augmenting path from source to sink in the residual graph:
- Use BFS to find the shortest augmenting path (shortest in terms of number of edges)
- Find the bottleneck capacity along this path
- Augment flow along the path
- Update the residual graph
- Return the maximum flow
What is a Residual Graph?
The residual graph G_f for a flow f contains:
- Forward edges: for each edge (u,v) with capacity c and current flow f, if c > f, include edge with residual capacity c - f
- Backward edges: for each edge (u,v) with flow f > 0, include reverse edge (v,u) with residual capacity f
This allows the algorithm to "undo" previous flow decisions.
BFS vs DFS and Time Complexity
Why DFS Ford-Fulkerson Can Be Exponential:
With DFS, you might choose poor augmenting paths that make minimal progress. Classic example:
v (capacity 1)
/ \
1 / \ 1
/ \
s --2-- t
(capacity 2)
With unlucky DFS choices:
-
Choose path s→v→t, augment by 1. Residual: v→s edge created with capacity 1
-
Choose path s→v→s→t (going backwards!), augment by 1
-
This oscillates, taking exponential iterations with irrational capacities
The classic counterexample uses adversarial choices where DFS might take 2×10^6 iterations on edges with capacity 10^6.
Why BFS Guarantees Polynomial Time:
BFS finds the shortest augmenting path in terms of edge count, which ensures important structural properties. The distance from source to sink in the residual graph never decreases, and each edge can become critical only O(V) times, giving us O(VE²) time complexity overall. by at least 2, so distances range from 0 to V-1, meaning each edge saturates at most V/2 times.
The total complexity breaks down as follows: there are O(VE) possible edge saturations, and since each augmentation saturates at least one edge, we get O(VE) augmentations. Combined with O(E) time per BFS traversal, this yields O(VE²) overall complexity.
For Edmonds-Karp, each edge appears on a shortest augmenting path at most O(V) times because the distance from s increases by at least 2 each time an edge is saturated and later reused. when edge (u,v) is used, d(s,v) = d(s,u) + 1. Once reused, d(s,v) ≥ d(s,u) + 2, so the distance grows by at least 2. Since distances are bounded by V, each edge saturates O(V) times, giving O(VE) total augmentations. Combined with O(E) per BFS traversal, this yields O(VE²) overall.
The key difference between approaches: Ford-Fulkerson with DFS finds arbitrary paths and runs in O(E·F) time, which depends on flow values and can be exponential. Edmonds-Karp using BFS guarantees shortest paths and achieves O(VE²), depending only on graph structure. The distance property ensures that in each phase where the shortest path length stays constant, at least one edge becomes saturated, forcing the distance to increase by at least 2 before it can be used again. This monotonic growth bounds the number of phases and thus the total augmentations. the residual capacities along the path, subtracting flow from forward edges and adding it to reverse edges.
The algorithm repeats this process until no augmenting path exists. The key insight is that each BFS finds a shortest augmenting path, which ensures we make progress efficiently. Since the shortest path length can increase at most O(V) times before the algorithm terminates, and each BFS takes O(E) time, the overall complexity becomes O(VE²). d_f(s,v) + 1 = d_f(s,z), which gives d_f(s,z) > d_f(s,v). This means d_{f'}(s,z) ≥ d_f(s,z) - 1 ≥ d_f(s,v), a contradiction.
Therefore distances never decrease: d_{f'}(s,v) ≥ d_f(s,v). This implies each forward edge (u,v) on a shortest augmenting path can be used at most n/2 times, since every time it reappears as forward, the distance d_f(s,v) must strictly increase from d_f(s,u) + 1.
With O(m) edges each appearing O(n) times as forward edges, we get O(nm) total augmentations. Since each augmentation requires O(n + m) time for BFS, the overall complexity is O(VE²) for Edmonds-Karp.
Edmonds-Karp Algorithm: BFS-Based Max Flow
Algorithm Overview
Edmonds-Karp is a specific implementation of Ford-Fulkerson that uses BFS instead of DFS to find augmenting paths:
def edmonds_karp(graph, source, sink):
residual_graph = initialize_residual_graph(graph)
max_flow = 0
while True:
# KEY: Use BFS to find SHORTEST augmenting path
path = bfs_shortest_path(residual_graph, source, sink)
if not path:
break
# Augment along shortest path
flow = min(residual_capacity[u][v] for u,v in path)
max_flow += flow
# Update residual graph with forward & backward edges
for u, v in path:
residual_graph[u][v] -= flow
residual_graph[v][u] += flow
return max_flow
Why DFS Ford-Fulkerson Can Be Exponential
Pathological Example: With irrational capacities and poor DFS choices:
v (cap 1)
/ \
1 / \ 1
/ \
s --2-- t
DFS might oscillate through:
- s → v → t (augment by 1)
- s → v → s → t (augment by 1, using backward edge)
- Repeat ~2×10^6 times with larger graphs
Fundamental Problem: DFS finds arbitrary paths, potentially making minimal progress per iteration. Time complexity becomes:
O(E⋅∣fmax∣) — depends on capacity values, not graph structure
Why BFS Guarantees Polynomial Time
Core Insight: Distance Monotonicity
Let df(s,v) = shortest path distance from source to vertex v in residual graph Gf.
Lemma (Distance Never Decreases):
df′(s,v)≥d
Proof Sketch:
- If a shorter path existed in Gf′, it must use a back-edge (reverse edge created by augmentation)
- But back-edges only help when a previous augmentation happened on a forward edge
- This creates a contradiction with the shortest path property
Bounding Augmentations
Key Consequence: When edge (u,v) appears in a shortest augmenting path:
df(s,v)=d
When (u,v) appears again in a future shortest path:
df′(s,
Since maximum distance is ∣V∣−1, each edge saturates at most 2∣V∣ times on shortest paths.
Total augmentations:
- ∣E∣ edges × O(∣V∣) saturations per edge = O(∣V∣∣E∣) augmentations
Total Time Complexity
Time=due to distance monotonicity
This is polynomial in graph structure alone, independent of capacity values.
Comparison: DFS vs BFS
| Aspect | DFS Ford-Fulkerson | Edmonds-Karp (BFS) |
|---|
| Path strategy | Arbitrary | Shortest path |
| Edges selected | Any path to sink | Minimum hops to sink |
| Complexity | O(E⋅∥fmax∥) | |
Visual Example: Why Shortest Paths Help
Graph:
2─────5
/ \
s → t (huge capacity)
\ /
3─────4
- DFS: Might pick s→2→5→4→3→s→... (oscillating through poor reverse edges)
- BFS: Always takes 2-hop paths consistently, preventing oscillation
The shortest path constraint prevents adversarial flow patterns that exploit DFS's greedy choices.
When to Use Each
- Edmonds-Karp: General purpose, guaranteed polynomial time
- Dinic's Algorithm: Even faster (O(V2E)) — also uses BFS/layered graphs
- Push-Relabel: Fastest practical (O(V3) or better)
- DFS Ford-Fulkerson: Only when capacities are small integers and speed matters less than simplicity
The fundamental lesson: BFS finds structurally better paths by respecting graph distance, preventing the exponential behavior DFS allows.