Sub topic 5
The Ford-Fulkerson Method
This chapter assumes you already know what a flow network is, how to build the residual graph for a flow, and how to find an augmenting path and its bottleneck. You should also be able to read a recursive depth-first search (DFS) in Python.
The idea in one loop
Ford-Fulkerson puts the earlier pieces together into one loop:
- Start with zero flow on every edge.
- Look for any path from the source
sto the sinktin the residual graph. - If no such path exists, stop. The current flow is the answer.
- Otherwise, push the path's bottleneck amount along it. Subtract it from each forward residual capacity and add it to each reverse one.
- Go back to step 2.
This is called a method and not an algorithm because step 2 does not say how to find the path. DFS, BFS, or a random choice all count as Ford-Fulkerson. That choice turns out to matter a lot for running time, as later chapters show.
Two invariants that make it correct so far
Each augmentation keeps two properties true:
- The flow stays feasible. The bottleneck is the smallest residual capacity on the path, so no edge goes over its capacity. Every internal vertex on the path gets exactly as much new flow in as it sends out, so conservation still holds.
- The flow value strictly increases. The path leaves
sand never comes back to it, so the net flow out ofsgrows by the bottleneck, which is always positive.
These two facts do not prove that the final flow is maximum, and they do not prove that the loop ever stops. The max-flow min-cut theorem covers optimality, and the chapter on slow cases covers termination.
A DFS implementation
The version below uses an adjacency matrix and finds paths with recursive DFS. The DFS carries the smallest capacity seen so far (pushed) down the path. When it reaches t, it updates the residual edges as the recursion unwinds.
def ford_fulkerson(capacity, s, t):
n = len(capacity)
residual = [row[:] for row in capacity]
def dfs(u, pushed, visited):
if u == t:
return pushed
visited[u] = True
for v in range(n):
if not visited[v] and residual[u][v] > 0:
d = dfs(v, min(pushed, residual[u][v]), visited)
if d > 0:
residual[u][v] -= d
residual[v][u] += d
return d
return 0
max_flow = 0
while True:
pushed = dfs(s, float('inf'), [False] * n)
if pushed == 0:
return max_flow
max_flow += pushed
# Nodes: 0 = s, 1..4 = intermediate, 5 = t
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],
]
print(ford_fulkerson(capacity, 0, 5)) # 23Common errors
| Symptom | Likely cause |
|---|---|
| Result is too small | Reverse residual edges are never increased |
| Infinite loop | `visited` is not reset, or a path with zero bottleneck is accepted |
| Result is too large | Augmenting by an edge's capacity instead of the path bottleneck |
Exercise
Run the DFS above on the example network by hand. Which augmenting path does the first iteration find, and what is its bottleneck?
Show answer
The DFS checks neighbors in increasing index order, so it follows 0 → 1 → 3 → 2 → 4 → 5. The residual capacities along that path are 16, 12, 9, 14, and 4, so the bottleneck is 4.
Exercise
Why does each augmentation raise the flow value by exactly the bottleneck amount?
Show answer
A simple s-t path uses exactly one edge leaving s and no edge entering s. Pushing the bottleneck along it therefore adds exactly that amount to the net flow out of s.