Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ. A Flow Network, terminology • Modeled as a flow graph which is a directed graph of G=(V,E). • Flow – a function f : V V R: Rate passing through the network from the source vertex s to the sinking vertex t. In literature called as s-t network. Net flow, |f(u,v)|. • Source is vertex from which all the edges are leaving ‘s’, and sink is the vertex, all the edges incident to ‘t’ . 2/15 s 0/9 5/14 a 4/19 t 2/5 b 3/3 A Flow Network, Constraints A flow is an assignment of real numbers xij to edges (i,j) of a given network that satisfy the following: flow-conservation requirements and capacity constraints • Capacity constraint: Each edge has a maximum capacity ‘ce’ allowed to transmit through such that.. Net flow cannot exceed ce… – u, v V: f(u, v) c(u, v) • Skew symmetry constraint: u, v V: f(u,v) = –f(v,u) • Flow conservation Law: The sum of the net flow incident to a vertex is equal to the net flow leaving the same vertex. – u, v V – {s, t}, ΣvVf(u, v)=0. for entire vertices. – What goes in = what leaves out: ΣvVf(u, V)- ΣvVf(V, v) =0, – s and t vertices: ΣvVf(v, s)=0, ΣvVf(t, v)=0, ΣvVf(s, v) = ΣvVf(v, t)>0 and R+, 2/15 • No leakage and no memory is allowed. • Total value of flow, |f| = f(s, V) = f(V, t) s 0/9 5/14 a 4/19 t 2/5 b • Among the areas applied are traffic, freight, airflow, hydrodynamic, communications, and stocks, companies. • The Maximum Flow Problem Definition: is to find a feasible flow through a single-source, single-sink flow network that is maximum. 3/3 • Opposite flows bt two vertices (due to skew symmetry) will cancel each other. • Super source and super sink connected with infinite capacity. 2/13 • Find paths to maximize |f|. 2/19 a b 3/15 • f(X, Y) means ΣxXΣyYf(x, y) 1/10 1/4 9 1/5 s • f(u, V – {s, t}) = 0 1/14 Lucky Puck Distribution Network c 3/11 d 2/3 Max Flow, |f| = 19 Or is it? Best we can do? t • For X, Y, Z V with X Y =, • f(X Y, Z) = f(X, Z) + f(Y, Z) flow between any x and y through Z…. • f(Z, X Y) = f(Z, X) + f(Z, Y) • The value of a flow is the total net flow into the sink; that is, |f| = f(V, t), and from the source s |f| = f(s, V) (by definition) = f(V-{V-s}, V) = f(V, V) - f(V - s, V) = f(V, V - s) |f| = f(V, t) = f(V, t) + f(V, V - s - t) = f(V, t) (by flow conservation) Cancellation of flows 2/15 s 2/9 5/14 a t 5/5 b 2/15 5/19 2/3 s 0/9 5/14 a 5/19 t 3/5 b 2/3 Maximum Flow as LP . Minimum Cost Flow . Ford-Fulkerson method • • Initialize flow value to 0. Start to iterate (dynamic or greedy!).. Find augmenting paths – Find a path p from s to t, such that there is some flow value x > 0, and for each edge (u, v) in p, add x units of flow to |f|, such that max{x} min{cf(u, v): (u, v) is on p}. – Increment flow, f(u, v) = f(u, v) + x • Calculate residual capacities: cf(u,v) = c(u,v) – x i.e. the actual capacity minus the net flow (x) from u to v Flow may be negative!! That can be interpreted as increasing residual capacity. • Residual network: Gf =(V, Ef), where • • Continue till no any augmenting path (any forward channel) left through Observation – edges in Ef are either edges in E or their reversals: |Ef| 2|E| Ef = {(u,v) V V : cf(u,v) > 0} Augmenting Path? a 15 10 4 s 14 13 c 11 b 9 19 5 d t 3 Ford-Fulkerson method • Contains several algorithms: – Residue networks – Augmenting paths • Find a path p from s to t (augmenting path), such that there is some value x > 0, and for each edge (u,v) in p we can add x units of flow – f(u,v) + x c(u,v) Augmenting Path? 10/15 s 6/14 a 2/4 c 8/13 10 b 9 8/11 13/19 t 5/5 d 3/3 Residual Network • To find augmenting path we can find any path in the residual network: – Residual capacities: cf(u,v) = c(u,v) – f(u,v) • i.e. the actual capacity minus the net flow from u to v • Net flow may be negative – Residual network: Gf =(V,Ef), where Ef = {(u,v) V V : cf(u,v) > 0} – Observation – edges in Ef are either edges in E or their reversals: |Ef| 2|E| 5/15 Sub-graph With c(u,v) and f(u,v) a b 0/14 5/6 c Residual Sub-Graph 10 a b 19 1 c 5 Residual Graph • Compute the residual graph of the graph with the following flow: 10/15 s 6/14 a 2/4 c 8/13 10 b 9 8/11 13/19 t 5/5 d 3/3 Residual Capacity and Augmenting Path • Finding an Augmenting Path – Find a path from s to t in the residual graph – The residual capacity of a path p in Gf: cf(p) = min{cf(u,v): (u,v) is in p} • i.e. find the minimum capacity along p – Doing augmentation: for all (u,v) in p, we just add this cf(p) to f(u,v) (and subtract it from f(v,u)) – Resulting flow is a valid flow with a larger value. Residual network and augmenting path f(p1:(s, v1),…) = 11, as far as cf(s,v1)<16, and as far as sum v1=0, and f(p2:(s, v2),…), calculate final residual graph Gf, and next search augmentation path. The Ford-Fulkerson method Ford-Fulkerson(G,s,t) 1 for each edge (u,v) in G.E do 2 f(u,v) f(v,u) 0 3 while there exists a path p from s to t in residual network Gf do 4 cf = min{cf(u,v): (u,v) is in p} 5 for each edge (u,v) in p do 6 f(u,v) f(u,v) + cf 7 f(v,u) -f(u,v) 8 return f The algorithms based on this method differ in how they choose p in step 3. Steps 1, 2, O(E), and 4-7 O(E) times for each unit of |f*|. If chosen poorly the algorithm might not terminate. Execution of Ford-Fulkerson (1) Left Side = Residual Graph Right Side = Augmented Flow Execution of Ford-Fulkerson (2) Left Side = Residual Graph Right Side = Augmented Flow Path augmenting scenarios Fig 27.4 of CLSR: The flow network G with f. The residual network Gf with augmenting path p shaded; its residual capacity is cf(p) = c(v2, v3) = 4. (c) The flow in G that results from augmenting along path p by its residual capacity 4. (d) The residual network induced by the flow in (c). The Ford-Fulkerson algorithm, Residual Capacity and Augmenting Path In each iteration find any path augmenting the flow along by the residual capacity câ(p). Following algo computes the max f in G = (V, E) by updating the net â[u, v] between connected vertices; u, v. If not connected, implicit assumption is that â[u, v] = 0. Assumption: the c(u, v) is a constant-time function, which is 0 if (u, v) E. c(u, v) might be derived from fields stored within vertices and their adjacency lists. The residual capacity: câ(u, v).. Here the câ(p) is a temporary variable for the câ(u, v) of the path p. () refers a function [] refers a mutable identifier. Ford-Fulkerson(G,s,t) 1 for each edge (u,v) G.E do //Initializing 2 f(u,v) f(v,u) 0 3 while path p from s to t in residual network Gf do *** 4 cf = min{cf(u,v): (u,v) p} // determining augmentation 5 for each edge (u,v) p do // updating residual capacities, new Gf 6 f(u,v) f(u,v) + cf 7 f(v,u) -f(u,v) 8 return f T(n) depends on how line 3 is implemented and the algorithms based on this differ in how to determine p. If chosen poorly the algorithm might not terminate. a 16 10 4 s 13 c 12 9 b 20 7 14 d t Execution 4 Residual Graphs Augmented Flow Execution of Ford-Fulkerson Residual Graph Augmented Flow 5/11 a 3/11 s 13 c 12 b 9 7 3/11 d 1/19 t 4 Observe that terminates when there is no a path from s to t whose all edges are in forward direction. And observe that the edges whose capacities are depleted form bottleneck, which is the minimum cut and their total capacity of the maximum achivible. • Does the method find the maximum flow? Yes, if program can get to the point where the residual graph has no path from s to t. Which is the moment when the G(V, E) is partitioned into two disjoint sets, by a definite split, minimum cut, c(S, V-S) = c(S, T), such that s S and t T. • And the net flow (f(S,T)) through the cut (or the capacity) is the total capacity of the flows f(u,v), where s S and t T which includes negative flows back from T to S as well. Think about it from the graph… • Minimum cut – a cut with the smallest capacity of all cuts. |f|= f(S,T) i.e. the value of a max flow is equal to the capacity of a min cut. 10/15 s 6/14 Cut capacity = 24 a 2/4 c 8/13 10 b 9 8/11 13/19 t 5/5 d 3/3 Min Cut capacity = 21 Cut properties.. • A cut (S, T) of flow network G = (V, E) is a partition (or a disjoint set) of V into S and T = V - S such that s S and t T. MST cut was for undirected graphs, here a directed graph is cut such that (tails) s S and (heads) t T.) If f is a flow, then the net flow across the cut (S, T) is defined to be â(S, T). The capacity of the cut (S, T) is c(S, T). • f(S, T) = ΣuSΣvTf(u, v) ΣuSΣvTc(u, v) = c(S, T). • The value of any flow â in a flow network G is bounded from above by the capacity of any cut of G. • Proof Let (S, T) be any cut of G and let â be any flow. By |â| = â(S, T) (Lemma 27.5 clrs) and the capacity constraints, • = ΣuSΣvTf(u, v) ΣuSΣvTc(u, v) = c(S, T) Max-flow min-cut theorem If f is a flow in a flow network G=(V, E) with source s and sink t, then the following conditions are equivalent: 1. f is a maximum flow in G. 2. The residual network Gf contains no augmenting paths. 3. Flow value of f = c(S, T) for some cut (S,T) of G. (This cut is called min-cut. Bottleneck. Among all the cuts of G(S, T), it is the cut that returns the minimum capacity.) Proof 1→2 Use contradiction. If it does not hold, then the residual network Gf must have some augmenting paths. So the net flow |f| could be further increased which contradicts to that |f| is a maximum flow in G. 2→3 Suppose that Gf has no augmenting path, that is, that Gf contains no path from s to t. Define S={vV: there exists a path from s to v in Gf} and T=V-S. The partition (S,T) is a cut: we have s S trivially and t T because there is no path from s to t in Gf. For each pair of vertices u and v such that uS and vT, we have f(u,v)=c(u,v) since otherwise (u,v)Ef, which would place v in set S. Thus, flow value of f=c(S,T). (Lemma: flow value f=f(S,T)) The end of Proof 3→1 The value of any flow f in a flow network G is bounded from above by the capacity of any cut of G. flow value f(S, T)=ΣuSΣvTf(u, v) ΣuSΣvTc(u, v) = c(S, T). so the cut capacity c(S,T) is the upper bound of flow f. Now the flow value f=c(S,T) thus implies that f is a maximum flow. Worst case analysis • In the worst case, suppose that at each step Ford-Fulkerson’s algorithm performs some small 1000000 incremental (integer) s augmentations of ∆f, then the number of iterations to reach to the 1000000 maximum flow, |f*|/∆f, where f* is maximum flow. v 1/1 p u 1 v • In the given example (clrs) programs starts to traverse through path p1, suvt and alternates to the path p2, svut. instead of completing the algorithm in two steps. 1000000 1 p s 1000000-1/1 2 u 1000000-1/1 s • The running time of FordFulkerson’s algorithm is O(|f*|(E)) 1/1000000-1 1000000 v 1 p u t 1000000 1/1000000-1 t 1000000 1000000-1/1 t 1 1/1000000-1 Edmonds-Karp algorithm • If searching path p in line 3, is implemented with a BFS in the residual network, and if the augmentation path is a shortest path from s to t, regardless of the weights of edges, • Edmonds-Karp algorithm. O(VE2), take shortest augmentation path. • How do we find such a shortest path? DFS or BFS.. • The number of augmenting paths needed by the shortest-augmenting-path algorithm never exceeds nm/2, where n and m are the number of vertices and edges, respectively • Since the time required to find shortest augmenting path by breadth-first search is in O(n+m)=O(m) for networks represented by their adjacency lists, the time efficiency of the shortest-augmenting-path algorithm is in O(VE2) or O(nm2) for this representation. Running time O(VE2), because the number of augmentations is O(VE) • • • More efficient algorithms have been found that can run in close to O(nm) time BFS and AP and Maximum Capacity Path (MCP) - find an A.P. that maximizes incremental flow • • • • How many augmenting paths for each method? Edmonds-Karp - A.P.s to give overall time. Critical edge on A.P. No capacity will remain on a critical edge after A.P. is recorded Observations: Edge may become critical several times. A vertex cannot get closer to source in later rounds of BFS. s a Dist i First time critical b Dist i+1 s Later Second time critical s a Dist > i+2 a Dist > i+2 • Each augmentation has complexity O(E) • Numbers of augmentations is O(VE) – Each edge can be a critical edge at most |V|/2-1 times – Once edge is critical, its flow is saturated – Must appear in path the other way next – Since previous path is shortest, this reversing path must be two edges longer df’(s,v)=df(s,u)+1 df’(s,u)=df’(s,v)+1 ≥df(s,v)+1 (paths increase monotonically) =df(s,u)+2 t b Dist > i+1 b Dist > i+3 t t The number of distances available for the tail of a repeated critical edge is (V-2)/2. The number of edges that become critical is < E. Thus, A.P.s overall. Flow must be sent in opposite direction by another A.P. before an edge can become critical again. Shortest-Augmenting-Path Algorithm Generate augmenting path with the least number of edges by BFS. Start at s, perform BFS traversal by marking new (unlabeled) vertices with two labels and a sign mark: • 1st label – indicates the amount of additional flow that can be brought from the previous to the current vertex being labeled. • 2nd label – indicates the vertex from which the vertex being labeled was reached, with “+” or “–” in forward or in backward, respectively. • Vertex labeling: The start is always labeled with ∞,• All other vertices are labeled as follows: If unlabeled vertex j is connected to the front vertex i with the edge – from i to j with unused capacity rij = uij –f(uij, vij) (forward edge), vertex j is labeled with lj, i+, where lj = min{li, rij}. – from j to i with positive flow xji (backward edge), vertex j is labeled lj,i-, where lj = min{li, xji} • If the sink ends up being labeled, then there is an augmentation path by the amount min{cp} which indicated is with the sink’s first label. • The augmentation path is traced along the path from sink to source; the current flow quantities are increased on the forward edges and decreased on the backward edges of this path. • If the sink remains unlabeled after the traversal queue becomes empty, the algorithm returns the current flow as maximum and terminates. 5 0/3 0/2 s 2 0/3 0/4 0/5 0/1 3 0/2 5 0/3 t s 2,2+ 0/4 ∞,- 2,1+ 0/2 0/5 2 2,2+ 4 Queue: 1 2 4 3 5 6 ↑↑↑↑ Augment the flow by 2 (the sink’s first label) along the path 1→2→3→6 0/2 3 0/3 0/1 4 3,1+ t 2,3+ 5 0/3 2/2 s 0/4 2/5 2 3 2/2 1,2+ 5 t 0/3 0/3 0/1 4 ∞,s 1,32/2 2 0/3 1/3 2/2 1/3 4 Queue: 1 4 ↑↑ 4 1/4 1/5 2/2 t 0/1 5 2 1,4+ 2/5 3 1,5+ Queue: 1 4 3 2 5 6 ↑↑↑↑↑ s 0/4 3 2/2 3,1+4 Augment the flow by 1 (the sink’s first label) along the path 1→4→3←2→5→6 t 5 1/1 1/3 ∞,s 2/2 1/3 2 4 1/4 1/5 3 2/2 t 1/1 2,1+ No augmenting path (the sink is unlabeled) the current flow is maximum • S : (∞, 1, +) • B : (S, 8, +) • E : (S, 28, +) • F : (S, 15, +) • A : (B, 8, +) • C : (B, 8, +) • D : (B, 6, -) • T : (A, 8, +) • f updated by 8. P = (S,B, A, T) Multiple Sources or Sinks • What if you have a problem with more than one source and more than one sink? • Modify the graph to create a single supersource and supersink a 15 s 10 4 14 4 x c e 15 g b d 13 11 f 9 t 3 13 h i 14 j c e 15 y 3 4 g b d 13 11 f 9 13 k 5 9 11 10 4 14 13 10 4 s 5 a 15 13 5 9 11 10 4 14 13 3 t 13 l 5 h 3 Application – Bipartite Matching • Example – given a community with n men and m women • Assume we have a way to determine which couples (man/woman) are compatible for marriage – E.g. (Joe, Susan) or (Fred, Susan) but not (Frank, Susan) • Problem: Maximize the number of marriages – No polygamy allowed – solve this problem by creating a flow network out of a bipartite graph Bipartite Graph • A bipartite graph is an undirected graph G=(V,E) in which V can be partitioned into two sets V1 and V2 such that (u,v) E implies either u V1 and v V2 or vice versa. • That is, all edges go between the two sets V1 and V2 and not within V1 and V2. Model for Matching Problem • Men on leftmost set, women on rightmost set, edges if they are compatible A X B Y C B B Y Y C C D X X Z Men A A Z D Women A matching Z D Optimal matching Solution Using Max Flow • Add a supersouce, supersink, make each undirected edge directed with a flow of 1 A A X X B Y C s B Y C Z D Z D Since the input is 1, flow conservation prevents multiple matchings t