Residual Networks, Augmenting Paths and Cuts
The obvious way to find a large flow is greedy: find a path from to with spare capacity, push as much as fits, repeat. The obvious way gets stuck. A path chosen early can block better routes, and no further path from to has spare capacity although the flow is not maximum. The repair is to allow a later path to send flow backwards along an edge, cancelling some of what an earlier path pushed. The residual network makes that precise, and cuts tell us when no improvement is possible. Together they are the three ideas behind the Ford-Fulkerson method.
The residual network
Given a flow network and a flow , the residual capacity of a pair of vertices is
Because has no antiparallel edges, at most one case applies. The residual network has an edge for every pair with . Each edge of gives rise to at most two residual edges: a forward edge with the remaining capacity and a backward edge with the current flow. So .
An augmenting path is a simple path from to in . Its residual capacity, or bottleneck, is . Augmenting along pushes units along the path: on a forward edge the flow increases, on a backward edge the flow of the original edge decreases. The result is again a flow, and its value is .
Why augmenting gives a flow
Capacity. On a forward edge the flow grows by at most the remaining room, on a backward edge it shrinks by at most the current flow, so it stays in . Conservation. An inner vertex of the path has one path edge entering and one leaving. Each of the four combinations (forward in, forward out; forward in, backward out; and so on) changes 's inflow and outflow by the same amount, or changes neither and moves units from one entering edge to another. Value. The first edge of leaves , and since a simple path never returns to , it is a forward edge out of (or a backward edge into ): either way the net flow out of grows by .
An example where a path must cancel flow
In this network, Edmonds-Karp (shortest augmenting paths, a later lesson) first sends 2 units along and 2 along . The edge then carries flow that would be better used by the route from . The third augmenting path goes , then backwards from to , cancelling one unit on , then on to and .
The program builds the residual network after the first two augmentations and finds the third path with a breadth-first search in it.
from collections import deque
cap = {("S", "A"): 4, ("S", "C"): 3, ("A", "B"): 3, ("A", "D"): 6, ("C", "B"): 4, ("B", "T"): 2, ("D", "T"): 3}
flow = {e: 0 for e in cap}
for path, amount in [("SABT", 2), ("SADT", 2)]: # the first two augmentations
for u, v in zip(path, path[1:]):
flow[(u, v)] += amount
def residual(cap, flow):
res = {}
for (u, v), c in cap.items():
if c - flow[(u, v)] > 0:
res[(u, v)] = ("forward", c - flow[(u, v)])
if flow[(u, v)] > 0:
res[(v, u)] = ("backward", flow[(u, v)])
return res
res = residual(cap, flow)
print("residual edges:")
for (u, v), (kind, r) in sorted(res.items()):
print(f" {u}->{v}: {r} ({kind})")
def augmenting_path(res, s, t):
prev, q = {s: None}, deque([s])
while q and t not in prev:
u = q.popleft()
for (a, b) in sorted(res):
if a == u and b not in prev:
prev[b] = u
q.append(b)
if t not in prev:
return None
path, v = [t], t
while prev[v]:
v = prev[v]
path.append(v)
return path[::-1]
p = augmenting_path(res, "S", "T")
b = min(res[(u, v)][1] for u, v in zip(p, p[1:]))
print("augmenting path:", " -> ".join(p), "bottleneck", b)
for u, v in zip(p, p[1:]):
if (u, v) in cap:
flow[(u, v)] += b
else:
flow[(v, u)] -= b # cancel flow on the original edge
print("new flow:", {f"{u}{v}": x for (u, v), x in flow.items()})
print("value:", flow[("S", "A")] + flow[("S", "C")])
print("another augmenting path?", augmenting_path(residual(cap, flow), "S", "T"))residual edges:
A->B: 1 (forward)
A->D: 4 (forward)
A->S: 4 (backward)
B->A: 2 (backward)
C->B: 4 (forward)
D->A: 2 (backward)
D->T: 1 (forward)
S->C: 3 (forward)
T->B: 2 (backward)
T->D: 2 (backward)
augmenting path: S -> C -> B -> A -> D -> T bottleneck 1
new flow: {'SA': 4, 'SC': 1, 'AB': 1, 'AD': 3, 'CB': 1, 'BT': 2, 'DT': 3}
value: 5
another augmenting path? None
After the augmentation, carries 1 unit instead of 2, 's unit reaches through , and the freed unit at goes through . No augmenting path remains. Is the flow of value 5 the maximum? Cuts answer that.
Cuts
A cut of a flow network is a partition of into and with and . (The sets are written like the vertex names in the example; context tells them apart.) The net flow across it and its capacity are
The capacity counts only edges from to ; the net flow subtracts the flow on edges going back.
Lemma. For every flow and every cut , .
Sum the conservation equations of all vertices in other than (each says: out minus in is 0) and add the definition of (out of minus into ). The result is "flow out of -vertices minus flow into -vertices", summed over all of . Every edge with both ends in appears once with and once with and cancels, leaving exactly the edges that cross the cut: .
Corollary. For every flow and every cut , .
Because , using on the backward edges and the capacity constraint on the forward ones. So every cut's capacity is an upper bound on every flow's value. The program lists all cuts of the example and checks the lemma for the final flow.
from itertools import combinations
cap = {("S", "A"): 4, ("S", "C"): 3, ("A", "B"): 3, ("A", "D"): 6, ("C", "B"): 4, ("B", "T"): 2, ("D", "T"): 3}
flow = {("S", "A"): 4, ("S", "C"): 1, ("A", "B"): 1, ("A", "D"): 3, ("C", "B"): 1, ("B", "T"): 2, ("D", "T"): 3}
inner = "ABCD"
rows = []
for k in range(len(inner) + 1):
for extra in combinations(inner, k):
S = {"S", *extra}
net = sum(f for (u, v), f in flow.items() if u in S and v not in S) - \
sum(f for (u, v), f in flow.items() if v in S and u not in S)
capacity = sum(c for (u, v), c in cap.items() if u in S and v not in S)
rows.append((capacity, "".join(sorted(S)), net))
for capacity, S, net in sorted(rows)[:6]:
print(f"S-side {{{', '.join(S)}}}: capacity {capacity:2}, net flow {net}")
print("...")
print("net flow across all 16 cuts:", sorted({net for _, _, net in rows}))
print("smallest capacity:", min(rows)[0])S-side {A, B, C, D, S}: capacity 5, net flow 5
S-side {B, C, S}: capacity 6, net flow 5
S-side {S}: capacity 7, net flow 5
S-side {A, B, C, S}: capacity 8, net flow 5
S-side {A, B, D, S}: capacity 8, net flow 5
S-side {C, S}: capacity 8, net flow 5
...
net flow across all 16 cuts: [5]
smallest capacity: 5
Every one of the 16 cuts carries net flow 5, as the lemma says, and the smallest capacity is also 5: the cut with -side , crossed only by the two edges into . Since no flow can exceed a cut's capacity, the flow of value 5 is maximum. The next lesson proves that this always happens: when no augmenting path exists, some cut is exactly full.
In C++
The C++ version stores capacities and flows in matrices, so the residual capacity of any pair is one expression, and finds an augmenting path by BFS.
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
int main() {
const std::string names = "SABCDT";
const int n = 6;
auto id = [&](char c) { return static_cast<int>(names.find(c)); };
std::vector<std::vector<int>> cap(n, std::vector<int>(n, 0)), flow = cap;
for (auto [u, v, c] : std::vector<std::tuple<char, char, int>>{{'S','A',4}, {'S','C',3}, {'A','B',3},
{'A','D',6}, {'C','B',4}, {'B','T',2}, {'D','T',3}})
cap[id(u)][id(v)] = c;
auto residual = [&](int u, int v) { return cap[u][v] - flow[u][v] + flow[v][u]; };
for (int round = 1;; ++round) {
std::vector<int> prev(n, -1);
prev[0] = 0;
std::queue<int> q;
q.push(0);
while (!q.empty() && prev[5] < 0) {
int u = q.front(); q.pop();
for (int v = 0; v < n; ++v)
if (prev[v] < 0 && residual(u, v) > 0) { prev[v] = u; q.push(v); }
}
if (prev[5] < 0) break;
int b = 1 << 30;
for (int v = 5; v != 0; v = prev[v]) b = std::min(b, residual(prev[v], v));
std::string path;
for (int v = 5; v != 0; v = prev[v]) {
int u = prev[v];
int cancel = std::min(b, flow[v][u]); // use a backward edge first
flow[v][u] -= cancel;
flow[u][v] += b - cancel;
path = names[v] + path;
}
std::printf("round %d: %c%s, bottleneck %d\n", round, 'S', path.c_str(), b);
}
int value = flow[0][1] + flow[0][3];
std::printf("maximum flow %d\n", value);
}round 1: SABT, bottleneck 2
round 2: SADT, bottleneck 2
round 3: SCBADT, bottleneck 1
maximum flow 5
The matrix BFS scans vertices in index order , so it finds the same three paths as the animation.
Test yourself
Edge has capacity 7 and carries flow 5. Which residual edges does it produce?
- Only with capacity 2
- with capacity 2 and with capacity 5
- with capacity 7
- with capacity 2
What happens to the flow when an augmenting path uses a backward residual edge ?
- Flow is added to a new edge
- The flow on the original edge decreases by the bottleneck
- The path is rejected
- The capacity of grows
A flow has value 9. What can you say about the capacity of any cut ?
- It is exactly 9
- It is at least 9
- It is at most 9
- Nothing
Why does the capacity of a cut count only edges from to ?
- Edges from to have no capacity
- Flow on edges from back to only reduces the net flow across the cut, so it cannot help a flow exceed the bound
- By convention only
- Because the network has no such edges