CSE 42 Inroducion o Algorihm Winer 202 The Nework Flow Problem 2
The Nework Flow Problem 5 a 4 3 x 3 7 6 b 4 y 4 7 6 c 5 z How much uff can flow from o? 3
Sovie Rail Nework, 955 Reference: On he hiory of he ranporaion and maximum flow problem. Alexander Schrijver in Mah Programming, 9: 3, 2002. 4
Ne Flow: Formal Definiion Given: A digraph G = (V,E) Two verice, in V (ource & ink) A capaciy c(u,v) 0 for each (u,v) E (and c(u,v) = 0 for all nonedge (u,v)) Find: A flow funcion f: V x V R.., for all u,v: f(u,v) c(u,v) [Capaciy Conrain] f(u,v) = -f(v,u) [Skew Symmery] if u,, f(u,v) = 0 [Flow Conervaion] Maximizing oal flow f = f(,v) Noaion:!! f ( X, Y ) = f ( x, y ) x" X y" Y 5
Example: A Flow Funcion flow/capaciy, no 0.66... 2/2 2/3 u f(,u) = f(u,) = 2 f(u,) = f(,u) = -2 (Why?) f(,) = -f(,) = 0 (In every flow funcion for hi G. Why?) f (u,v ) = " # f (u,v ) = f (u, ) + f (u, ) v V =! 2 + 2 = 0 6
Example: A Flow Funcion 4/5 a 3/4 /3 x 3/3 7 6 b 4 /4 y 7 /6 c 5 z No hown: f(u,v) if 0 Noe: max flow 4 ince f i a flow, f = 4 7
Max Flow via a Greedy Alg? a 3 2 2 b While here i an pah in G Pick uch a pah, p Find c p, he min capaciy of any edge in p Subrac c p from all capaciie on p a 3 b a 2 b 2 Delee edge of capaciy 0 a 2 b 8
Max Flow via a Greedy Alg? Thi doe NOT alway find a max flow: If you pick b a fir, a 3 2 2 b a b Flow uck a 2. Bu flow 3 poible. 9
A Brief Hiory of Flow # Year Dicoverer() Bound n = # of verice 95 Danzig O(n 2 mu) m= # of edge 2 955 Ford & Fulkeron O(nmU) U = Max capaciy 3 970 Diniz; Edmond & Karp O(nm 2 ) 4 970 Diniz O(n 2 m) 5 972 Edmond & Karp; Diniz O(m 2 logu) Source: Goldberg & Rao, FOCS 97 6 973 Diniz;Gabow O(nm logu) 7 974 Karzanov O(n 3 ) 8 977 Cherkaky O(n 2 qr(m)) 9 980 Galil & Naamad O(nm log 2 n) 0 983 Sleaor & Tarjan O(nm log n) 986 Goldberg &Tarjan O(nm log (n 2 /m)) 2 987 Ahuja & Orlin O(nm + n 2 log U) 3 987 Ahuja e al. O(nm log(n qr(log U)/(m+2)) 4 989 Cheriyan & Hagerup E(nm + n 2 log 2 n) 5 990 Cheriyan e al. O(n 3 /log n) 6 990 Alon O(nm + n 8/3 log n) 7 992 King e al. O(nm + n 2+ε ) 8 993 Phillip & Webrook O(nm(log m/n n + log 2+ε n) 9 994 King e al. O(nm(log m/(n log n) n) 20 997 Goldberg & Rao O(m 3/2 log(n 2 /m) log U) ; O(n 2/3 m log(n 2 /m) logu) 2
Greed Reviied a 2/2 / a +/2 2/3 /3 2/2 b 2/2 b / a 2 a 2 2 2 2 b 2 b 3
Reidual Capaciy The reidual capaciy (w.r.. f) of (u,v) i c f (u,v) = c(u,v) - f(u,v) E.g.: c f (,b) = 7; c f (a,x) = ; c f (x,a) = 3; 3/4 a 4/5 /3 x 3/3 7 6 b c 4 5 y /4 z 7 /6 c f (x,) = 0 (a auraed edge) 4
Reidual Nework & Augmening Pah The reidual nework (w.r.. f) i he graph G f = (V,E f ), where E f = { (u,v) c f (u,v) > 0 } An augmening pah (w.r.. f) i a imple pah in G f. 5
A Reidual Nework 4/5 7 6 a b c 3/4 4 5 x /3 y /4 z 3/3 7 /6 4 7 6 reidual nework: he graph G f = (V,E f ), where E f = { (u,v) c f (u,v) > 0 } 3 a x 3 2 4 7 b y 3 5 c 5 z 6
An Augmening Pah 4/5 a 3/4 /3 x 3/3 augmening pah: a imple pah in G f. 7 6 b c 4 5 y /4 z 7 /6 4 7 6 a b c 4 5 3 2 3 x y z 3 7 5 7
Lemma If f admi an augmening pah p, hen f i no maximal. Proof: obviou -- augmen along p by c p, he min reidual capaciy of p edge. 8
9 Augmening A Flow 4/5 6 7 3/4 /3 4 5 3/3 7 /6 /4 a b c x y z 4 3 6 7 2 4 5 3 7 5 3 a b c x y z 4/5 /6 7 3/4 /3 4 /5 3/3 /7 /6 4 a b c x y z
Lemma : Augmened Flow are Flow If f i a flow & p an augmening pah of capaciy c p, hen f i alo a valid flow, where Proof: f '( u, v) = $! #! " f ( u, v) + c f ( u, v) % c f ( u, v), a) Flow conervaion eay b) Skew ymmery eay ( v, u) oherwie c) Capaciy conrain prey eay p p,, if if ( u, v) in pah in pah p p 20
Lma : Augmened Flow are Flow $ f ( u, v) + c! f '( u, v) = # f ( u, v) % c! " f ( u, v), p p,, if ( u, v) if ( v, u) oherwie in pah in pah p p f a flow & p an aug pah of cap c p, hen f alo a valid flow. Proof (Capaciy conrain): (u,v), (v,u) no on pah: no change (u,v) on pah: f (u,v) = f(u,v) + c p f(u,v) + c f (u,v) = f(u,v) + c(u,v) - f(u,v) = c(u,v) f (v,u) = f(v,u) - c p < f(v,u) c(v,u) Reidual Capaciy: 0 < c p c f (u,v) = c(u,v) - f(u,v) Cap Conrain: -c(v,u) f(u,v) c(u,v) 2
Lemma Example Cae Le (u,v) be any edge in augmening pah. Noe G f c p u u c p v c p v c f (u,v) = c(u,v) f(u,v) c p > 0 Cae : f(u,v) 0: G before u u f(u,v)/c(u,v) v v Add forward flow G afer u u f(u,v)+c p /c(u,v) v v 22
Lemma Example Cae 2 Le (u,v) be any edge in augmening pah. Noe G f c p u u c p v c p v c f (u,v) = c(u,v) f(u,v) c p > 0 Cae 2: f(u,v) -c p : f(v,u) = -f(u,v) c p G before u u f(v,u)/c(v,u) v v Cancel/redirec revere flow G afer u u f(v,u)-c p /c(v,u) v v 23
Lemma Example Cae 3 Le (u,v) be any edge in augmening pah. Noe G f c p u u c p v c p v c f (u,v) = c(u,v) f(u,v) c p > 0 Cae 3: -c p < f(u,v) < 0: G before u u v v??? G afer u u v v 24
Lemma Example Cae 3 Le (u,v) be any edge in augmening pah. Noe G f c p u u c p v c p v c f (u,v) = c(u,v) f(u,v) c p > 0 Cae 3: -c p < f(u,v) < 0 c p > f(v,u) > 0: Boh: cancel/redirec revere flow and add forward flow G before u G afer u u u f(v,u)/c(v,u) 0/c(u,v) 0/c(v,u) c p -f(v,u) /c(u,v) v v v v 25
Ford-Fulkeron Mehod While G f ha an augmening pah, augmen Queion:» Doe i hal?» Doe i find a maximum flow?» How fa? 26
Cu A pariion S,T of V i a cu if S, T. Capaciy of cu S,T i c ( S, T ) =! c( u, v ) {} c=8 5 7 6 a b c 4 4 5 3 {,b,c} c=5 4 x y z 3 7 6 {} c=6 5 7 6 {,x} c=2 u" S v" T a b c um of cap of edge from S o T x y z 3 27
Lemma 2 For any flow f and any cu S,T, he ne flow acro he cu equal he oal flow, i.e., f = f(s,t), and he ne flow acro he cu canno exceed he capaciy of he cu, i.e. f(s,t) c(s,t) Corollary: Max flow Min cu Cu Cap = 3 Ne Flow = Cu Cap = 2 Ne Flow = 28
Lemma 2 For any flow f and any cu S,T, ne flow acro cu = oal flow cu capaciy Proof: Track a flow uni. Sar a, end a. croe cu an odd # of ime; ne =. La croing ue a forward edge oaled in C(S,T) Cu Cap = 3 Ne Flow = Cu Cap = 2 Ne Flow = 29
Max Flow / Min Cu Theorem For any flow f, he following are equivalen () f = c(s,t) for ome cu S,T (a min cu) (2) f i a maximum flow (3) f admi no augmening pah Proof: () (2): corollary o lemma 2 (2) (3): conrapoiive of lemma 30
(3) () (no aug) (cu) u S T v Idea: where boleneck S = { u an augmening pah wr f from o u } T = V - S; S, T For any (u,v) in S T, an augmening pah from o u, bu no o v. (u,v) ha 0 reidual capaciy: (u,v) E auraed f(u,v) = c(u,v) (v,u) E no flow f(u,v) = 0 = -f(v,u) Thi i rue for every edge croing he cu, i.e. f f ( S, T ) = f ( u, v ) = u" S v" T f ( u, v ) =! =!!! u" S, v" T,( u, v )" E u" S, v" T,( u, v )" E c( u, v ) = c( S, T ) 3
Corollarie & Fac If Ford-Fulkeron erminae, hen i found a max flow. I will erminae if c(e) ineger or raional (bu may no if hey re irraional). However, may ake exponenial ime, even wih ineger capaciie: c a c c = 0 99, ay c b c 32
How o Make i Faer Many way. Three imporan one: Scaling do big edge fir; ee ex. if C = max capaciy, T = O(m 2 log C) Preflow-Puh ee ex. T = O(n 3 ) Edmond-Karp (nex) T = O(nm 2 ) 33
Edmond-Karp Algorihm Ue a hore augmening pah (via Breadh Fir Search in reidual graph) Time: O(n m 2 ) 34
BFS/Shore Pah Lemma Diance from i never reduced by: Deleing an edge proof: no new (hence no horer) pah creaed Adding an edge (u,v), provided v i nearer han u proof: BFS i unchanged, ince v viied before (u,v) examined v a back edge u 35
Lemma 3 Le f be a flow, G f he reidual graph, and p a hore augmening pah. Then no verex i cloer o afer augmenaion along p. Proof: Augmenaion only delee edge, add back edge 36
Augmenaion v BFS G G f G f 5/9 2/- x 3/0 0/5 x x 6/- u u u 3/3 v v v 2/5 37
Theorem 2 The Edmond-Karp Algorihm perform O(mn) flow augmenaion Proof: {u,v} i criical on augmening pah p if i cloe o having min reidual capaciy. Won be criical again unil farher from. So each edge criical a mo n ime. 38
Augmenaion v BFS Level G G f 0 G f 0 G laer 5/9 2/- x 4 x 5. x 8. 3/0 0/5 u 6/- 3/3 v 2/5 7 8 u 3 0 v 3 2 k - k 4 u 0 3 v 0 5 k k+ u 3 v BFS Level BFS Level 39
Corollary Edmond-Karp run in O(nm 2 ) 40
Flow Inegraliy Theorem If all capaciie are ineger» Some max flow ha an ineger value» Ford-Fulkeron mehod find a max flow in which f(u,v) i an ineger for all edge (u,v) 0.5/ 0.5/ 0.5/ 0.5/ / A valid flow, bu unneceary 4
Biparie Maximum Maching Biparie Graph: G = (V,E) V = L R (L R = ) E L R Maching: A e of edge M E uch ha no wo edge ouch a common verex Problem: Find a maching M of maximum ize 42
Reducing Maching o Flow Given biparie G, build flow nework N a follow: Add ource, ink Add edge à L Add edge Rà All edge capaciie Theorem: Max flow iff max maching 43
Reducing Maching o Flow Theorem: Max maching ize = max flow value M à f? Eay end flow only hrough M f à M? Flow inegraliy Thm, + cap conrain 44
Noe on Maching Max Flow Algorihm i probably overly general here Bu mo direc maching algorihm ue "augmening pah" ype idea imilar o ha in max flow See ex & homework Time mn /2 poible via Edmond-Karp 46
7.2 Baeball Eliminaion Some lide by Kevin Wayne 48
Baeball Eliminaion Team i Win w i Which eam have a chance of finihing he eaon wih mo win?» Monreal eliminaed ince i can finih wih a mo 80 win, bu Alana already ha 83.» w i + g i < w j eam i eliminaed.» Only reaon por wrier appear o be aware of.» Sufficien, bu no neceary! Loe To play Again = g ij l i g i Al Phi NY Mon Alana 83 7 8-6 Philly 80 79 3-0 2 New York 78 78 6 6 0-0 Monreal 77 82 3 2 0-49
Baeball Eliminaion Team i Win w i Loe To play Again = g ij l i g i Al Phi NY Mon Alana 83 7 8-6 Philly 80 79 3-0 2 New York 78 78 6 6 0-0 Monreal 77 82 3 2 0 - Which eam have a chance of finihing he eaon wih mo win?» Philly can win 83, bu ill eliminaed...» If Alana loe a game, hen ome oher eam win one. Remark. Depend on boh how many game already won and lef o play, and on whom hey're again. 50
Baeball Eliminaion Baeball eliminaion problem.» Se of eam S.» Diinguihed eam S.» Team x ha won w x game already.» Team x and y play each oher g xy addiional ime.» I here any oucome of he remaining game in which eam finihe wih he mo (or ied for he mo) win? 5
Baeball Eliminaion: Max Flow Formulaion Can eam 3 finih wih mo win? Aume eam 3 win all remaining game w 3 + g 3 win. Divvy remaining game o ha all eam have w 3 + g 3 win. -2 game lef -4 2 eam 4 can ill win hi many more game -5 g 24 = 7 2-4 4 w 3 + g 3 - w 4 2-5 5 game node 4-5 eam node 52
Baeball Eliminaion: Max Flow Formulaion Theorem. Team 3 i no eliminaed iff max flow aurae all edge leaving ource. Inegraliy each remaining x-y game added o # win for x or y. Capaciy on (x, ) edge enure no eam win oo many game. -2 game lef -4 2 eam 4 can ill win hi many more game -5 g 24 = 7 2-4 4 w 3 + g 3 - w 4 2-5 5 game node 4-5 eam node 53
Baeball Eliminaion: Explanaion for Spor Wrier Team Win Loe To play Again =g ij i w i l i g i NY Bal Bo Tor NY 75 59 28-3 8 7 Balimore 7 63 28 3-2 7 Boon 69 66 27 8 2-0 Torono 63 72 27 7 7 0 - Deroi 49 86 27 3 4 0 0 De 3 4 0 - - AL Ea: Augu 30, 996 Which eam have a chance of finihing he eaon wih mo win? Deroi could finih eaon wih 49 + 27 = 76 win. 54
Baeball Eliminaion: Explanaion for Spor Wrier Team Win Loe To play Again =g ij i w i l i g i NY Bal Bo Tor NY 75 59 28-3 8 7 Balimore 7 63 28 3-2 7 Boon 69 66 27 8 2-0 Torono 63 72 27 7 7 0 - Deroi 49 86 27 3 4 0 0 AL Ea: Augu 30, 996 Which eam could finih he eaon wih mo win? Deroi could finih eaon wih 49 + 27 = 76 win. Cerificae of eliminaion. R = {NY, Bal, Bo, Tor} Have already won w(r) = 278 game. Mu win a lea r(r) = 27 more. Average eam in R win a lea 305/4 > 76 game. De 3 4 0 - - 55
Baeball Eliminaion: Explanaion for Spor Wrier Cerificae of eliminaion T " S, w(t ) :=! " # # win $ w i i#t # remaining game! $ " $ $ #, g(t ) := $ g x y {x, y} " T, If LB on avg # game won! #" # $ w(t)+ g(t) T > w z + g z hen z eliminaed (by ube T). Theorem. [Hoffman-Rivlin 967] Team z i eliminaed iff here exi a ube T* ha eliminae z. Proof idea. Le T* = eam on ource ide of min cu. 56
w l g NY Bal Tor Bo NY 90-6 4 Balimore 88 6-4 Torono 87 0 6-4 Boon 79 2 4 4 4 - g* = +6+ = 8 ( 90 + 87 + 6 ) / 2 > 9, o he e T = {NY, Tor} prove Boon i eliminaed. Noe: T={NY,Tor, Bal} i NOT a cerificae, ince (90+88+87+8)/3 = 9 Fig 7.2 Min cu no flow of value g*, o Boon eliminaed.
Baeball Eliminaion: Explanaion for Spor Wrier Pf of heorem. Ue max flow formulaion, and conider min cu (A, B). Define T* = eam node on ource ide of min cu. Oberve x-y A iff boh x T* and y T*. infinie capaciy edge enure if x-y A hen x A and y A if x A and y A bu x-y T*, hen adding x-y o A decreae capaciy of cu game lef y eam x can ill win hi many more game g 24 = 7 x-y x w z + g z - w x 58
Baeball Eliminaion: Explanaion for Spor Wrier Pf of heorem. Ue max flow formulaion, and conider min cu (A, B). Define T* = eam node on ource ide of min cu. Oberve x-y A iff boh x T* and y T*. g(s!{z}) > cap(a, B) capaciy of game edge leaving A!### "### $ = g(s!{z})! g(t*)!### "### $ + (w z + g z! w x ) capaciy of eam edge leaving A # x "T* = g(s!{z})! g(t*)! w(t*) + T* (w z + g z ) Rearranging: w z + g z < w(t*)+ g(t*) T* 59
Maching & Baeball: Key Poin Can (omeime) ake problem ha eemingly have nohing o do wih flow & reduce hem o a flow problem How? Build a clever nework; map allocaion of uff in original problem (mach edge; win) o allocaion of flow in nework. Clever edge capaciie conrain oluion o mimic original problem in ome way. Inegraliy ueful. 60
Maching & Baeball: Key Poin Furhermore, in he baeball example, min cu can be ranlaed ino a uccinc cerificae or proof of ome propery ha i much more ranparen han ee, I ran max-flow and i ay flow mu be le han g*. Thee example ugge why max flow i o imporan i a very general ool ued in many oher algorihm. 6