Sub topic 5
The Ford-Fulkerson Method
This chapter assumes you already know three things from the earlier chapters: what a feasible flow is, how to build the residual graph with its reverse edges, and how to find an augmenting path and its bottleneck. It also assumes you can write a recursive depth-first search in Python. Here we put those pieces together into a complete procedure, run it by hand on a small network, and implement it.
The Method in One Loop
Ford-Fulkerson is a loop with three steps:
- Start with the zero flow: f(u, v) = 0 on every edge.
- While the residual graph has a path from s to t, pick any such path, find its bottleneck δ, and push δ units along it.
- When no s-t path remains in the residual graph, stop. The current flow is maximum.
People usually call Ford-Fulkerson a method rather than an algorithm, because step 2 does not say how to choose the path. DFS, BFS, a random choice, or the widest path all qualify. That choice decides how fast the method runs, and with irrational capacities it decides whether the method stops at all. Later chapters cover both questions. This chapter uses DFS, which is the simplest choice.
What Each Iteration Guarantees
Each iteration keeps two invariants. They are the reason the loop is worth running.
The flow stays feasible. A residual edge u → v has capacity c(u, v) − f(u, v) if it is a forward edge, or f(v, u) if it is a reverse edge. Pushing δ ≤ that amount cannot push a forward edge past its capacity, and it cannot cancel more flow than an edge carries. At each interior vertex of the path, δ more units enter and δ more units leave, so conservation still holds.
The flow value grows by exactly δ. The path is simple, so it leaves s once and never comes back. The net flow out of s therefore rises by δ, and δ > 0 because every residual edge on the path has positive capacity.
These invariants give you a feasible flow whose value goes up on every iteration. They do not yet show that the final flow is maximum. That proof needs cuts and comes in the max-flow min-cut chapter. For now, take the stopping rule on trust: no augmenting path means maximum flow.
A DFS Implementation
This version stores the residual capacities in an n × n matrix. The DFS carries along the smallest residual capacity it has seen so far. When it reaches t, it updates the residual matrix as the recursion unwinds, so finding the path and augmenting it happen in the same pass.
def ford_fulkerson(capacity, s, t):
# capacity is an n x n matrix; capacity[u][v] is the capacity of edge u -> v
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:
got = dfs(v, min(pushed, residual[u][v]), visited)
if got > 0:
residual[u][v] -= got # use up forward residual capacity
residual[v][u] += got # allow this flow to be cancelled later
return got
return 0
max_flow = 0
while True:
visited = [False] * n
pushed = dfs(s, float('inf'), visited)
if pushed == 0:
return max_flow, residual
max_flow += pushed
def edge_flows(capacity, residual):
# Flow on each original edge, read back from the residual matrix
n = len(capacity)
return {(u, v): capacity[u][v] - residual[u][v]
for u in range(n) for v in range(n)
if capacity[u][v] > 0 and capacity[u][v] - residual[u][v] > 0}
# 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],
]
flow, residual = ford_fulkerson(capacity, 0, 5)
print(flow) # 23
print(edge_flows(capacity, residual))
# {(0, 1): 12, (0, 2): 11, (1, 3): 12, (2, 4): 11, (3, 5): 19, (4, 3): 7, (4, 5): 4}The edge_flows helper uses the fact that on an original edge, residual = capacity − flow, so flow = capacity − residual. That formula is safe here because no pair of vertices has edges in both directions. If a pair does, capacity − residual gives the net flow between them, and you should keep only the positive side.
Tracing the Run
The DFS tries neighbors in increasing index order. On this network it finds four augmenting paths:
| Iteration | Augmenting path (residual graph) | Bottleneck δ | Flow value after |
|---|---|---|---|
| 1 | s → 1 → 3 → 2 → 4 → t | 4 | 4 |
| 2 | s → 1 → 3 → t | 8 | 12 |
| 3 | s → 2 → 3 → t | 4 | 16 |
| 4 | s → 2 → 4 → 3 → t | 7 | 23 |
On the fifth search, the DFS from s reaches only vertices 1, 2, and 4, and the method stops with value 23.
Iteration 3 is the important one. The original network has no edge 2 → 3, only 3 → 2 with capacity 9. Iteration 1 sent 4 units along 3 → 2, which created a reverse residual edge 2 → 3 with capacity 4. Iteration 3 uses that reverse edge, which cancels the 4 units on 3 → 2. In the final flow, edge 3 → 2 carries 0, and those 4 units reach t by the direct edge 3 → t instead.
The first DFS took a poor detour, and the reverse edge undid it without any backtracking logic in the code. If you left out reverse edges, this same path order would get stuck: after iteration 2, every path into t would be blocked and the code would report less than 23.
You can check conservation at vertex 3: 12 + 7 = 19 units come in and 19 + 0 = 19 go out. The saturated edges 1 → 3, 4 → 3, and 4 → t are a first hint of why 23 is optimal. A later chapter shows that these three edges form a cut of capacity 12 + 7 + 4 = 23.
Common Errors
- Resetting
visitedin the wrong place. Create a freshvisitedlist for each augmenting search and keep it for that whole search. If you clear it inside the recursion, the DFS can revisit vertices, run in exponential time, or follow non-simple paths. - Comparing with
>= 0instead of> 0. An edge with zero residual capacity is not in the residual graph. Following it gives δ = 0, and the loop either stops too early or never stops. - Treating the matrix as an edge list. The matrix cannot store two parallel edges u → v. Add their capacities into one cell, or switch to the paired-edge list from the implementation chapter.
- Deep recursion. Python's default recursion limit is about 1000, so a long path in a large graph raises
RecursionError. Use an explicit stack, or use BFS, which is what Edmonds-Karp does. - Using floats for capacities. Rounding can leave tiny positive residuals such as 1e-17, and the loop may keep pushing these near-zero amounts. Use integers when you can, or compare against a small epsilon.
Termination With Integer Capacities
With integer capacities, every residual capacity stays an integer, because the code only ever adds or subtracts integer bottlenecks. So every δ is at least 1. The flow value never exceeds the total capacity leaving s, so the loop runs at most f times, where f is the maximum flow value. Each iteration costs O(V²) with a matrix, or O(V + E) with adjacency lists. The total is O(E · f*) with lists. This bound depends on the size of the capacities, not just the size of the graph. A later chapter shows how bad that can get, and how Edmonds-Karp removes the dependence.
Exercise
Run the method by hand on this network: s → a (capacity 1), s → b (1), a → b (1), a → t (1), b → t (1). Suppose the first augmenting path chosen is s → a → b → t. Which path must the second augmentation use, and what is the final flow on edge a → b?
Show answer
After the first path, s → a, a → b, and b → t are saturated. The residual graph has s → b (1), a reverse edge b → a (1), and a → t (1). The only s-t path is s → b → a → t, which uses the reverse edge b → a and cancels the unit on a → b. The final flow is 2, with 0 units on a → b: one unit goes s → a → t and one goes s → b → t.
Exercise
The edge_flows helper computes capacity − residual. Give a two-vertex network where this formula reports the wrong flow on an individual edge, and say what the helper actually reports there.
Show answer
Take edges u → v with capacity 5 and v → u with capacity 3, and suppose 2 units flow on u → v and 0 on v → u. Then residual[u][v] = 3 and residual[v][u] = 3 + 2 = 5. The helper reports 5 − 3 = 2 for u → v, which is correct. For v → u it gets 3 − 5 = −2, which it filters out. In general the formula gives net flow, so if a flow put 2 units on u → v and 1 unit on v → u, the helper would report 1 unit on u → v. That is an equivalent feasible flow with the same value, but it is not the original edge-by-edge flow.
Exercise
All capacities are integers and the total capacity of edges leaving s is C. Give an upper bound on the number of augmentations Ford-Fulkerson performs, and explain why it holds no matter which augmenting paths are chosen.
Show answer
At most C augmentations. Residual capacities stay integers, so each bottleneck is at least 1, and each augmentation raises the flow value by at least 1. The value of any feasible flow is at most C, the capacity out of s, so there can be at most C increases.