Max flow and min cut
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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".
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
اقرأ الدرس خطوة بخطوة
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 tTwo 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/3First 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 = 2An 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→aSecond 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 = 3Reroute 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 = 4No 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 maximumThe 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 = 4Max 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 cutHow 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))
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A middle vertex has pipes in carrying 3 and 1. Its two pipes out carry 2 and x. What must x be?
Flow conservation: what comes in (3 + 1 = 4) must go out (2 + x), so x = 2.
02An augmenting path has room 3, 1 and 2 on its three pipes. How much flow can you push along it?
The bottleneck is the smallest room on the path, here 1. Pushing more would overfill that pipe.
03Pipe u→v has capacity 5 and carries 2. What does the residual graph hold for this pair?
Forward room is capacity minus flow (5 − 2 = 3). The undo arrow lets a later path take back up to the 2 units already sent.
04Ford-Fulkerson stops because no augmenting path is left. What do the vertices the last search reached give you?
Every pipe from a reached vertex to an unreached one must be full (or the search would have crossed it), and no flow comes back. So the cut's capacity equals the flow, and both are optimal.
05Which algorithm is Ford-Fulkerson that always picks a shortest augmenting path (by number of pipes)?
Edmonds-Karp finds each path by breadth-first search. That limits the number of paths to O(V·E), so the total time is O(V·E²).
المسرد
- Flow network
- A directed graph with a source s, a sink t and a capacity on every edge (pipe).
- Capacity
- The most a pipe can carry. A flow may never put more than this on it.
- Flow conservation
- At every vertex except s and t, the flow coming in equals the flow going out.
- Residual graph
- The leftover room on every pipe, plus an undo arrow backwards for the flow already on it.
- Augmenting path
- A route from s to t in the residual graph with room on every step. Pushing its bottleneck raises the flow.
- Bottleneck
- The smallest room on an augmenting path: how much can be pushed along it.
- Cut
- A split of the vertices into an s side and a t side. Its capacity is the total capacity of pipes going from the s side to the t side.
- Max-flow min-cut theorem
- The largest flow from s to t equals the smallest capacity of any cut.
الملاحظات والمصادر
- 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.
- The theorem follows MIT 6.046J Lecture 13 and CLRS: the value of any flow is at most the capacity of any cut, and these are equivalent: f is a maximum flow, f has no augmenting path, and |f| equals the capacity of some cut.
- Ford-Fulkerson with any augmenting path and whole-number capacities stops after at most |f*| paths, so O(E·|f*|) time. Choosing shortest paths (Edmonds-Karp) bounds the number of paths by O(V·E) whatever the capacities are. One search costs O(V + E) in general (MIT 6.006); CLRS writes O(E) because a network where every vertex lies on some s–t path has E ≥ V − 1.
- The code is real Python 3. With the edges above it prints 4 ['a', 'b', 'd', 's'] and then the flow on every pipe: sa 3, sd 1, ab 1, ac 2, db 1, bt 2, ct 2.
- MIT 6.046J Design and Analysis of Algorithms (Spring 2015), Lecture 13: Incremental Improvement: Max Flow, Min Cut (notes)
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 9: Breadth-First Search
- Sedgewick and Wayne, Algorithms 4th ed., §6.4 Maxflow
- Sedgewick and Wayne, FordFulkerson.java (shortest augmenting paths)
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), Maximum Flow chapter
- Edmonds-Karp algorithm (running time and its CLRS analysis)
- Python documentation: collections.deque
اختبار يتاح بعد الخطوة الأخيرة.