Sub topic 1
What Is a Flow Network?
A flow network is a model of anything that moves through a system with limited room: water through pipes, packets through routers, trucks along roads. This chapter defines the model precisely and builds the example network used throughout the tutorial.
Prerequisites
This chapter assumes you already know:
- Directed graphs and how to store them as an adjacency list or adjacency matrix
- Breadth-first search and depth-first search
- Big-O notation
- Basic Python: lists, loops, functions, and classes
- Simple proofs by contradiction or by invariant
The Formal Definition
A flow network is a directed graph G = (V, E) with three extra pieces:
- A capacity function c(u, v) ≥ 0 for every edge (u, v). This is the most that edge can carry. If (u, v) is not an edge, then c(u, v) = 0.
- A source vertex s, where flow starts.
- A sink vertex t, where flow ends, with s ≠ t.
The goal of the whole tutorial is to send as much flow as possible from s to t without exceeding any capacity. That quantity is the maximum flow.
Some extra conventions make the theory cleaner:
- Every vertex lies on some path from s to t. A vertex that is not on any such path can never carry flow, so you can delete it.
- No self-loops.
- Many textbooks also forbid antiparallel edges, meaning both (u, v) and (v, u) at once. Later chapters show why reverse directions matter and how to handle them in code.
A Running Example
The network below has six vertices. Vertex 0 is the source, vertex 5 is the sink, and each edge label is a capacity.
In code, an adjacency matrix keeps capacities easy to read for small graphs:
# Nodes: 0 = s, 1..4 = v1..v4, 5 = t
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],
]
s, t = 0, 5
n = len(capacity)
edges = [(u, v, capacity[u][v]) for u in range(n) for v in range(n) if capacity[u][v] > 0]
for u, v, c in edges:
print(f"{u} -> {v}: capacity {c}")
out_of_source = sum(capacity[s])
into_sink = sum(capacity[u][t] for u in range(n))
print("capacity leaving s:", out_of_source) # 29
print("capacity entering t:", into_sink) # 24Row u, column v holds c(u, v). A zero means there is no edge.
Common Mistakes
| Mistake | Why it is wrong |
|---|---|
| Treating edges as two-way | Capacity c(u, v) applies only from u to v. If there is no edge back, c(v, u) is 0. |
| Mixing up capacity and flow | Capacity is a fixed limit given as input. Flow is the amount actually sent, and the algorithm chooses it. |
| Assuming a negative capacity means "flow backward" | Capacities are never negative. Backward movement is handled later through reverse edges in the residual graph. |
| Forgetting that s and t must differ | If s = t, the problem has no meaning. |
Exercise
In the example network, what upper bound on the maximum flow can you read off directly from the edges touching s and t, and why does it hold?
Show answer
The bound is min(29, 24) = 24. Every unit of flow must leave s on edges with total capacity 16 + 13 = 29, and it must enter t on edges with total capacity 20 + 4 = 24, so no flow can exceed 24. The true maximum, computed later, is 23.
Exercise
Suppose you add an edge from v1 back to s with capacity 5. Does this break the definition of a flow network?
Show answer
No. It is still a valid directed graph with non-negative capacities, a source, and a sink. Under the textbook convention it creates an antiparallel pair with s -> v1, which is allowed in general but has to be handled with care in implementations.