Sub topic 1
What Is a Flow Network?
Prerequisites
This chapter assumes you already know these topics:
- Directed graphs, and how to store them as an adjacency list or an adjacency matrix
- Breadth-first search (BFS) and depth-first search (DFS)
- Big-O notation
- Basic Python
- Simple proofs by invariant or by contradiction
Flow Networks
A flow network is a directed graph in which every edge (u, v) has a capacity c(u, v) ≥ 0. Two vertices have special roles. The source s is where flow starts, and the sink t is where it ends. If an edge is missing, its capacity is 0.
Valid Flows: Capacity and Conservation
A flow f assigns a value to each edge. A flow is feasible (valid) when it meets two constraints:
- Capacity constraint: 0 ≤ f(u, v) ≤ c(u, v) for every edge.
- Conservation constraint: at every vertex except s and t, the flow coming in equals the flow going out.
The value of the flow is the net amount that leaves s. In practice, most bugs come from forgetting to exempt s and t from conservation, or from letting a flow value go negative.
def is_feasible(capacity, flow, s, t):
n = len(capacity)
for u in range(n):
for v in range(n):
if flow[u][v] < 0 or flow[u][v] > capacity[u][v]:
return False
for v in range(n):
if v in (s, t):
continue
inflow = sum(flow[u][v] for u in range(n))
outflow = sum(flow[v][w] for w in range(n))
if inflow != outflow:
return False
return True
capacity = [
[0, 16, 13, 0, 0, 0],
[0, 0, 0, 12, 0, 0],
[0, 4, 0, 0, 14, 0],
[0, 0, 9, 0, 0, 20],
[0, 0, 0, 7, 0, 4],
[0, 0, 0, 0, 0, 0],
]
flow = [[0] * 6 for _ in range(6)]
flow[0][1] = flow[1][3] = flow[3][5] = 12
print(is_feasible(capacity, flow, 0, 5)) # TrueThe Residual Graph and Why Reverse Edges Matter
The residual graph G_f shows how much more you can change the flow on each edge. Every edge (u, v) that carries flow f(u, v) produces two residual edges:
| Residual edge | Residual capacity | Meaning |
|---|---|---|
| u → v | c(u, v) − f(u, v) | Room left to push more flow |
| v → u | f(u, v) | Flow you can cancel |
Reverse edges let an algorithm undo an earlier choice that turned out to be poor. Without them, a greedy algorithm can get stuck with less than the maximum flow.
Augmenting Paths and the Bottleneck
An augmenting path is a path from s to t in the residual graph. Its bottleneck is the smallest residual capacity along the path. To augment, add the bottleneck to each forward residual edge on the path and subtract it from the matching reverse edge. The flow stays feasible, and its value grows by the bottleneck.
In the network above, the path s → 1 → 3 → t has residual capacities 16, 12, and 20. Its bottleneck is 12. That is the flow the code above checked.
Exercise
An edge u → v has capacity 10 and carries flow 7. What are the residual capacities of u → v and v → u? If an augmenting path uses v → u with a bottleneck of 5, what is the new flow on u → v?
Show answer
The residual capacity of u → v is 10 − 7 = 3, and the residual capacity of v → u is 7. Pushing 5 along v → u cancels 5 units, so the new flow on u → v is 7 − 5 = 2.