Sub topic 3
The Residual Graph and Why Reverse Edges Matter
This chapter assumes you know the material from the previous two chapters. That means a flow network with capacities c(u, v), a source s and a sink t, and a feasible flow f that satisfies the capacity and conservation constraints. It also assumes you can read an adjacency matrix and write basic Python.
The question here is this: given a flow that is valid but maybe not maximum, where can we still send more flow? The residual graph answers it. It records how much each edge can still take. It also records how much flow could be pushed back, which turns out to matter just as much.
Why Greedy Pushing Fails
Here is a simple first try. Find any path from s to t whose edges all have spare capacity, push as much flow as the path allows, and repeat until no such path exists. The problem is that an early choice can block later paths, and this greedy method has no way to undo a choice.
Take this four-node network, where every edge has capacity 1:
| Edge | Capacity |
|---|---|
| s → a | 1 |
| s → b | 1 |
| a → b | 1 |
| a → t | 1 |
| b → t | 1 |
The maximum flow is 2: send one unit along s → a → t and one along s → b → t. Now suppose the greedy method first picks the path s → a → b → t and pushes 1 unit. After that, s → a is full and b → t is full. The only edge still leaving s with room is s → b, and every edge leaving b toward t is full. The method stops with a flow of 1.
The mistake was sending flow across a → b. The fix is to allow a later path to cancel that flow. If one unit enters b from s and then "goes backward" over a → b to reach a, the unit that was crossing a → b gets rerouted. The net effect is that a → b carries 0 and both a → t and b → t carry 1. Reverse edges in the residual graph are how we represent that cancellation.
Residual Capacity
For a flow f on a network with capacities c, the residual capacity of each ordered pair (u, v) has two parts:
- Forward room: if
u → vis an edge, it can carryc(u, v) - f(u, v)more units. - Cancellable flow: if
v → uis an edge carryingf(v, u)units, we can cancel up tof(v, u)of them. Cancelling is the same as moving flow fromutov.
When the network contains no antiparallel pairs (both u → v and v → u), only one part applies to each pair. In general you add the two parts:
c_f(u, v) = c(u, v) - f(u, v) + f(v, u)
Here a missing edge counts as having capacity 0 and flow 0. The residual graph G_f has the same vertices as the network. It has an edge u → v exactly when c_f(u, v) > 0, labeled with that residual capacity.
Each original edge u → v with flow f adds up to two residual edges:
| State of original edge u → v | Forward residual u → v | Backward residual v → u |
|---|---|---|
| Unused (f = 0) | c | none (0) |
| Partly used (0 < f < c) | c − f | f |
| Saturated (f = c) | none (0) | c |
Since every original edge adds at most two residual edges, G_f has at most 2E edges. That fact comes back when we analyze running time.
The Residual Graph for Our Example
Apply the table to the blocked flow above (s → a, a → b and b → t each carry 1):
s → ais saturated, so the only residual edge isa → swith capacity 1.s → bis unused, so it stays ass → bwith capacity 1.a → bis saturated, so the only residual edge isb → awith capacity 1.a → tis unused, so it stays asa → twith capacity 1.b → tis saturated, so the only residual edge ist → bwith capacity 1.
The residual graph now has the path s → b → a → t, and its middle step is the backward edge b → a. Pushing one unit along it cancels the flow on a → b, and the total becomes 2. Finding such paths and pushing along them is the subject of the next chapter. For now, the point is that the path exists only because of the reverse edge.
Computing the Residual Graph in Python
The code below builds the residual matrix straight from the formula. It lists the residual edges, pushes one unit along s → b → a → t, and then reads the new flow back out of the residual matrix.
def residual_matrix(capacity, flow):
# c_f(u, v) = c(u, v) - f(u, v) + f(v, u)
n = len(capacity)
return [[capacity[u][v] - flow[u][v] + flow[v][u] for v in range(n)]
for u in range(n)]
def residual_edges(res):
n = len(res)
return [(u, v, res[u][v]) for u in range(n) for v in range(n) if res[u][v] > 0]
def push_along(res, path, amount):
# Pushing on u -> v uses up residual room on u -> v
# and creates the same amount of room on v -> u
for u, v in zip(path, path[1:]):
res[u][v] -= amount
res[v][u] += amount
def flow_from_residual(capacity, res):
# c(u, v) - c_f(u, v) = f(u, v) - f(v, u), the net flow from u to v
n = len(capacity)
return [[max(0, capacity[u][v] - res[u][v]) for v in range(n)]
for u in range(n)]
# Nodes: 0 = s, 1 = a, 2 = b, 3 = t
capacity = [
[0, 1, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1],
[0, 0, 0, 0],
]
flow = [
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 0, 0, 0],
]
res = residual_matrix(capacity, flow)
print(residual_edges(res))
# [(0, 2, 1), (1, 0, 1), (1, 3, 1), (2, 1, 1), (3, 2, 1)]
push_along(res, [0, 2, 1, 3], 1) # s -> b -> a -> t
new_flow = flow_from_residual(capacity, res)
print(new_flow)
# [[0, 1, 1, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 0]]
print(sum(new_flow[0])) # 2In the new flow, a → b carries 0. The push over the backward edge cancelled it, as intended. Note also that push_along changes only the residual matrix and never touches flow. The Edmonds-Karp code later in this tutorial works the same way. It stores only the residual matrix and treats the flow as implied by it.
Antiparallel Edges
Suppose a network has both u → v with capacity 5 and v → u with capacity 3. A matrix stores one number per ordered pair, so the forward room of u → v and the cancellable flow of v → u must share the cell res[u][v]. The summed formula handles this correctly. With f(u, v) = 2 and f(v, u) = 0:
c_f(u, v) = 5 - 2 + 0 = 3c_f(v, u) = 3 - 0 + 2 = 5
The cost is that the residual matrix records only net flow. If you read flows back with flow_from_residual, it will report u → v carrying 2 and v → u carrying 0, even if the original flow had 4 one way and 2 the other. Both describe the same net movement of flow, and both are valid flows of the same value. Adjacency-list implementations store each edge next to its own reverse edge. That keeps parallel and antiparallel edges separate, and a later chapter covers it.
Common Errors
- No reverse edges. A method that tracks only forward room is the greedy method above. It can stop early, below the maximum.
- Wrong capacity on the reverse edge. A backward residual edge has capacity
f(u, v), the flow on the edge. It does not have capacityc(u, v). Using the capacity lets you cancel flow that was never sent. - Overwriting antiparallel entries. If you set
res[v][u] = f(u, v)instead of adding to it, you erase the capacity of a real edgev → u. - Walking zero-capacity edges. A search over the residual graph must skip pairs with
res[u][v] == 0. Otherwise it finds "paths" that can carry nothing. - Reading a backward push as real flow. After a push over
b → a, no flow goes frombtoa. The flow ona → bwent down.
Exercise
An edge u → v has capacity 10 and carries 7 units, and there is no edge v → u. Which residual edges does it produce, and with what capacities? What changes if the flow rises to 10?
Show answer
It produces u → v with residual capacity 3 and v → u with residual capacity 7. At flow 10 the edge is saturated, so the forward residual edge disappears (capacity 0) and only v → u remains, with capacity 10.
Exercise
A network has u → v with capacity 4 and v → u with capacity 6. The current flow has f(u, v) = 4 and f(v, u) = 1. Compute cf(u, v) and cf(v, u), and check that their sum equals c(u, v) + c(v, u).
Show answer
cf(u, v) = 4 − 4 + 1 = 1 and cf(v, u) = 6 − 1 + 4 = 9. Their sum is 10, which equals 4 + 6.
Exercise
In the four-node example, suppose the first push had gone along s → a → t instead of s → a → b → t. List the residual edges, and say whether the second unit of flow needs a backward edge.
Show answer
The flow is s → a = 1 and a → t = 1. The residual edges are a → s (1), s → b (1), a → b (1), t → a (1) and b → t (1). The path s → b → t uses only forward edges, so no backward edge is needed. Reverse edges matter when an earlier choice was bad. A good order of choices may never use them, but the method cannot know that order in advance.