0/30/30/20/20/20/20/3undo0cutS sideT sideascdtb1flow = 02while True:3 parent = bfs(res, s, t)4 if t not in parent: break5 path = walk_back(parent, t)6 b = min(res[u][v] for u, v in path)7 for u, v in path:8 res[u][v] -= b9 res[v][u] += b10 flow += bb–flow0+2+1+1
Parcels: the flow pushed along one augmenting pathPipes: flow/capacity, yellow = how fullRings: what the last search reached; dashes: the min cutFord-Fulkerson with shortest augmenting paths (Edmonds-Karp) on a network of seven pipes, run by the Python loop on the card. Each parcel carries a path's bottleneck from s to t, and the pipes fill (yellow) as it passes; the third rides the undo arrow b→a and moves one unit off a→b. When no path is left, the vertices the last search reached (rings) and the dashed cut show a cut of capacity 4, equal to the maximum flow.

The problem

  1. 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.

Augmenting paths

The min cut

What happens

;; step 1/10 · network
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

The figure draws only the undo arrow b→a, the one a search actually uses. In the real residual graph every pipe carrying flow has an undo arrow (a→s, b→a, c→a, t→b and so on); the Python dict res holds all of them, starting at 0. Which shortest path the search finds first depends on the order it scans neighbours. Here that is the order of the edges list, because Python dicts keep insertion order. Another order can give other paths, but always the same maximum flow of 4.