Okay, the user wants an algorithm for maximum flow using the Edmonds-Karp method. I think they’re looking for both a description and pseudocode, along with an explanation and the complexity involved. I’ll provide details on the max-flow concept, the residual graph, and the use of BFS to find the shortest augmenting path. I need to make sure the pseudocode is concise but informative, aiming for clarity without unnecessary fluff. So, let’s get started! Below is a self-contained description and pseudocode for computing a maximum flow using the Edmonds–Karp method.
Problem Setting
Directed graph ( G = (V, E) )
Capacity function ( c : E \to \mathbb{R}_{\ge 0} )
Source vertex ( s \in V )
Sink vertex ( t \in V )
Goal: Find a flow ( f : E \to \mathbb{R}_{\ge 0} ) of maximum value from ( s ) to ( t ) that respects:
Capacity constraints:
( 0 \le f(u, v) \le c(u, v) ) for all ((u, v) \in E)
Flow conservation (except at (s) and (t)):
( \sum_{u : (u, v) \in E} f(u, v) = \sum_{w : (v, w) \in E} f(v, w) ) for all ( v \in V \setminus {s, t} )
Key Ideas of Edmonds–Karp
Start with zero flow.
Construct a residual graph ( G_f ), which reflects where we can push more flow or reduce flow:
Use BFS in the residual graph to find a shortest (in terms of number of edges) augmenting path from ( s ) to ( t ).
Augment flow along this path by the minimum residual capacity on it (the bottleneck).
Update the residual graph and repeat until no augmenting path exists.
Using BFS (rather than arbitrary search) is what makes it Edmonds–Karp specifically.
Pseudocode
Data Structures
capacity[u][v]: capacity of edge (u \to v) (0 if no edge).
flow[u][v]: current flow on edge (u \to v) (initialize to 0).
adj[u]: list of neighbors (v) such that either capacity[u][v] > 0 or capacity[v][u] > 0 (for residual graph adjacency).
Algorithm
text
EDMONDS_KARP(V, adj, capacity, s, t):
for each u in V:
for each v in V:
flow[u][v] ← 0
max_flow ← 0
while true:
# 1. BFS to find augmenting path in residual graph
parent[v] ← NIL for all v in V
parent[s] ← s
create an empty queue Q
enqueue(Q, s)
while Q not empty and parent[t] = NIL:
u ← dequeue(Q)
for each v in adj[u]:
residual_cap ← capacity[u][v] - flow[u][v]
if residual_cap > 0 and parent[v] = NIL:
parent[v] ← u
enqueue(Q, v)
# If no path to t, we are done
if parent[t] = NIL:
break
# 2. Compute bottleneck capacity along the path s → ... → t
path_flow ← +∞
v ← t
while v ≠ s:
u ← parent[v]
path_flow ← min(path_flow, capacity[u][v] - flow[u][v])
v ← u
# 3. Augment flow along this path
v ← t
while v ≠ s:
u ← parent[v]
flow[u][v] ← flow[u][v] + path_flow
flow[v][u] ← flow[v][u] - path_flow # maintain antisymmetry
v ← u
max_flow ← max_flow + path_flow
return max_flow, flow
Explanation of Steps
Residual capacity check: residual_cap = capacity[u][v] - flow[u][v] ensures we only traverse edges that can still carry additional flow.
Backward edges:
They are implicitly present via flow[v][u] being possibly negative or positive, and capacity[v][u] typically initialized to 0. The residual capacity from v to u is capacity[v][u] - flow[v][u], which is effectively flow[u][v] if capacity[v][u] = 0.
Augmentation:
For each edge on the found path, we:
Increase forward flow by path_flow
Decrease the reverse flow by path_flow (enabling possible later “undo” via residual edges)
Complexity
Each BFS runs in ( O(|E|) ).
Edmonds–Karp guarantees that the number of augmentations is ( O(|V|\cdot|E|) ).
Total time complexity:
[
O(|V| \cdot |E|^2)
]
This is a complete Edmonds–Karp algorithm to compute the maximum flow in a directed network.