Ford-Fulkerson and Bad Path Choices
The max-flow min-cut theorem says that a flow is maximum exactly when its residual network has no augmenting path. That suggests the Ford-Fulkerson method (1956): start from the zero flow and augment along any augmenting path until none is left. It is called a method rather than an algorithm because it does not say which path to take, and that choice decides everything: with integer capacities it always terminates, but a poor choice can make it take time proportional to the value of the flow instead of the size of the graph. The DSA course shows the method by example.
The method
FORD-FULKERSON(G, s, t)
1 for each edge (u, v) in G.E: f(u, v) = 0
2 while there is an augmenting path p from s to t in the residual network G_f
3 c_f(p) = min { c_f(u, v) : (u, v) on p }
4 for each edge (u, v) on p
5 if (u, v) in G.E
6 f(u, v) = f(u, v) + c_f(p) // forward edge: push
7 else
8 f(v, u) = f(v, u) - c_f(p) // backward edge: cancel
9 return fCorrectness. Each augmentation produces a valid flow of larger value (residual networks). When the loop stops there is no augmenting path, so by the max-flow min-cut theorem the flow is maximum.
Termination and running time with integer capacities. If all capacities are integers, every residual capacity and every bottleneck is a positive integer, so each augmentation raises by at least 1. A maximum flow is therefore reached after at most augmentations. Each one finds a path by BFS or DFS in time (every vertex lies on an - path, so ), for a total of .
Integrality theorem. With integer capacities, Ford-Fulkerson returns a maximum flow in which every is an integer: it starts at 0 and only ever adds or subtracts integers. So some maximum flow is integral, a fact that makes flows useful for combinatorial problems such as bipartite matching.
A version with depth-first search
The program finds augmenting paths by DFS, following residual edges in alphabetical order, on the network of Flow Networks, and prints each augmentation.
cap = {("S", "A"): 10, ("S", "B"): 7, ("A", "B"): 4, ("A", "C"): 5, ("B", "D"): 9,
("C", "B"): 3, ("C", "T"): 8, ("D", "C"): 4, ("D", "T"): 6}
flow = {e: 0 for e in cap}
def rc(u, v):
return cap[(u, v)] - flow[(u, v)] if (u, v) in cap else flow.get((v, u), 0)
def neighbours(u):
return sorted({b for (a, b) in cap if a == u} | {a for (a, b) in cap if b == u})
def dfs_path(u, t, seen):
if u == t:
return [t]
seen.add(u)
for v in neighbours(u):
if v not in seen and rc(u, v) > 0:
rest = dfs_path(v, t, seen)
if rest:
return [u] + rest
return None
value = rounds = 0
while (p := dfs_path("S", "T", set())):
b = min(rc(u, v) for u, v in zip(p, p[1:]))
for u, v in zip(p, p[1:]):
if (u, v) in cap: flow[(u, v)] += b
else: flow[(v, u)] -= b
value += b; rounds += 1
print(f"augment {''.join(p)} by {b}: value {value}")
print(f"maximum flow {value} after {rounds} augmentations")augment SABDCT by 4: value 4
augment SACBDT by 3: value 7
augment SACDT by 2: value 9
augment SBCDT by 1: value 10
augment SBCT by 2: value 12
augment SBDCT by 2: value 14
maximum flow 14 after 6 augmentations
DFS takes long, winding paths, but the final value, 14, is the same as with the shortest paths in the animation of that lesson; only the number and shape of the augmentations differ.
When the choice of paths is bad
The network below has four edges of capacity and a single edge of capacity 1 across the middle. The maximum flow is : send along the top and along the bottom. Shortest paths find it in two augmentations, as the animation shows for :
An unlucky choice uses the middle edge every time: with bottleneck 1, then , cancelling that unit on the middle edge, again with bottleneck 1, and so on. Each augmentation adds just 1, so it takes augmentations. The program plays both strategies for growing .
def run(M, choose):
cap = {("S", "X"): M, ("S", "Y"): M, ("X", "Y"): 1, ("X", "T"): M, ("Y", "T"): M}
flow = {e: 0 for e in cap}
def rc(u, v):
return cap[(u, v)] - flow[(u, v)] if (u, v) in cap else flow.get((v, u), 0)
candidates = ["SXT", "SYT", "SXYT", "SYXT"]
rounds = 0
while True:
usable = [p for p in candidates if all(rc(u, v) > 0 for u, v in zip(p, p[1:]))]
if not usable:
break
p = choose(usable, rounds)
b = min(rc(u, v) for u, v in zip(p, p[1:]))
for u, v in zip(p, p[1:]):
if (u, v) in cap: flow[(u, v)] += b
else: flow[(v, u)] -= b
rounds += 1
return flow[("S", "X")] + flow[("S", "Y")], rounds
shortest = lambda usable, r: min(usable, key=len)
def adversary(usable, r): # zig-zag through the middle edge whenever possible
long_paths = [p for p in usable if len(p) == 4]
if long_paths:
return long_paths[0] if len(long_paths) == 1 else ("SXYT" if r % 2 == 0 else "SYXT")
return usable[0]
print(f"{'M':>7} {'shortest paths':>15} {'zig-zag':>10}")
for M in [10, 100, 1000, 10000]:
v1, r1 = run(M, shortest)
v2, r2 = run(M, adversary)
assert v1 == v2 == 2 * M
print(f"{M:>7} {r1:>15} {r2:>10}") M shortest paths zig-zag
10 2 20
100 2 200
1000 2 2000
10000 2 20000
The graph has 4 vertices and 5 edges whatever is, yet the zig-zag strategy needs augmentations: the running time is exponential in the number of bits of the input, since writing down takes only bits. The bound is pseudo-polynomial, like the knapsack DP.
Note: With irrational capacities it is worse: there are networks where Ford-Fulkerson with a bad choice of paths never terminates, and the values of its flows converge to something smaller than the maximum. Integer (or rational) capacities avoid that, and choosing paths well avoids the dependence on altogether: that is Edmonds-Karp, the next lesson.
In C++
The C++ version keeps the residual capacities in one matrix (the forward and backward amounts of every pair) and finds paths with an iterative DFS.
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
int main() {
const std::string names = "ABCDST"; // alphabetical, as in the Python version
const int n = 6, s = 4, t = 5;
auto id = [&](char c) { return static_cast<int>(names.find(c)); };
std::vector<std::vector<int>> r(n, std::vector<int>(n, 0)); // residual capacities
struct E { char u, v; int c; };
for (E e : {E{'S','A',10}, E{'S','B',7}, E{'A','B',4}, E{'A','C',5}, E{'B','D',9},
E{'C','B',3}, E{'C','T',8}, E{'D','C',4}, E{'D','T',6}})
r[id(e.u)][id(e.v)] = e.c;
int value = 0, rounds = 0;
while (true) {
std::vector<int> prev(n, -1), next_v(n, 0), stack = {s};
prev[s] = s;
while (!stack.empty() && prev[t] < 0) { // DFS, smallest index first
int u = stack.back();
int& v = next_v[u];
while (v < n && (prev[v] >= 0 || r[u][v] == 0)) ++v;
if (v == n) { stack.pop_back(); continue; }
prev[v] = u;
stack.push_back(v);
}
if (prev[t] < 0) break;
int b = 1 << 30;
for (int v = t; v != s; v = prev[v]) b = std::min(b, r[prev[v]][v]);
std::string path;
for (int v = t; v != s; v = prev[v]) { r[prev[v]][v] -= b; r[v][prev[v]] += b; path = names[v] + path; }
value += b;
++rounds;
std::printf("augment S%s by %d: value %d\n", path.c_str(), b, value);
}
std::printf("maximum flow %d after %d augmentations\n", value, rounds);
}augment SABDCT by 4: value 4
augment SACBDT by 3: value 7
augment SACDT by 2: value 9
augment SBCDT by 1: value 10
augment SBCT by 2: value 12
augment SBDCT by 2: value 14
maximum flow 14 after 6 augmentations
Test yourself
Why does Ford-Fulkerson terminate when all capacities are integers?
- Because it uses BFS
- Because every bottleneck is a positive integer, so each augmentation raises the value by at least 1, and the value is bounded by any cut's capacity
- Because the graph is acyclic
- It does not always terminate
What is the running time of Ford-Fulkerson with integer capacities and DFS paths?
In the network with four edges of capacity and a middle edge of capacity 1, how many augmentations can a bad path choice need?
- 2
What does the integrality theorem guarantee?
- Every maximum flow is integral
- With integer capacities, there is a maximum flow with integer values on every edge, and Ford-Fulkerson finds one
- Flows are always integers
- Capacities can be rounded