Choose the shortest remaining route.
Use BFS for augmenting routes and verify max flow with a final residual cut.
01 Model the idea
Edmonds–Karp is Ford–Fulkerson with breadth-first search selecting an augmenting route with the fewest residual edges. That route rule gives a polynomial O(VE²) bound for adjacency-list implementations, independent of the numerical maximum-flow value. When no route remains, vertices reachable from the source in the residual graph define one side of a minimum cut.
The final residual graph has no source-to-sink route, and its cut certifies the maximum.
02 State the strategy
Read the steps independently of syntax. Explain what each step preserves before translating it into code.
BFS a shortest residual route
AUGMENT its bottleneck
WHEN no route exists, collect reachable vertices
COMPARE flow value with original cut capacity03 Make it concrete
The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.
def residual_graph(n, edges):
graph = [{} for _ in range(n)]
for u, v, capacity in edges:
if type(capacity) is not int or capacity < 0: raise ValueError("Nonnegative integer capacities required")
graph[u][v] = graph[u].get(v, 0) + capacity
graph[v].setdefault(u, 0)
return graph
from collections import deque
def maximum_flow(n, edges, source, sink):
if source == sink: raise ValueError("Distinct source and sink required")
residual, total = residual_graph(n, edges), 0
while True:
parent, queue = {source: None}, deque([source])
while queue and sink not in parent:
node = queue.popleft()
for neighbour, available in residual[node].items():
if available > 0 and neighbour not in parent:
parent[neighbour] = node; queue.append(neighbour)
if sink not in parent: return total, set(parent)
amount, node = float("inf"), sink
while parent[node] is not None:
previous = parent[node]; amount = min(amount, residual[previous][node]); node = previous
node = sink
while parent[node] is not None:
previous = parent[node]
residual[previous][node] -= amount; residual[node][previous] += amount; node = previous
total += amount
edges = [(0, 1, 3), (0, 2, 2), (1, 2, 1), (1, 3, 2), (2, 3, 3)]
flow, reachable = maximum_flow(4, edges, 0, 3)
cut = sum(capacity for u, v, capacity in edges if u in reachable and v not in reachable)
print("flow:", flow)
print("cut capacity:", cut)flow: 5 cut capacity: 5
- 1
The only search change from the DFS version is the queue discipline. The residual-update rule remains the same.
- 2
On the final failed search, parent contains the residual-reachable set. Original edges crossing out of that set define the cut capacity.
- 3
A feasible flow value matching a cut capacity is a useful optimality certificate. Test both flow feasibility and the cut relationship rather than trusting one final number alone.
04 Count the cost
O(VE²) with adjacency lists
O(V + E)
Bounds refer to the shown implementation and exclude printed output unless stated. n is input size; V and E are graph vertices and edges.
05 Your next move
Check the flow/cut equality on a single-edge network.
Reveal one possible solution +
def residual_graph(n, edges):
graph = [{} for _ in range(n)]
for u, v, capacity in edges:
if type(capacity) is not int or capacity < 0: raise ValueError("Nonnegative integer capacities required")
graph[u][v] = graph[u].get(v, 0) + capacity
graph[v].setdefault(u, 0)
return graph
from collections import deque
def maximum_flow(n, edges, source, sink):
if source == sink: raise ValueError("Distinct source and sink required")
residual, total = residual_graph(n, edges), 0
while True:
parent, queue = {source: None}, deque([source])
while queue and sink not in parent:
node = queue.popleft()
for neighbour, available in residual[node].items():
if available > 0 and neighbour not in parent:
parent[neighbour] = node; queue.append(neighbour)
if sink not in parent: return total, set(parent)
amount, node = float("inf"), sink
while parent[node] is not None:
previous = parent[node]; amount = min(amount, residual[previous][node]); node = previous
node = sink
while parent[node] is not None:
previous = parent[node]
residual[previous][node] -= amount; residual[node][previous] += amount; node = previous
total += amount
edges = [(0, 1, 7)]
flow, reachable = maximum_flow(2, edges, 0, 1)
cut = sum(cap for u, v, cap in edges if u in reachable and v not in reachable)
assert flow == cut == 7
print(flow, cut)06 Check your reasoning
What route does Edmonds–Karp choose?
Understand it. Then own it.
Can you explain the invariant, trace an edge case, and defend the cost model? Log the experiment when the reasoning makes sense.