EMIS 8374 The FordFulkerson Algorithm aka the labeling
EMIS 8374 The Ford-Fulkerson Algorithm (aka the labeling algorithm) Updated 4 March 2008
Ford-Fulkerson Algorithm begin x : = 0; label node t; while t is labeled do begin unlabel all nodes; pred(j) : = 0 for all j in N; label s; LIST : = {s}; while LIST is not empty and t is not labeled do begin remove a node i from LIST; for all {j in N: (i, j) in A and rij > 0} do if j is unlabeled then pred(j) : = i, label j, add j to LIST; end; if t is labeled then augment flow on path from s to t end; Slide 2
Labeling Algorithm Example (0, 5) s 2 (0, 2) 4 (0, 4) 1 6 (0, 6) t (0, 7) 3 (0, 5) 5 Slide 3
The Residual Network G(x) 2 2 5 s 1 0 6 0 0 0 3 4 4 0 4 5 0 7 5 6 t 0 Slide 4
Iteration 1: LIST = {1}, Labeled = {1} i=1 2 2 5 s 1 0 6 0 0 0 3 4 4 0 4 5 0 0 5 6 t 7 Slide 5
Iteration 1: LIST = {1}, Labeled = {1} • i=1 • LIST = {} • Arc (1, 2) – pred(2) =1 – label 2 – LIST = {2} • Arc (1, 3) – pred (3) = 1 – label 3 – LIST = {2, 3} Slide 6
Iteration 1: LIST = {2, 3}, Labeled= {1, 2, 3} pred(2) = 1 2 5 s 1 0 3 pred(3) = 1 4 4 0 0 6 0 2 0 4 5 0 7 5 6 t 0 Slide 7
Iteration 1: LIST = {2, 3}, Labeled = {1, 2, 3} • i=2 • LIST = {3} • Arc (2, 4) – pred(4) =2 – label 4 – LIST = {3, 4} • Arc (2, 1) – residual capacity of (2, 1) = 0 • Arc (2, 5) – pred (5) = 2 – label 5 – LIST = {3, 4, 5} Slide 8
Iteration 1: LIST = {3, 4, 5}, Labeled= {1, 2, 3, 4, 5} pred(2) = 1 2 5 s 1 0 3 pred(3) = 1 4 4 0 0 6 0 2 pred(4) = 2 0 4 5 0 7 5 6 t 0 pred(5) = 2 Slide 9
Iteration 1: LIST = {3, 4, 5}, Labeled = {1, 2, 3, 4, 5} • i=3 • LIST = {4, 5} • Arc (3, 5) – 5 is already labeled • Arc (3, 1) – residual capacity of (3, 1) = 0 Slide 10
Iteration 1: LIST = {4, 5}, Labeled = {1, 2, 3, 4, 5} • i=4 • LIST = {5} • Arc (4, 2) – residual capacity of (4, 2) = 0 • Arc (4, 6) – pred(6) =4 – label 6 – LIST = {5, 6} Slide 11
Iteration 1: LIST = {5, 6}, Labeled= {1, 2, 3, 4, 5, 6} pred(2) = 1 2 5 s 1 0 4 4 0 0 6 0 2 pred(4) = 2 3 pred(3) = 1 0 4 5 0 pred(6) = 4 7 5 6 t 0 pred(5) = 2 Slide 12
Iteration 1: The sink is labeled • Use pred labels to trace back from the sink to the source to find path P –P=1→ 2→ 4→ 6 • = min {rij: (i, j) in P) = 2 • Send 2 units of flow from to s to t along path P Slide 13
Flow x After Iteration 1 (2, 5) s 2 (2, 2) 4 (2, 4) (0, 4) 1 (0, 6) 3 (0, 5) 6 t (0, 7) 5 v=2 Slide 14
The Residual Network G(x) 0 2 3 s 1 2 6 0 2 0 3 2 4 5 0 7 5 6 t 0 Slide 15
Iteration 2: LIST = {1}, Labeled = {1} • i=1 • LIST = {} • Arc (1, 2) – pred(2) =1 – label 2 – LIST = {2} • Arc (1, 3) – pred (3) = 1 – label 3 – LIST = {2, 3} Slide 16
Iteration 2: LIST = {2, 3}, Labeled={1, 2, 3} p=1 2 3 s 1 2 0 6 0 2 0 3 p=1 2 4 5 0 7 5 6 t 0 Slide 17
Iteration 2: LIST = {2, 3}, Labeled = {1, 2, 3} • i=2 • LIST = {3} • Arc (2, 4) – residual cap (2, 4) = 0 • Arc (2, 5) • Arc (2, 1) – residual capacity of (2, 1) = 0 – pred (5) = 2 – label 5 – LIST = {3, 5} Slide 18
Iteration 2: LIST = {3, 5}, Labeled={1, 2, 3, 5} p=1 2 3 s 1 2 0 6 0 2 0 3 p=1 2 4 5 0 7 5 6 t 0 p=2 Slide 19
Iteration 2: LIST = {3, 5}, Labeled = {1, 2, 3, 5} • i=3 • LIST = {5} • Arc (3, 5) – 5 is already labeled • Arc (3, 1) – residual capacity of (3, 1) = 0 • i=5 • LIST = {} • Arc (5, 2) – residual cap = 0 • Arc (5, 3) – residual cap = 0 • Arc (5, 6) – pred(6) = 5 – label 6 – LIST = {6} Slide 20
Iteration 2: LIST = {6}, Labeled={1, 2, 3, 5, 6} p=1 2 3 s 1 2 0 6 0 2 0 3 p=1 2 4 5 0 7 5 6 0 t p=5 p=2 Slide 21
Iteration 2: The sink is labeled • Use pred labels to trace back from the sink to the source to find path P – P = 1 -> 2 -> 5 -> 6 • = min {rij: (i, j) in P) = 3 • Send 3 units of flow from to s to t along path P Slide 22
Flow x After Iteration 2 (5, 5) s 2 (2, 2) 4 (2, 4) (3, 4) 1 (0, 6) 3 (0, 5) 6 t (3, 7) 5 v=5 Slide 23
The Residual Network G(x) 0 2 0 s 1 5 6 0 2 3 3 2 4 2 1 5 0 4 5 6 t 0 Slide 24
Iteration 3: LIST = {1}, Labeled = {1} • i=1 • LIST = {} • Arc (1, 2) – residual capacity = 0 • Arc (1, 3) – pred (3) = 1 – label 3 – LIST = {3} Slide 25
Iteration 3: List = {3}, Labeled = {1, 3} 0 2 0 s 1 5 6 0 2 3 3 p=1 2 4 2 1 5 0 4 5 6 t 0 Slide 26
Iteration 3: LIST = {3}, Labeled = {1, 3} • i=3 • LIST = {} • Arc (3, 1) – residual capacity = 0 • Arc (3, 5) – pred (5) = 3 – label 5 – LIST = {5} Slide 27
Iteration 3: List = {5}, Labeled = {1, 3, 5} 0 2 0 s 1 5 6 0 2 3 3 p=1 2 4 2 1 5 0 4 5 6 t 0 p=2 Slide 28
Iteration 3: LIST = {5}, Labeled = {1, 3, 5} • i = 5, LIST = {} • Arc (5, 2) – pred(2) = 5 – label 2 – LIST = {2} • Arc (5, 3): residual capacity = 0 • Arc (5, 6) – pred (6) = 5 – label 6 – LIST = {2, 6} Slide 29
Iteration 3: List = {2, 6}, Labeled = {1, 2, 3, 5, 6} p=5 2 0 s 1 5 0 6 0 2 3 3 p=1 2 4 2 1 5 0 4 5 6 0 t p=5 p=2 Slide 30
Iteration 3: The sink is labeled • Use pred labels to trace back from the sink to the source to find path P – P = 1 -> 3 -> 5 -> 6 • = min {rij: (i, j) in P) = 4 • Send 4 units of flow from to s to t along path P Slide 31
Flow x After Iteration 3 (5, 5) s 2 (2, 2) 4 (2, 4) (3, 4) 1 (4, 6) 3 (4, 5) 6 t (7, 7) 5 v=9 Slide 32
The Residual Network G(x) 0 2 0 s 1 5 2 4 2 3 3 2 4 2 1 1 4 0 5 6 t 7 Slide 33
Iteration 4: LIST = {1}, Labeled = {1} • i=1 • LIST = {} • Arc (1, 2) – residual capacity = 0 • Arc (1, 3) – pred (3) = 1 – label 3 – LIST = {3} Slide 34
Iteration 4: List = {3}, Labeled = {1, 3} 0 2 0 s 1 5 2 4 2 3 3 p=1 2 4 2 1 1 4 0 5 6 t 7 Slide 35
Iteration 4: LIST = {3}, Labeled = {1, 3} • i=3 • LIST = {} • Arc (3, 1) – 1 is labeled • Arc (3, 5) – pred (5) = 3 – label 5 – LIST = {5} Slide 36
Iteration 4: List = {5}, Labeled = {1, 3, 5} 0 2 0 s 1 5 2 4 2 3 3 p=1 2 4 2 1 1 4 0 5 6 t 7 p=3 Slide 37
Iteration 4: LIST = {5}, Labeled = {1, 3, 5} • i=5 • LIST = {} • Arc (5, 2) – pred(2) = 5 – label 2 – LIST = {2} • Arc (5, 6) – residual capacity = 0 Slide 38
Iteration 4: List = {2}, Labeled = {1, 2, 3, 5} p=5 2 0 s 1 5 0 2 4 2 3 3 p=1 2 4 2 1 1 4 0 5 6 t 7 p=3 Slide 39
Iteration 4: LIST = {2}, Labeled = {1, 2, 3, 5} • i = 2 LIST = {} • Arc (2, 1) – 1 is already labeled • Arc(2, 4) – residual capacity = 0 • Arc (2, 5) – 5 is already labeled Slide 40
Iteration 4 • List = {} • The sink is not labeled • Algorithm ends with optimal flow x Slide 41
Correctness • At the end of each iteration, the algorithm either augments the flow or terminates because it can’t label the sink. • Let S be the set of labeled nodes when the algorithm terminates. Let T = N S. • We need to show that when the algorithm terminates v = u[S, T] which implies x is a maximum flow. Slide 42
Correctness: arcs in (S, T) s i j Slide 43
Correctness: arcs in (T, S) i • • j s Suppose xij > 0 rji = uji – xji + xij Implies rji > 0 since uji xji Implies s can reach node i in G(x) Implies i is labeled (i. e. , node i is in S) Implies xij = 0 Contradiction. If i in T and j in S, then xij = 0 Slide 44
Complexity • Let U = max {(i, j) in A} uij. – If S = {s} and T = N{s}, then u[S, T] ≤ n. U. – The maximum flow is at most n. U. – The inner loop runs at most n. U times. • An iteration of the inner while loop is O(m): – Each arc is inspected at most once – Finding is O(n) – Updating the flow on P is O(n) • Complexity is O(nm. U). Slide 45
Pathological Example (0, 106) s 1 (0, 106) 2 (0, 106) (0, 1) 5 3 t (0, 106) Slide 46
Pathological Example: First Augmenting Path (0, 106) s 1 (0, 106) 2 (0, 106) (0, 1) 5 3 t (0, 106) v=1 Slide 47
Residual Network 106 -1 s 2 1 1 106 1 3 106 1 5 t 106 -1 Slide 48
An Augmenting Path in the Residual Network 106 -1 s 2 1 1 106 1 3 106 1 5 t 106 -1 Slide 49
Updated Flow in G (1, 106) s 1 (1, 106) 2 (1, 106) (0, 1) 5 3 t (1, 106) v=2 Slide 50
Updated Residual Network 106 -1 s 1 106 -1 2 1 1 1 3 106 -1 1 1 5 t 106 -1 Slide 51
Next Augmenting Path in the Residual Network 106 -1 s 1 106 -1 2 1 1 1 3 106 -1 1 1 5 t 106 -1 This will take 2 million iterations to find the maximum flow! Slide 52
Polynomial Max Flow Algorithms (Chapter 7) • Always augment along the shortest augmenting path in the residual network. – Use breadth-first search to find augmenting path – O(n 2 m) • Always augment along the maximum-capacity augmenting path in the residual network. – O(nm log U) • Goldberg’s algorithm (preflow-push) with highestlabel implementation. – O(n 2 m 1/2) Slide 53
- Slides: 53