ENع
←Max flow, min cut
Lesson S4 · Special topics

Max flow and min cut

14 min10 steps5 questionsIntermediate
About this lesson

How much can a network of pipes carry from a source to a sink? Push flow along one augmenting path at a time, keep an undo arrow for every push, and stop when no path is left: that loop is the Ford-Fulkerson method. The last search also hands you a cut: a set of full pipes whose capacities add up to exactly the flow you found. That equality is the max-flow min-cut theorem. The search is plain breadth-first search, as in the Software lesson "Graph search: BFS and DFS", and the graph is stored as in "Adjacency lists and adjacency matrices".

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig S4 · Interactive
01 / 10

Pipes from s to t

A network of one-way pipes runs from a source s to a sink t. Each pipe has a capacity: the most it can carry per second. Question: how much can flow from s to t in total? That is the maximum flow problem.

a flow network, V = 6 vertices, E = 7 pipes
  s → a   capacity 3
  s → d   capacity 3
  a → b   capacity 2
  a → c   capacity 2
  d → b   capacity 2
  b → t   capacity 2
  c → t   capacity 3

source s, sink t
goal: the largest total flow from s to t
Loading figure…
or ← →
Read the lesson step by step
  1. Pipes from s to t

    A network of one-way pipes runs from a source s to a sink t. Each pipe has a capacity: the most it can carry per second. Question: how much can flow from s to t in total? That is the maximum flow problem.

    a flow network, V = 6 vertices, E = 7 pipes
      s → a   capacity 3
      s → d   capacity 3
      a → b   capacity 2
      a → c   capacity 2
      d → b   capacity 2
      b → t   capacity 2
      c → t   capacity 3
    
    source s, sink t
    goal: the largest total flow from s to t
  2. Two rules for a flow

    A flow puts an amount on every pipe, shown as flow/capacity. Rule 1: never more than the capacity. Rule 2: every middle vertex, like a, sends out exactly what comes in. The flow’s value is what leaves s. All start at 0.

    capacity:      0 ≤ f(u, v) ≤ c(u, v) on every pipe
    conservation:  flow in = flow out at a, b, c, d
      at a:  f(s, a) = f(a, b) + f(a, c)
    value |f| = flow out of s = flow into t
    
    sa 0/3  sd 0/3  ab 0/2  ac 0/2  db 0/2  bt 0/2  ct 0/3
  3. First path: push 2

    Breadth-first search finds the shortest route from s to t through pipes with room left: s, a, b, t. Such a route is called an augmenting path. Its bottleneck, the smallest room on it, is 2. Push 2 along it. Flow: 2.

    bfs reached s a d b c t → path s→a→b→t
    room: s→a 3, a→b 2, b→t 2
    bottleneck b = 2
    
    sa 2/3  sd 0/3  ab 2/2  ac 0/2  db 0/2  bt 2/2  ct 0/3
    flow = 2
  4. An undo arrow

    A push can be taken back. Pipe a→b now carries 2, so we add an undo arrow b→a with room 2. Room left on every pipe plus these undo arrows is the residual graph. Every search runs on it.

    residual room on u → v:  c(u, v) − f(u, v)
    undo arrow v → u:        f(u, v)
    
    a → b  room 0   b → a (undo) room 2
    line 9 res[v][u] += b grows the undo arrow
    every pipe with flow has one; the figure draws b→a
  5. Second path: push 1

    Pipes a→b and b→t are full now. The next search finds s, a, c, t. Pipe s→a has only 1 unit of room left, so the bottleneck is 1. Push it. Flow: 3.

    bfs reached s a d c b t → path s→a→c→t
    room: s→a 1, a→c 2, c→t 3
    bottleneck b = 1
    
    sa 3/3  sd 0/3  ab 2/2  ac 1/2  db 0/2  bt 2/2  ct 1/3
    flow = 3
  6. Reroute through the undo

    s→a is full, so the search goes s, d, b, back along the undo arrow to a, then c, t. Bottleneck 1. Riding the undo arrow takes 1 unit off a→b: it now goes a→c, and d’s unit replaces it into b. Flow: 4.

    bfs reached s d b a c t → path s→d→b→a→c→t
    room: s→d 3, d→b 2, b→a undo 2, a→c 1, c→t 2
    bottleneck b = 1
    b → a is an undo arrow: f(a, b) goes 2 → 1
    
    sa 3/3  sd 1/3  ab 1/2  ac 2/2  db 1/2  bt 2/2  ct 2/3
    flow = 4
  7. No path left: stop

    The next search reaches d and b, then a through the undo arrow. Every way on to c or t is full. t is not reached, so line 4 breaks the loop. This search-and-push loop is the Ford-Fulkerson method. The maximum flow is 4.

    bfs reached s d b a; t not reached
      s→a full, a→c full, b→t full
      a→s and the other undo arrows lead back to reached vertices
    
    sa 3/3  sd 1/3  ab 1/2  ac 2/2  db 1/2  bt 2/2  ct 2/3
    flow = 4: no augmenting path, so it is maximum
  8. The cut it leaves

    Draw a line around what the last search reached: a, b, d, s. That splits the vertices in two, so it is a cut. The pipes crossing it outward, a→c and b→t, are both full. Capacity: 2 + 2 = 4.

    S = {a, b, d, s}   T = {c, t}
    pipes from S to T:
      a → c   capacity 2, flow 2
      b → t   capacity 2, flow 2
    pipes from T to S: none
    capacity of the cut = 4
  9. Max flow equals min cut

    Every unit from s to t must cross any cut, so no flow can beat any cut’s capacity. Our flow of 4 meets a cut of 4, so both are the best possible. This is the max-flow min-cut theorem.

    for any flow f and any cut (S, T):  |f| ≤ c(S, T)
    here |f| = 4 = c(S, T)
      so no flow is bigger than 4
      and no cut is smaller than 4
    equivalent (MIT 6.046J, CLRS):
      f is a maximum flow
      f has no augmenting path
      |f| = c(S, T) for some cut
  10. How much it costs

    Ford-Fulkerson that always takes a shortest augmenting path is the Edmonds-Karp algorithm. It needs at most O(V·E) paths, each found by a search costing O(E). Total: O(V·E²). Here: 3 paths, then 1 failed search.

    this run: V = 6, E = 7
      augmenting paths  3  (bottlenecks 2, 1, 1)
      searches          4
    
    Edmonds-Karp (BFS paths): O(V·E²)   (CLRS)
    any path, integer capacities: O(E·|f*|)
    Sedgewick and Wayne: O(E·V·(E + V))

Quiz Unlocks after the last step.