Sub topic 2
Valid Flows: Capacity and Conservation
This chapter assumes you know what a flow network is from the previous chapter: a directed graph with a capacity on each edge, a source s, and a sink t. It also assumes basic Python and comfort with adjacency matrices.
What a Flow Assigns
A flow assigns a number f(u, v) to every edge u -> v. You can read it as the amount of material moving along that edge. Not every assignment counts. A flow is feasible (or valid) only when it satisfies two constraints.
Capacity constraint
For every edge, 0 <= f(u, v) <= c(u, v). Flow cannot be negative, and it cannot exceed the edge's capacity. An edge that does not exist has capacity 0, so it must carry 0 flow.
Conservation constraint
For every vertex other than s and t, the flow coming in equals the flow going out:
- sum of
f(w, v)over allw= sum off(v, w)over allw
Intermediate vertices only pass material along. They cannot store it or create it. Only s produces flow and only t absorbs it.
The Value of a Flow
The value |f| is the net flow leaving the source: total out of s minus total into s. Conservation forces this to equal the net flow entering t. Every unit that leaves s has to end up somewhere, and every vertex in between passes on exactly what it receives.
A Worked Example
The diagram uses the network that the rest of this tutorial uses. Each label reads flow/capacity.
To check vertex 3, add what comes in (12 from vertex 1 and 7 from vertex 4, so 19) and compare it with what goes out (19 to t and 0 to vertex 2, so 19). The two totals match. The value is 12 + 11 = 23, which equals 19 + 4 arriving at t.
Checking Feasibility in Code
The function below checks both constraints and reports the first violation it finds.
def check_flow(capacity, flow, s, t):
n = len(capacity)
for u in range(n):
for v in range(n):
if not 0 <= flow[u][v] <= capacity[u][v]:
return False, f"capacity violated on {u}->{v}"
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, f"conservation violated at {v}: in {inflow}, out {outflow}"
value = sum(flow[s][v] for v in range(n)) - sum(flow[v][s] for v in range(n))
return True, value
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, 12, 11, 0, 0, 0],
[0, 0, 0, 12, 0, 0],
[0, 0, 0, 0, 11, 0],
[0, 0, 0, 0, 0, 19],
[0, 0, 0, 7, 0, 4],
[0, 0, 0, 0, 0, 0],
]
print(check_flow(capacity, flow, 0, 5)) # (True, 23)Common Errors
- Checking conservation at
sort. These two vertices are exempt. If you checked them, every nonzero flow would fail. - Putting flow on missing edges. In matrix form, a missing edge has capacity 0, so any positive flow on it breaks the capacity constraint.
- Computing value from outflow alone. If edges enter
s, subtract their flow.
Exercise
In the example, change f(s, 2) from 11 to 13 and leave every other edge unchanged. Which constraint fails, and where?
Show answer
The capacity constraint still holds, because 13 <= 13. Conservation fails at vertex 2: 13 units come in but only 11 go out (to vertex 4).
Exercise
Explain why the net flow out of s always equals the net flow into t for a feasible flow.
Show answer
Add up the net outflow (out minus in) over all vertices. Each edge's flow counts once as outflow and once as inflow, so the total is 0. Conservation makes every intermediate vertex contribute 0, so net out of s plus net out of t equals 0. That means net out of s equals net into t.