MAXIMUM FLOW inroducion Ford-Fulkeron algorihm maxflow-mincu heorem
Mincu problem Inpu. An edge-weighed digraph, ource verex, and arge verex. each edge ha a poiive capaciy capaciy 9 10 4 15 15 10 5 8 10 15 4 6 15 10 16 2
Mincu problem Def. A -cu (cu) i a pariion of he verice ino wo dijoin e, wih in one e A and in he oher e B. Def. I capaciy i he um of he capaciie of he edge from A o B. 10 5 15 capaciy = 10 + 5 + 15 4
Mincu problem Def. A -cu (cu) i a pariion of he verice ino wo dijoin e, wih in one e A and in he oher e B. Def. I capaciy i he um of he capaciie of he edge from A o B. 10 8 don' coun edge from B o A capaciy = 10 + 8 + 16 16 5
Mincu problem Def. A -cu (cu) i a pariion of he verice ino wo dijoin e, wih in one e A and in he oher e B. Def. I capaciy i he um of he capaciie of he edge from A o B. Minimum -cu (mincu) problem. Find a cu of minimum capaciy. 10 8 10 capaciy = 10 + 8 + 10 = 28 6
Maxflow problem Inpu. An edge-weighed digraph, ource verex, and arge verex. each edge ha a poiive capaciy capaciy 9 4 15 5 8 10 4 15 16 9
Maxflow problem Def. An -flow (flow) i an aignmen of value o he edge uch ha: Capaciy conrain: 0 edge' flow edge' capaciy. Local equilibrium: inflow = ouflow a every verex (excep and ). flow capaciy 5 / 9 inflow a v = 5 + 5 + 0 = 10 ouflow a v = 10 + 0 = 10 5 / 5 5 / 8 10 / 10 10 / 16 7
Maxflow problem Def. An -flow (flow) i an aignmen of value o he edge uch ha: Capaciy conrain: 0 edge' flow edge' capaciy. Local equilibrium: inflow = ouflow a every verex (excep and ). Def. The value of a flow i he inflow a. we aume no edge poin o or from 5 / 9 5 / 5 5 / 8 10 / 10 value = 5 + 10 + 10 = 25 10 / 16 8
Maxflow problem Def. An -flow (flow) i an aignmen of value o he edge uch ha: Capaciy conrain: 0 edge' flow edge' capaciy. Local equilibrium: inflow = ouflow a every verex (excep and ). Def. The value of a flow i he inflow a. Maximum -flow (maxflow) problem. Find a flow of maximum value. 8 / 9 5 / 5 8 / 8 10 / 10 value = 8 + 10 + 10 = 28 13 / 16 9
Summary Inpu. A weighed digraph, ource verex, and arge verex. Mincu problem. Find a cu of minimum capaciy. Maxflow problem. Find a flow of maximum value. 8 / 9 10 5 / 5 8 / 8 10 / 10 8 10 13 / 16 value of flow = 28 capaciy of cu = 28 Remarkable fac. Thee wo problem are dual! 15
Ford-Fulkeron algorihm Iniializaion. Sar wih 0 flow. iniializaion flow capaciy 0 / 9 value of flow 0 / 5 0 / 8 0 / 10 0 0 / 16 11
Idea: increae flow along augmening pah Augmening pah. Find an undireced pah from o uch ha: Can increae flow on forward edge (no full). Can decreae flow on backward edge (no empy). 1 augmening pah 0 / 9 boleneck capaciy = 10 10 0 / 5 0 / 8 0 / 10 0 + 10 = 10 0 / 16 12
Idea: increae flow along augmening pah Augmening pah. Find an undireced pah from o uch ha: Can increae flow on forward edge (no full). Can decreae flow on backward edge (no empy). 2 nd augmening pah 0 / 9 0 / 5 0 / 8 10 / 10 10 + 10 = 20 10 0 / 16 13
Idea: increae flow along augmening pah Augmening pah. Find an undireced pah from o uch ha: Can increae flow on forward edge (no full). Can decreae flow on backward edge (no empy). 3 rd augmening pah 5 0 / 9 backward edge (no empy) 5 5 0 / 5 0 / 8 10 / 10 20 + 5 = 25 10 / 16 20
Idea: increae flow along augmening pah Augmening pah. Find an undireced pah from o uch ha: Can increae flow on forward edge (no full). Can decreae flow on backward edge (no empy). 4 h augmening pah 8 5 / 9 backward edge (no empy) 5 / 5 8 5 / 8 10 / 10 25 + 3 = 28 13 1 0 10 / 16 15
Idea: increae flow along augmening pah Terminaion. All pah from o are blocked by eiher a Full forward edge. Empy backward edge. no more augmening pah 8 / 9 5 / 5 8 / 8 10 / 10 28 13 / 16 full forward edge empy backward edge 16
Ford-Fulkeron algorihm Ford- Fulkeron algorihm Sar wih 0 flow. While here exi an augmening pah: - find an augmening pah - compue boleneck capaciy - increae flow on ha pah by boleneck capaciy Queion. How o compue a mincu? How o find an augmening pah? If FF erminae, doe i alway compue a maxflow? Doe FF alway erminae? If o, afer how many augmenaion? 17
Relaionhip beween flow and cu Def. The ne flow acro a cu (A, B) i he um of he flow on i edge from A o B minu he um of he flow on i edge from B o A. ne flow acro cu = 5 + 10 + 10 = 25 5 / 9 5 / 5 5 / 8 10 / 10 value of flow = 25 10 / 16 18
Relaionhip beween flow and cu Def. The ne flow acro a cu (A, B) i he um of he flow on i edge from A o B minu he um of he flow on i edge from B o A. ne flow acro cu = 10 + 5 + 10 = 25 5 / 9 5 / 5 5 / 8 10 / 10 value of flow = 25 10 / 16 19
Relaionhip beween flow and cu Def. The ne flow acro a cu (A, B) i he um of he flow on i edge from A o B minu he um of he flow on i edge from B o A. ne flow acro cu = (10 + 10 + 5 + 10 + 0 + 0) (5 + 5 + 0 + 0) = 25 5 / 9 edge from B o A 5 / 5 5 / 8 10 / 10 value of flow = 25 10 / 16 20
Relaionhip beween flow and cu Flow-value lemma. Le f be any flow and le (A, B) be any cu. Then, he ne flow acro (A, B) equal he value of f. Inuiion. Conervaion of flow. Pf. By inducion on he ize of B. Bae cae: B = { }. Inducion ep: remain rue by local equilibrium when moving any verex from A o B. Corollary. Ouflow from = inflow o = value of flow. 21
Relaionhip beween flow and cu Weak dualiy. Le f be any flow and le (A, B) be any cu. Then, he value of he flow he capaciy of he cu. Pf. Value of flow f = ne flow acro cu (A, B) capaciy of cu (A, B). flow-value lemma flow bounded by capaciy 8 / 9 10 5 / 5 7 / 8 9 / 10 5 15 12 / 16 value of flow = 27 capaciy of cu = 30 22
Maxflow-mincu heorem Augmening pah heorem. A flow f i a maxflow iff no augmening pah. Maxflow-mincu heorem. Value of he maxflow = capaciy of mincu. Pf. The following hree condiion are equivalen for any flow f : i. There exi a cu whoe capaciy equal he value of he flow f. ii. f i a maxflow. iii. There i no augmening pah wih repec o f. [ i ii ] Suppoe ha (A, B) i a cu wih capaciy equal o he value of f. Then, he value of any flow f ' capaciy of (A, B) = value of f. Thu, f i a maxflow. weak dualiy by aumpion 30
Maxflow-mincu heorem Augmening pah heorem. A flow f i a maxflow iff no augmening pah. Maxflow-mincu heorem. Value of he maxflow = capaciy of mincu. Pf. The following hree condiion are equivalen for any flow f : i. There exi a cu whoe capaciy equal he value of he flow f. ii. f i a maxflow. iii. There i no augmening pah wih repec o f. [ ii iii ] We prove conrapoiive: ~iii ~ii. Suppoe ha here i an augmening pah wih repec o f. Can improve flow f by ending flow along hi pah. Thu, f i no a maxflow. 24
Maxflow-mincu heorem Augmening pah heorem. A flow f i a maxflow iff no augmening pah. Maxflow-mincu heorem. Value of he maxflow = capaciy of mincu. Pf. The following hree condiion are equivalen for any flow f : i. There exi a cu whoe capaciy equal he value of he flow f. ii. f i a maxflow. iii. There i no augmening pah wih repec o f. [ iii i ] Suppoe ha here i no augmening pah wih repec o f. Le (A, B) be a cu where A i he e of verice conneced o by an undireced pah wih no full forward or empy backward edge. By definiion of cu, i in A. Since no augmening pah, i in B. Capaciy of cu = ne flow acro cu = value of flow f. forward edge full; backward edge empy flow-value lemma 25
Compuing a mincu from a maxflow To compue mincu (A, B) from maxflow f : By augmening pah heorem, no augmening pah wih repec o f. Compue A = e of verice conneced o by an undireced pah wih no full forward or empy backward edge. 8 / 9 5 / 5 8 / 8 10 / 10 A 3 / 4 forward edge (no full) 16 / 16 full forward edge empy backward edge backward edge (no empy) 33
How o chooe augmening pah? Ue care when elecing augmening pah. Some choice lead o exponenial algorihm. Clever choice lead o polynomial algorihm. augmening pah number of pah implemenaion random pah E U randomized queue DFS pah E U ack (DFS) hore pah ½ E V queue (BFS) fae pah E ln(e U) prioriy queue digraph wih V verice, E edge, and ineger capaciie beween 1 and U 27
How o chooe augmening pah? Chooe augmening pah wih: Shore pah: fewe number of edge. Fae pah: max boleneck capaciy. Edmond- Karp 1972 (USA) Dinic 1970 (Sovie Union) 28
Flow nework repreenaion Flow edge daa ype. Aociae flow fe and capaciy ce wih edge e = v w. flow fe capaciy ce 7 / 9 Flow nework daa ype. Mu be able o proce edge e = v w in eiher direcion: include e in adjacency li of boh v and w. Reidual (pare) capaciy. Forward edge: reidual capaciy = ce fe. Backward edge: reidual capaciy = fe. Augmen flow. Forward edge: add. Backward edge: ubrac. reidual capaciy 2 7 forward edge backward edge 4
Flow nework repreenaion Reidual nework. A ueful view of a flow nework. include all edge wih poiive reidual capaciy original nework 9 / 9 4 / 4 4 / 5 0 / 8 4 / 10 reidual nework 9 backward edge (no empy) 4 15 1 8 6 4 4 Key poin. Augmening pah in original nework are in 1-1 correpondence wih direced pah in reidual nework. forward edge (no full) 5
Flow edge API public cla FlowEdge FlowEdge(in v, in w, double capaciy) in from() in o() in oher(in v) creae a flow edge v w verex hi edge poin from verex hi edge poin o oher endpoin double capaciy() capaciy of hi edge double flow() double reidualcapaciyto(in v) void addreidualflowto(in v, double dela) flow in hi edge reidual capaciy oward v add dela flow oward v flow fe capaciy ce reidual capaciy forward edge 2 7 / 9 7 backward edge 31
Flow edge: Java implemenaion public cla FlowEdge { privae final in v, w; privae final double capaciy; privae double flow; public FlowEdge(in v, in w, double { hi.v = v; hi.w = w; hi.capaciy = capaciy; } // from and o // capaciy // flow capaciy) flow variable (muable) public public public public in from() in o() double capaciy() double flow() { reurn { reurn { reurn { reurn v; } w; } capaciy; } flow; } public in oher(in verex) { if (verex == v) reurn w; ele if (verex == w) reurn v; ele hrow new IllegalArgumenExcepion(); } } public double reidualcapaciyto(in verex) public void addreidualflowto(in verex, double dela) {...} {...} nex lide 32
Flow edge: Java implemenaion (coninued) public double reidualcapaciyto(in verex) { if (verex == v) reurn flow; ele if (verex == w) reurn capaciy - flow; ele hrow new IllegalArgumenExcepion(); } forward edge backward edge public void addreidualflowto(in verex, double dela) { if (verex == v) flow -= dela; ele if (verex == w) flow += dela; ele hrow new IllegalArgumenExcepion(); } forward edge backward edge flow fe capaciy ce reidual capaciy forward edge 2 7 / 9 7 backward edge 33
Flow nework API public cla FlowNework void Ierable<FlowEdge> FlowNework(in V) FlowNework(In in) addedge(flowedge e) adj(in v) creae an empy flow nework wih V verice conruc flow nework inpu ream add flow edge e o hi flow nework forward and backward edge inciden o v Ierable<FlowEdge> edge() in V() in E() Sring osring() all edge in hi flow nework number of verice number of edge ring repreenaion Convenion. Allow elf-loop and parallel edge. 34
Flow nework: Java implemenaion public cla FlowNework { privae final in V; privae Bag<FlowEdge>[] adj; ame a EdgeWeighedGraph, bu adjacency li of FlowEdge inead of Edge public FlowNework(in V) { hi.v = V; adj = (Bag<FlowEdge>[]) new for (in v = 0; v < V; v++) } Bag[V]; adj[v] = new Bag<FlowEdge>(); public void addedge(flowedge e) { } in in v = e.from(); w = e.o(); adj[v].add(e); adj[w].add(e); add forward edge add backward edge } public Ierable<FlowEdge> { reurn adj[v]; } adj(in v) 35
Flow nework: adjacency-li repreenaion Mainain verex-indexed array of FlowEdge li (ue Bag abracion). inyfn.x V 6 E 8 0 1 2.0 0 2 3.0 1 3 3.0 1 4 1.0 2 3 1.0 2 4 1.0 3 5 2.0 4 5 3.0 adj[] 0 1 2 3 4 5 0 2 3.0 1.0 0 1 2.0 2.0 reference o he ame FlowEdge objec 1 4 1.0 0.0 1 3 3.0 2.0 0 1 2.0 2.0 2 4 1.0 1.0 2 3 1.0 0.0 0 2 3.0 1.0 3 5 2.0 2.0 2 3 1.0 0.0 1 3 3.0 2.0 4 5 3.0 1.0 2 4 1.0 1.0 1 4 1.0 0.0 4 5 3.0 1.0 3 5 2.0 2.0 Bag objec Noe. Adjacency li include edge wih 0 reidual capaciy. (reidual nework i repreened implicily) 36
Finding a hore augmening pah (cf. breadh-fir earch) privae boolean haaugmeningpah(flownework G, { edgeto = new FlowEdge[G.V()]; marked = new boolean[g.v()]; in, in ) Queue<Ineger> queue = new Queue<Ineger>(); queue.enqueue(); marked[] = rue; while (!queue.iempy()) { in v = queue.dequeue(); } for (FlowEdge e : G.adj(v)) found pah from o w { in he reidual nework? in w = e.oher(v); if (!marked[w] && (e.reidualcapaciyto(w) > 0) ) { edgeto[w] = e; ave la edge on pah o marked[w] = rue; w; mark w; queue.enqueue(w); add w o he queue } } } reurn marked[]; i reachable from in reidual nework? 37
Ford-Fulkeron: Java implemenaion public cla FordFulkeron { privae boolean[] marked; // rue if ->v pah in reidual nework priva FlowEdge[] // la edge on ->v e edgeto; double // pah value of flow priva value; e public FordFulkeron(FlowNework G, in, in ) { value = 0.0; compue edgeto[] while (haaugmeningpah(g,, )) { compue boleneck capaciy double bole = Double.POSITIVE_INFINITY; for (in v = ; v!= ; v = edgeto[v].oher(v)) bole = Mah.min(bole, edgeto[v].reidualcapaciyto(v)); for (in v = ; v!= ; v = edgeto[v].oher(v)) edgeto[v].addreidualflowto(v, bole); } } value += bole; augmen flow privae boolean haaugmeningpah(flownework G, in, { /* See previou lide. */ } in ) } public double value() { reurn value; } public boolean incu(in v) { reurn marked[v]; } i v reachable from in reidual nework? 38
Biparie maching problem N uden apply for N job. biparie maching problem I here a way o mach all uden o job? Each ge everal offer. 1 Alice Adobe Amazon Google 2 Bob Adobe Amazon 3 Carol Adobe Faceboo k Google 4 Dave Amazon Yahoo 5 Eliza Amazon Yahoo 6 Ado be Alice Bob Carol 7 Amazon Alice Bob Dave Eliza 8 Facebook Carol 9 Google Alice Caro l 10 Yahoo Dave Eliza 39
Biparie maching problem Given a biparie graph, find a perfec maching. perfec maching (oluion) biparie graph biparie maching problem Alice Bob Caro l Dave Eliza Google Adobe Facebook Yahoo Amazon 3 1 2 4 5 6 7 8 9 10 1 Alice Adobe Amazon Google 2 Bob Adobe Amazon 3 Carol Adobe Faceboo k Google 4 Dave Amazon Yahoo 6 Ado be Alice Bob Carol 7 Amazon Alice Bob Dave Eliza 8 Facebook Carol 9 Google Alice Caro l N uden N companie 5 Eliza Amazon Yahoo 10 Yahoo Dave Eliza 40
Nework flow formulaion of biparie maching Creae,, one verex for each uden, and one verex for each job. Add edge from o each uden (capaciy 1). Add edge from each job o (capaciy 1). Add edge from uden o each job offered (infinie capaciy). flow nework biparie maching problem 1 6 2 7 3 8 4 9 5 10 N uden N companie 1 Alice Adobe Amazon Google 2 Bob Adobe Amazon 3 Carol Adobe Faceboo k Google 4 Dave Amazon Yahoo 5 Eliza Amazon Yahoo 6 Ado be Alice Bob Carol 7 Amazon Alice Bob Dave Eliza 8 Facebook Carol 9 Google Alice Caro l 10 Yahoo Dave Eliza 41
Nework flow formulaion of biparie maching 1-1 correpondence beween perfec maching in biparie graph and ineger-valued maxflow of value N. flow nework biparie maching problem 1 2 3 4 5 6 7 8 9 10 1 Alice Adobe Amazon Google 2 Bob Adobe Amazon 3 Carol Adobe Faceboo k Google 4 Dave Amazon Yahoo 6 Ado be Alice Bob Carol 7 Amazon Alice Bob Dave Eliza 8 Facebook Carol 9 Google Alice Caro l N uden N companie 5 Eliza Amazon Yahoo 10 Yahoo Dave Eliza 42
Wha he mincu ell u Goal. When no perfec maching, explain why. 1 6 2 3 4 5 7 8 9 10 S = { 2, 4, 5 } T = { 7, 10 } uden in S can be mached only o companie in T S > T no perfec maching exi 43
Wha he mincu ell u Mincu. Conider mincu (A, B). Le S = uden on ide of cu. Le T = companie on ide of cu. Fac: S > T ; uden in S can be mached only o companie in T. 1 6 2 7 S = { 2, 4, 5 } 3 8 T = { 7, 10 } uden in S can be mached only o 4 9 companie in T S > T 5 10 no perfec maching exi Boom line. When no perfec maching, mincu explain why. 44