Sub topic 2
Valid Flows: Capacity and Conservation
This chapter assumes you know directed graphs and the adjacency-matrix representation, that you can read basic Python, and that you have the flow network from the previous chapter: a directed graph with a capacity on each edge, a source s, and a sink t. Here we define what a flow is, what makes a flow valid, and how to measure how much flow it moves. Every algorithm later in the tutorial starts from a valid flow and keeps it valid, so you need to be able to check these rules both by hand and in code.
What a Flow Assigns
A flow is a function f that gives every ordered pair of vertices (u, v) a number f(u, v), the amount sent along the edge u -> v. If the pair has no edge, its capacity is 0 by convention. This lets us store a flow in an n x n matrix with the same shape as the capacity matrix, where flow[u][v] sits next to capacity[u][v].
A flow is feasible (also called valid) when it satisfies two constraints.
| Constraint | Rule | Applies to |
|---|---|---|
| Capacity | `0 <= f(u, v) <= c(u, v)` | Every pair `(u, v)` |
| Conservation | Sum of `f(u, v)` over all `u` equals sum of `f(v, w)` over all `w` | Every vertex `v` except `s` and `t` |
The capacity constraint says an edge carries no negative amount and no more than it can hold. Since non-edges have capacity 0, it also says that flow only travels along real edges. The conservation constraint says that intermediate vertices neither create nor absorb flow: what comes in goes out. Only the source and the sink are exempt.
The Value of a Flow
The value of a flow, written |f|, is the net amount leaving the source:
|f| = sum over v of f(s, v) - sum over u of f(u, s)
The subtraction is there because nothing in the definition forbids edges into s. Most textbook networks have none, but your code should not assume that.
You could also measure the flow at the sink, as the net amount entering t. The two measurements always agree. To see why, compute out(v) - in(v) for every vertex and add up the results. Each edge u -> v contributes +f(u, v) to out(u) and -f(u, v) to in(v), so the grand total is exactly 0. Conservation makes every term 0 except the ones for s and t, which leaves:
(out(s) - in(s)) + (out(t) - in(t)) = 0
So out(s) - in(s) = in(t) - out(t), and flow cannot leak out anywhere in the middle. This argument comes back later when we prove that any cut bounds the flow value.
A Worked Example
We use the six-vertex network that appears throughout this tutorial. Vertex 0 is s, vertex 5 is t, and each edge below is labelled flow/capacity.
Every edge is within its capacity, so the capacity constraint holds. To check conservation, look at each intermediate vertex:
| Vertex | Inflow | Outflow | Balanced? |
|---|---|---|---|
| 1 | 11 (from s) + 1 (from 2) = 12 | 12 (to 3) | Yes |
| 2 | 12 (from s) + 0 (from 3) = 12 | 1 (to 1) + 11 (to 4) = 12 | Yes |
| 3 | 12 (from 1) + 7 (from 4) = 19 | 19 (to t) + 0 (to 2) = 19 | Yes |
| 4 | 11 (from 2) | 7 (to 3) + 4 (to t) = 11 | Yes |
The value is 11 + 12 = 23 at the source, and the sink receives 19 + 4 = 23, as the argument above predicts.
Checking Feasibility in Code
The checker below reports every violation it finds instead of stopping at the first one. That makes it useful for debugging an algorithm that corrupts its flow.
def check_flow(capacity, flow, s, t):
"""Return a list of violations; an empty list means the flow is feasible."""
n = len(capacity)
problems = []
# Capacity constraint on every ordered pair (non-edges have capacity 0)
for u in range(n):
for v in range(n):
f = flow[u][v]
if f < 0:
problems.append(f"negative flow on {u}->{v}: {f}")
elif f > capacity[u][v]:
problems.append(
f"flow {f} exceeds capacity {capacity[u][v]} on {u}->{v}")
# Conservation at every vertex except s and t
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:
problems.append(f"node {v}: inflow {inflow} != outflow {outflow}")
return problems
def flow_value(flow, s):
n = len(flow)
return sum(flow[s][v] for v in range(n)) - sum(flow[u][s] for u in range(n))
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],
]
good = [
[0, 11, 12, 0, 0, 0],
[0, 0, 0, 12, 0, 0],
[0, 1, 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, good, 0, 5)) # []
print(flow_value(good, 0)) # 23
bad = [row[:] for row in good]
bad[1][3] = 13 # one unit too many on edge 1 -> 3
for p in check_flow(capacity, bad, 0, 5):
print(p)
# flow 13 exceeds capacity 12 on 1->3
# node 1: inflow 12 != outflow 13
# node 3: inflow 20 != outflow 19Changing one entry breaks three rules. The edge goes over capacity, vertex 1 now sends out more than it receives, and vertex 3 receives more than it sends out. A single bad update inside an algorithm usually shows up as a capacity violation plus a conservation violation at both ends of the edge. That pattern helps you find the faulty line.
Edge Cases and Common Errors
Flow into the source or out of the sink
A valid flow may send units back into s or out of t along existing edges. That is legal, but it lowers the value. A frequent bug is to compute |f| as only the sum of flow[s][v], which overcounts when the source has incoming edges. Use the net formula.
Conservation at s and t
Some people apply conservation to every vertex, including s and t. Under that rule the only feasible flow has value 0. Others forget to skip the sink and then report a "violation" at t for every correct flow. Both s and t must be exempt, and nothing else.
Antiparallel edges
If the network has both u -> v and v -> u, a flow may use both. Sending 3 units on u -> v and 2 on v -> u is feasible, but it moves the same net amount as sending 1 on u -> v and 0 on v -> u. The second version is simpler and leaves more spare capacity on both edges. Algorithms handle this cancellation through reverse edges in the residual graph, which is the subject of the next chapter.
Parallel edges and matrices
An adjacency matrix holds one number per ordered pair, so it cannot store two separate edges from u to v. For feasibility you can merge them into one edge whose capacity is the sum of theirs. If you need flow per edge, for example to report which physical link carries what, use the adjacency-list representation introduced later in the tutorial.
The zero flow
Setting f(u, v) = 0 everywhere always satisfies both constraints, because capacities are non-negative. That is why Ford-Fulkerson can start from zero. Every augmentation it performs keeps both constraints true, so the flow stays valid from the first step to the last.
Exercise
In the example network, someone proposes the flow s->1 = 10, s->2 = 13, 2->1 = 2, 1->3 = 12, 2->4 = 11, 4->3 = 7, 4->t = 4, 3->t = 19, and 0 on every other edge. Is it feasible? If not, name every violated constraint.
Show answer
Every edge is within capacity (s->2 = 13 equals its capacity exactly). Vertex 1 receives 10 + 2 = 12 and sends 12, so it balances. Vertex 2 receives 13 but sends 2 + 11 = 13, so it balances. Vertex 3 receives 12 + 7 = 19 and sends 19, so it balances. Vertex 4 receives 11 and sends 7 + 4 = 11, so it balances. The flow is feasible, and its value is 10 + 13 = 23.
Exercise
Prove that for any feasible flow, the net flow out of s equals the net flow into t, using only the conservation constraint.
Show answer
Add up out(v) - in(v) over all vertices v. Every edge u->v adds f(u, v) once to out(u) and subtracts it once through in(v), so the total is 0. Conservation makes out(v) - in(v) = 0 for each v other than s and t, so (out(s) - in(s)) + (out(t) - in(t)) = 0. Rearranging gives out(s) - in(s) = in(t) - out(t).
Exercise
Change flowvalue so that it measures the value at the sink, then use it to check the good flow from the code example. Why might the two functions disagree on an input that has not passed checkflow?
Show answer
Use sum(flow[u][t] for u in range(n)) - sum(flow[t][w] for w in range(n)), which returns 23 for the good flow. The proof that the source and sink measurements are equal depends on conservation. If some intermediate vertex is unbalanced, flow is created or lost there, and the two numbers can differ. For example, on the bad flow the source still gives 23 and the sink still gives 23, but changing bad[3][5] to 20 would make the sink give 24.