Sub topic 3
Implementing Edmonds-Karp with Paired Edges
This chapter assumes you know BFS, adjacency lists, Big-O notation, and the residual graph and augmenting paths from the earlier chapters.
Paired Forward and Reverse Edges
An adjacency matrix makes each BFS cost O(V²). An adjacency list cuts that to O(V + E). In this version, every edge is stored next to its reverse. If edge e is at an even index, its reverse is at e ^ 1. Pushing flow means doing cap[e] -= b and cap[e ^ 1] += b. Each edge has its own index, so parallel edges cause no problems.
from collections import deque
class MaxFlow:
def __init__(self, n):
self.n = n
self.graph = [[] for _ in range(n)]
self.to, self.cap = [], []
def add_edge(self, u, v, c):
self.graph[u].append(len(self.to)); self.to.append(v); self.cap.append(c)
self.graph[v].append(len(self.to)); self.to.append(u); self.cap.append(0)
def max_flow(self, s, t):
flow = 0
while True:
parent_edge = [-1] * self.n
visited = [False] * self.n
visited[s] = True
queue = deque([s])
while queue and not visited[t]:
u = queue.popleft()
for e in self.graph[u]:
v = self.to[e]
if not visited[v] and self.cap[e] > 0:
visited[v] = True
parent_edge[v] = e
queue.append(v)
if not visited[t]:
return flow
b, v = float('inf'), t
while v != s:
e = parent_edge[v]; b = min(b, self.cap[e]); v = self.to[e ^ 1]
v = t
while v != s:
e = parent_edge[v]; self.cap[e] -= b; self.cap[e ^ 1] += b; v = self.to[e ^ 1]
flow += b
def max_bipartite_matching(left, right, pairs):
n = left + right + 2
s, t = 0, n - 1
mf = MaxFlow(n)
for i in range(left):
mf.add_edge(s, 1 + i, 1)
for j in range(right):
mf.add_edge(1 + left + j, t, 1)
for i, j in pairs:
mf.add_edge(1 + i, 1 + left + j, 1)
return mf.max_flow(s, t)
print(max_bipartite_matching(3, 3, [(0, 0), (0, 1), (1, 0), (2, 1), (2, 2)])) # 3Finding the Minimum Cut
When the algorithm stops, run one more BFS from s and follow only edges with positive residual capacity. The vertices it reaches form the set S. The original edges from S to the rest of the graph form a minimum cut, and their capacities add up to the maximum flow value.
Proving the O(V E²) Bound
Because BFS always picks a shortest augmenting path, the distance from s to any vertex in the residual graph never decreases. Each augmentation saturates a bottleneck edge, called a critical edge. Before that same edge can be critical again, the distance to its tail must grow by at least 2. So each edge is critical at most O(V) times, which gives O(VE) augmentations. Each BFS costs O(E), so the total is O(VE²). The capacity values never enter the bound.
Integer Capacities Give Integer Flows
If every capacity is an integer, every bottleneck is an integer, so the maximum flow the algorithm finds is integral. Matching problems rely on this fact.
Modeling Tricks
| Problem | Transformation |
|---|---|
| Many sources or sinks | Add a super source with infinite-capacity edges to each source, and a super sink with the same kind of edges from each sink |
| Undirected edge {u, v} of capacity c | Add both u→v and v→u, each with capacity c |
| Vertex capacity c on v | Split v into v_in → v_out with an edge of capacity c |
Application: Bipartite Matching
max_bipartite_matching above gives every edge capacity 1. By integrality, the maximum flow puts 0 or 1 unit on each edge, and the left-to-right edges that carry flow form a maximum matching.
Where to Go Next
Dinic's algorithm augments along blocking flows in a BFS level graph and runs in O(V²E). On unit-capacity bipartite graphs this becomes O(E√V). Push-relabel algorithms run in O(V³) or better and are often the fastest choice on dense graphs.
Exercise
A road network limits how many trucks may pass through intersection x per hour to 5. How do you model this limit for max flow?
Show answer
Split x into xin and xout with an edge xin → xout of capacity 5. Redirect every edge entering x to xin, and start every edge that left x from xout.
Exercise
Why does the bipartite reduction never match one left vertex to two right vertices?
Show answer
The edge from the source to each left vertex has capacity 1. Conservation then lets at most 1 unit leave that vertex, and integrality means it all goes along a single edge.