TExcelTAD
Home › Data Structures & Algorithms › Maximum Flow Intro

Maximum Flow Intro

Picture a network of water pipes from a reservoir to a city. Each pipe can carry at most a certain amount of water per second, its capacity. How much water per second can reach the city in total? That is the maximum flow problem.

The same question appears with roads and cars, data links and bandwidth, or supply chains and trucks. It also hides inside problems that don't look like flow at all, such as matching job applicants to jobs.

Flow networks

A flow network is a directed graph where:

  • every edge u → v has a capacity c(u, v) ≥ 0,
  • one vertex is the source S (where flow starts) and one the sink T (where it ends).

A flow assigns an amount f(u, v) to each edge, obeying two rules:

  1. Capacity: 0 ≤ f(u, v) ≤ c(u, v). No pipe carries more than it can.
  2. Conservation: for every vertex except S and T, flow in = flow out. Nothing is stored or lost along the way.

The value of the flow is the total amount leaving the source (which equals the total arriving at the sink).

Our example network

This network is used in this lesson and the next two. Capacities are next to the edges:

text
          10           7            10
   S ─────────▶ A ─────────▶ C ─────────▶ T
   │            │            ▲            ▲
  8│           4│           3│            │6
   ▼            ▼            │            │
   └──────────▶ B ─────────▶ D ───────────┘
                      9

Edges: S→A 10, S→B 8, A→B 4, A→C 7, B→D 9, D→C 3, C→T 10, D→T 6.

Checking a flow

Here is one possible flow. The function checks both rules and reports the value:

python · try it yourself
capacity = {("S", "A"): 10, ("S", "B"): 8, ("A", "B"): 4, ("A", "C"): 7,
            ("B", "D"): 9, ("D", "C"): 3, ("C", "T"): 10, ("D", "T"): 6}

def check_flow(flow, source="S", sink="T"):
    for edge, f in flow.items():
        if not 0 <= f <= capacity[edge]:
            return f"capacity broken on {edge}"
    nodes = {u for e in capacity for u in e}
    for v in sorted(nodes - {source, sink}):
        inflow = sum(f for (a, b), f in flow.items() if b == v)
        outflow = sum(f for (a, b), f in flow.items() if a == v)
        if inflow != outflow:
            return f"conservation broken at {v}: in {inflow}, out {outflow}"
    return f"valid flow of value {sum(f for (a, b), f in flow.items() if a == source)}"

simple = {("S", "A"): 7, ("A", "C"): 7, ("C", "T"): 7,
          ("S", "B"): 6, ("B", "D"): 6, ("D", "T"): 6}
print(check_flow(simple))
leaky = dict(simple)
leaky[("S", "B")] = 8        # more in than out at B
print(check_flow(leaky))
valid flow of value 13
conservation broken at B: in 8, out 6

The simple flow sends 7 units along S → A → C → T and 6 along S → B → D → T, for a value of 13. Can we do better? There is still spare capacity on S → A, A → B and D → C.

Cuts: an upper limit

A cut splits the vertices into two groups, one containing S and the other containing T. Its capacity is the sum of capacities of the edges going from the S side to the T side. Every unit of flow must cross every cut, so no flow can be larger than any cut.

python · try it yourself
capacity = {("S", "A"): 10, ("S", "B"): 8, ("A", "B"): 4, ("A", "C"): 7,
            ("B", "D"): 9, ("D", "C"): 3, ("C", "T"): 10, ("D", "T"): 6}

def cut_capacity(source_side):
    return sum(c for (u, v), c in capacity.items() if u in source_side and v not in source_side)

for side in [{"S"}, {"S", "A"}, {"S", "A", "B"}, {"S", "A", "B", "C", "D"}]:
    print(f"{''.join(sorted(side)):<6} | rest: capacity {cut_capacity(side)}")
S      | rest: capacity 18
AS     | rest: capacity 19
ABS    | rest: capacity 16
ABCDS  | rest: capacity 16

The cut around the source allows 18, but the cuts {S, A, B} | {C, D, T} (edges A→C and B→D: 7 + 9) and {S, A, B, C, D} | {T} (edges C→T and D→T: 10 + 6) allow only 16. So the maximum flow is at most 16.

The max-flow min-cut theorem

One of the most beautiful results in algorithms says:

The maximum value of a flow equals the minimum capacity of a cut.

So if we find a flow of 16, we know it is optimal, because a cut of capacity 16 proves nothing more can get through. The next two lessons find that flow with augmenting paths:

  1. Find a path from S to T along which every edge still has spare capacity.
  2. Push as much flow along it as its tightest edge allows.
  3. Repeat until no such path exists.

The subtle part is that later paths may need to undo flow sent earlier, which is handled by a residual graph with reverse edges.

Algorithm Finds augmenting paths with Time
Ford-Fulkerson any search, usually DFS O(E · f), where f is the max-flow value (integer capacities)
Edmonds-Karp BFS (shortest paths first) O(V · E²), independent of the capacities

Applications

  • Bipartite matching: applicants on one side, jobs on the other, capacity 1 everywhere; the max flow is the largest number of applicants that can be hired.
  • Network reliability: the min cut shows the weakest set of links.
  • Image segmentation: separating foreground from background as a minimum cut.
  • Scheduling and transport: how many goods can move through warehouses and roads per day.

Going further: Graph Algorithms proves the max-flow min-cut theorem, bounds Edmonds-Karp and reduces bipartite matching to flow.

Test yourself

What does flow conservation require?

  1. Every edge is used at full capacity
  2. Flow in equals flow out at every vertex except the source and sink
  3. The source and sink have equal capacity
  4. Flow only moves along the shortest path

Why can no flow be larger than the capacity of any S-T cut?

  1. Cuts are always small
  2. Every unit of flow from S to T must cross the cut
  3. Flows avoid cuts
  4. Because of conservation at S

According to the max-flow min-cut theorem, what is the maximum flow of the lesson's network?

  1. 13
  2. 16
  3. 18
  4. 57