TExcelTAD
Home › Graph Algorithms › Residual Networks, Augmenting Paths and Cuts

Residual Networks, Augmenting Paths and Cuts

The obvious way to find a large flow is greedy: find a path from ss to tt 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 ss to tt 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 GG and a flow ff, the residual capacity of a pair of vertices is

cf(u,v)={c(u,v)−f(u,v)if (u,v)∈E(room left on the edge)f(v,u)if (v,u)∈E(flow that can be cancelled)0otherwisec_f(u, v) = \begin{cases} c(u, v) - f(u, v) & \text{if } (u, v) \in E \quad \text{(room left on the edge)} \\ f(v, u) & \text{if } (v, u) \in E \quad \text{(flow that can be cancelled)} \\ 0 & \text{otherwise} \end{cases}

Because GG has no antiparallel edges, at most one case applies. The residual network Gf=(V,Ef)G_f = (V, E_f) has an edge for every pair with cf(u,v)>0c_f(u, v) > 0. Each edge of GG gives rise to at most two residual edges: a forward edge with the remaining capacity and a backward edge with the current flow. So ∣Ef∣≤2∣E∣\lvert E_f \rvert \le 2 \lvert E \rvert.

An augmenting path is a simple path from ss to tt in GfG_f. Its residual capacity, or bottleneck, is cf(p)=min⁡{cf(u,v):(u,v) on p}c_f(p) = \min \{ c_f(u, v) : (u, v) \text{ on } p \}. Augmenting ff along pp pushes cf(p)c_f(p) units along the path: on a forward edge the flow increases, on a backward edge (v,u)(v, u) the flow of the original edge (u,v)(u, v) decreases. The result is again a flow, and its value is ∣f∣+cf(p)>∣f∣\lvert f \rvert + c_f(p) > \lvert f \rvert.

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 [0,c(u,v)][0, c(u, v)]. Conservation. An inner vertex vv 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 vv's inflow and outflow by the same amount, or changes neither and moves cf(p)c_f(p) units from one entering edge to another. Value. The first edge of pp leaves ss, and since a simple path never returns to ss, it is a forward edge out of ss (or a backward edge into ss): either way the net flow out of ss grows by cf(p)c_f(p).

An example where a path must cancel flow

In this network, Edmonds-Karp (shortest augmenting paths, a later lesson) first sends 2 units along S→A→B→TS \to A \to B \to T and 2 along S→A→D→TS \to A \to D \to T. The edge A→BA \to B then carries flow that would be better used by the route from CC. The third augmenting path goes S→C→BS \to C \to B, then backwards from BB to AA, cancelling one unit on A→BA \to B, then on to DD and TT.

The program builds the residual network after the first two augmentations and finds the third path with a breadth-first search in it.

python · try it yourself
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, A→BA \to B carries 1 unit instead of 2, CC's unit reaches TT through BB, and the freed unit at AA goes through DD. No augmenting path remains. Is the flow of value 5 the maximum? Cuts answer that.

Cuts

A cut (S,T)(S, T) of a flow network is a partition of VV into SS and T=V∖ST = V \setminus S with s∈Ss \in S and t∈Tt \in T. (The sets are written S,TS, T like the vertex names in the example; context tells them apart.) The net flow across it and its capacity are

f(S,T)=∑u∈S∑v∈Tf(u,v)−∑u∈S∑v∈Tf(v,u),c(S,T)=∑u∈S∑v∈Tc(u,v)f(S, T) = \sum_{u \in S} \sum_{v \in T} f(u, v) - \sum_{u \in S} \sum_{v \in T} f(v, u), \qquad c(S, T) = \sum_{u \in S} \sum_{v \in T} c(u, v)

The capacity counts only edges from SS to TT; the net flow subtracts the flow on edges going back.

Lemma. For every flow ff and every cut (S,T)(S, T), f(S,T)=∣f∣f(S, T) = \lvert f \rvert.

Sum the conservation equations of all vertices in SS other than ss (each says: out minus in is 0) and add the definition of ∣f∣\lvert f \rvert (out of ss minus into ss). The result is "flow out of SS-vertices minus flow into SS-vertices", summed over all of SS. Every edge with both ends in SS appears once with ++ and once with −- and cancels, leaving exactly the edges that cross the cut: f(S,T)f(S, T).

Corollary. For every flow ff and every cut (S,T)(S, T), ∣f∣≤c(S,T)\lvert f \rvert \le c(S, T).

Because ∣f∣=f(S,T)≤∑f(u,v)≤∑c(u,v)=c(S,T)\lvert f \rvert = f(S, T) \le \sum f(u, v) \le \sum c(u, v) = c(S, T), using f≥0f \ge 0 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 24=162^4 = 16 cuts of the example and checks the lemma for the final flow.

python · try it yourself
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 SS-side {S,A,B,C,D}\{S, A, B, C, D\}, crossed only by the two edges into TT. 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.

cpp · try it yourself
#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 S,A,B,C,D,TS, A, B, C, D, T, so it finds the same three paths as the animation.

Test yourself

Edge (u,v)(u, v) has capacity 7 and carries flow 5. Which residual edges does it produce?

  1. Only (u,v)(u, v) with capacity 2
  2. (u,v)(u, v) with capacity 2 and (v,u)(v, u) with capacity 5
  3. (u,v)(u, v) with capacity 7
  4. (v,u)(v, u) with capacity 2

What happens to the flow when an augmenting path uses a backward residual edge (v,u)(v, u)?

  1. Flow is added to a new edge (v,u)(v, u)
  2. The flow on the original edge (u,v)(u, v) decreases by the bottleneck
  3. The path is rejected
  4. The capacity of (u,v)(u, v) grows

A flow has value 9. What can you say about the capacity of any cut (S,T)(S, T)?

  1. It is exactly 9
  2. It is at least 9
  3. It is at most 9
  4. Nothing

Why does the capacity of a cut count only edges from SS to TT?

  1. Edges from TT to SS have no capacity
  2. Flow on edges from TT back to SS only reduces the net flow across the cut, so it cannot help a flow exceed the bound
  3. By convention only
  4. Because the network has no such edges