Sub topic 3
The Residual Graph and Why Reverse Edges Matter
This chapter assumes you know what a flow network is and can check that a flow obeys the capacity and conservation constraints. It also assumes you can read an adjacency matrix in Python. Here we answer one question: given a current flow, where can more flow still go?
Residual Capacity
The residual capacity of a pair of vertices is how much more flow you could send from u to v right now. For an edge u -> v with capacity c(u, v) carrying flow f(u, v), there are two kinds of residual capacity:
- Forward residual:
c(u, v) - f(u, v). This is the unused room on the edge. - Reverse residual:
f(u, v)on the pairv -> u. This is the flow you could cancel, or "push back."
In general, c_f(u, v) = c(u, v) - f(u, v) + f(v, u). The residual graph G_f has an edge u -> v wherever c_f(u, v) > 0. Saturated edges drop out, and every edge that carries flow gains a reverse edge.
Why Greedy Gets Stuck Without Reverse Edges
Take this network with every capacity equal to 1: s -> a, s -> b, a -> b, a -> t, b -> t. The maximum flow is 2, sent along s -> a -> t and s -> b -> t.
Suppose a naive greedy method first picks s -> a -> b -> t and sends 1 unit. That saturates s -> a, a -> b, and b -> t. Looking only at unused forward capacity, s can still reach b, but b -> t is full, so the method stops at 1. That first choice was a mistake, and forward edges alone give no way to undo it.
Reverse edges fix this. The flow on a -> b creates a residual edge b -> a, so the path s -> b -> a -> t now exists. Pushing 1 unit along it cancels the flow on a -> b. The result is the optimal routing, and no unit of flow travels between a and b.
Building the Residual Graph in Code
def residual_graph(capacity, flow):
n = len(capacity)
res = [[0] * n for _ in range(n)]
for u in range(n):
for v in range(n):
# unused forward room plus flow on v -> u that can be cancelled
res[u][v] = capacity[u][v] - flow[u][v] + flow[v][u]
return res
# 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_graph(capacity, flow)
print([(u, v) for u in range(4) for v in range(4) if res[u][v] > 0])
# [(0, 2), (1, 0), (1, 3), (2, 1), (3, 2)]The output matches the diagram. (0, 2) is s -> b, (1, 3) is a -> t, and the other three are reverse edges.
Common Errors
| Mistake | Consequence |
|---|---|
| Omitting reverse edges | The algorithm stops at a flow below the maximum |
| Setting reverse residual to `c(u, v)` instead of `f(u, v)` | Lets you cancel flow that was never sent |
| Overwriting `res[v][u]` when both `u -> v` and `v -> u` exist | Loses one edge's capacity; sum the terms as the formula does |
| Keeping edges with residual 0 in `G_f` | Paths through them push zero flow, and search wastes time |
Exercise
An edge u -> v has capacity 10 and carries flow 7. There is no edge v -> u in the original network. What are cf(u, v) and cf(v, u)?
Show answer
cf(u, v) = 10 - 7 = 3, and cf(v, u) = 0 - 0 + 7 = 7. Both edges appear in the residual graph.
Exercise
In the four-node example, the network has both edges a -> b (capacity 1) and, suppose we add, b -> a (capacity 2). With flow 1 on a -> b and 0 on b -> a, what is res[b][a]?
Show answer
res[b][a] = c(b, a) - f(b, a) + f(a, b) = 2 - 0 + 1 = 3.