The problem
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 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.
Augmenting paths
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.
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.
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.
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.
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.
The min cut
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.
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.
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.
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.