1 / 50

10. MaxFlow

10. MaxFlow. Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite matching and edge disjoint paths. 1. A Flow Network. 'pipe' capacity for 'water' flow. 1. 10. 20. 30. 0. 3. source. terminal (sink). 20. 10.

mshields
Download Presentation

10. MaxFlow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 10. MaxFlow Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite matching and edge disjoint paths Contest Algorithms: 10. MaxFlow

  2. 1. A Flow Network 'pipe' capacity for 'water' flow 1 10 20 30 0 3 source terminal (sink) 20 10 2 What is the maximum flow f from s to t?

  3. Answer flow / capacity 1 20/20 10/10 flows must be integer 10/30 0 3 t s 20/20 10/10 2 Maxflow f = 30 = ∑ outflow of s = ∑ inflow of t

  4. Flow Rules • Capacity constraint 0 ≤ edge's flow ≤ edge's capacity • Flow conservation inflow = outflow at every vertex (except s and t) inflow at v = 5 + 5 + 0 = 10 outflow at v = 10 + 0 = 10

  5. Another Example A maxflow f in G is 19

  6. 2. Ford-Fulkerson (FF) Algorithm • void FF(Graph G, Node s, Node t){ initialize flows on all edges in G to 0; change G into residual graph Gf; while (can find an augmented path P in Gf) {bottleneckb = minimum of edge weights in P;augment flow along P in G using b; regenerate Gf from updated G; } report maxflow f in G;}

  7. Graph  Residual Graph • Every flow/capacity edge in the graph G becomes two edges in the residual graph Gf forward edge: max amount can increase flow flow / capacity Gf 11 G 6 / 17 u v u v 6 values on edges called residual capacities backward edge: max amount can decrease flow

  8. 2.1 Example: initial G  Gf 0/10 1 1 20 0/20 10 G to Gf 0 0 0 3 0 3 0/30 0 30 10 0 0/10 0/20 0 20 2 2 1 20 10 To make Gf less messy, 0 weighted edges are not drawn. 0 3 30 10 20 2

  9. Find an Augmented Path P in Gf • In FF, an augmented path is any path from s to t in the residual graph Gf • forward and backward edges can be used (if not 0) 1 One possible augmented path P: 0 1 2 3 20 10 0 3 20 30 20 30 10 20 2 Bottleneck b = min of edge weights in P = 20

  10. Augment Flow along P in G using b • The flow fe on each edge e in G can change in two ways: • fe = fe+ b in G if the edge in P is a forward edge • i.e. increase the flow • fe = fe– b in G if the edge in P is a backward edge • i.e. decrease the flow

  11. Update G 0/10 1 1 0/20 20 10 G to Gf 0 3 3 0/30 0 30 10 0/10 0/20 20 2 2 0/10 1 One augmented path P: 0  1 2 3 20/20 augment flow 20 30 0 3 20 20/30 0/10 Bottleneck b = 20; 0 1 and 2 3 are the critical edges 20/20 2

  12. Regenerate Gf from Updated G 1 0 10 0/10 1 20/20 G to Gf 20 0 10 0 3 0 3 20 20/30 10 20 0 0 0/10 20/20 2 2 remove 0 weight edges 1 10 20 10 3 0 20 10 20 2

  13. Find Augmented Path 1 10 20 10 0 3 One augmented path P: 0 2 1 3 20 10 20 10 20 10 2 Bottleneck b = 10; 0 2 and 1 3 are the critical edges

  14. Update G 0/10 1 1 10 20/20 G to Gf 20 10 3 0 3 0 20/30 20 10 20 0/10 20/20 2 2 One augmented path P: 0 2 1 3 10/10 1 20/20 augment flow 10 20 10 0 3 10/30 Bottleneck b = 10 10/10 20/20 2

  15. Regenerate Gf from Updated G 1 0 0 10/10 1 20/20 G to Gf 20 10 20 0 3 10 0 3 10/30 0 20 10 0 10/10 2 20/20 2 remove 0 weight edges 1 20 10 20 0 3 10 20 10 2

  16. Find Augmented Path 1 There are no paths from 0 to 3 The while loop in FF() exits, and the maximum flow f in G is reported. 20 10 20 0 3 10 20 10 2 maxflow f = 30 = ∑ outflow of 0 = ∑ inflow of 3 = ∑ all bottlenecks b's = 20 + 10 = 30 10/10 1 20/20 0 3 10/30 10/10 20/20 2

  17. 2.2 Another Example v1 12 v3 20 16 t s 4 7 9 v2 v4 13 4 14

  18. Add Initial Flows of 0 v1 0/12 v3 0/20 0/16 t s 0/4 0/7 0/9 v2 v4 0/13 0/4 0/14

  19. Generate Initial Gf G to Gf 12 0/12 v1 v3 v1 v3 0/20 0 16 20 0/16 0/9 0 9 0 s t 0 s t 7 4 0/4 0/7 0 0 14 4 v2 v4 13 0/13 0 v2 v4 0/14 0/4 0 remove 0 weight edges 12 v1 v3 16 20 9 s t 7 4 14 4 13 v2 v4

  20. Gf G b = 4 b = 4 b = 4

  21. Gf G b = 7 2 b = 4 11 maxflow f = 23 (also = ∑ all b's) 2 11

  22. In the worst case, the running time of FF is O(E · |f|) • |f| might be very big depending on the problem. • It might be much larger than the number of edges.

  23. 3. Edmonds-Karp (EK) Algorithm void EK(Graph G, Node s, Node t){change G into residual graph Gf; maxflow = 0; while (can find an augmented SHORTEST path P in Gf) {bottleneckb = minimum of edge weights in P;augment flow along P in Gf using b; maxflow += bottleneck; }print maxflow;} Running time: O(V· E2)

  24. It's not necessary to keep regenerating Gf from G. All the flow changes are applied to Gf directly. • Finds an augmenting path using breadth-first search • bfs implemented using a queue and a loop

  25. First Example Again 1 10 20 30 0 3 20 10 2

  26. Generate Initial Gf 0/10 1 1 20 0/20 10 G to Gf 0 0 0 3 0 3 0/30 0 30 10 0 0/10 0/20 0 20 2 2 remove 0 weight edges 1 20 10 0 3 30 10 20 2

  27. Gf Gf 1 20 10 0 3 30 10 20 2 b = 10 1 10 10 bfs prevents loops in the augmented path between s and t 10 0 3 30 10 20 2 b = 10

  28. Gf Gf 1 10 10 10 30 0 3 10 10 10 2 b = 10 1 10 20 20 0 3 10 20 10 2 maxflow f = 30 = ∑ all b's

  29. Input Data Format • no. of vertices, source vertex, sink vertex • multiple lines, each line a list of neighbours • no. neighbours for u, followed by a series of (v, weight) pairs 4 0 3 2 1 20 2 10 2 2 30 3 10 1 3 20 0 ekData1.txt Contest Algorithms:10. MaxFlow

  30. Data Structures • An adjacency matrix for a directed, weighted graph • which is treated as the initial residual graph • A path is stored as a list of (u,v) pairs • The bfs code uses: • a queue of vertices • a Boolean visited[] array, to prevent cycles • a parentOf[] array of vertices which store the parents of node going from t to s in a path Contest Algorithms:10. MaxFlow

  31. Code see EdmondsKarp.java public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java EdmondsKarp <data-file>"); return; } Scanner sc = new Scanner(new File(args[0])); int numVs = sc.nextInt(); int source = sc.nextInt(); int sink = sc.nextInt(); // build residual graph without creating an ordinary graph first int[][] rGraph = new int[numVs][numVs]; for (int u = 0; u < numVs; u++) { int numNeighbors = sc.nextInt(); for (int j = 0; j < numNeighbors; j++) rGraph[u][sc.nextInt()] = sc.nextInt(); // (u, v) = weight } System.out.println("The maximum possible flow is " + edmondsKarp(rGraph, source, sink)); } // end of main() Contest Algorithms:10. MaxFlow

  32. public static int edmondsKarp(int[][] rGraph, int s, int t) { int maxFlow = 0; // Augment the flow while there is a path from source to sink ArrayList<IPair> path = null; while ((path = findShortestPath(rGraph, s, t)) != null) { System.out.print("Found: "); for (IPair p : path) System.out.print(p.getX() + " -> "); System.out.println(t); // Find minimum residual capacity of the edges along the path int bottleneck = Integer.MAX_VALUE; for (IPair p : path) bottleneck = Math.min(bottleneck, rGraph[p.getX()][p.getY()]); System.out.println(" Bottlneck: " + bottleneck); // update residual capacities along the path for (IPair p : path) { int u = p.getX(); int v = p.getY(); rGraph[u][v] -= bottleneck; // forward edge rGraph[v][u] += bottleneck; // backward edge } maxFlow += bottleneck; } return maxFlow; } // end of edmondsKarp() Contest Algorithms:10. MaxFlow

  33. public static ArrayList<IPair> findShortestPath(int[][] rGraph, int s, int t) /* Returns the shortest augmented path from source s to sink t in the residual graph. */ { // set all of visited array as not visited int numVs = rGraph.length; boolean visited[] = new boolean[numVs]; // false by default int parentOf[] = new int[numVs]; // enqueue source and mark as visited LinkedList<Integer> queue = new LinkedList<Integer>(); queue.add(s); visited[s] = true; parentOf[s] = -1; : Contest Algorithms:10. MaxFlow

  34. // BFS implemented using a queue and a loop while (queue.size() != 0) { int u = queue.poll(); for (int v = 0; v < numVs; v++) { if ((!visited[v]) && (rGraph[u][v] > 0)) { /* there's some residual capacity available for visiting the unvisited v */ queue.add(v); parentOf[v] = u; visited[v] = true; } } } if (visited[t]) { // have we got from source to sink? ArrayList<IPair> path = new ArrayList<IPair>(); for (int v = t; v != s; v = parentOf[v]) // from t to s path.add(0, new IPair(parentOf[v],v)); // add (u,v) so in rev order return path; } else return null; } // end of findShortestPath() Contest Algorithms:10. MaxFlow

  35. Example Execution 4 0 3 2 1 20 2 10 2 2 30 3 10 1 3 20 0 ekData1.txt Contest Algorithms:10. MaxFlow

  36. Example (Fig 4.24, CP) ekData.txt 4 0 1 2 2 70 3 30 2 2 25 3 70 3 0 70 3 5 1 25 3 0 30 2 5 1 70 Contest Algorithms:10. MaxFlow

  37. Execution Contest Algorithms:10. MaxFlow

  38. 4. The Mincut Problem • A cutis a partition of a flow network's vertices into two disjoint sets, with the source (s) in one set A and the sink (t) in the other set B. • A cut's capacity is the sum of the capacities of the edges from A to B. • The minimum cut (mincut) problem: find a cut of minimum capacity.

  39. Example don't count edges from B to A capacity = 10 + 5 + 15 = 30 capacity = 10 + 8 + 16 = 34 The minimum cut (mincut) problem: find a cut of minimum capacity.

  40. Maxflow == Mincut • The maxflowproblem • find a flow of maximum value • The mincutproblem • find a cut of minimum capacity • Two problems that appear very different but are actually two versions of the same question. • Maxflow-mincut theorem: maxflow value = mincut capacity

  41. Offers: 5. Bipartite Matching • Students apply for jobs. • Each student gets several offers. • Is there a way to match every student to a different job?

  42. As a Bipartite Graph Adobe Alice Amazon Bob Facebook Carol Google Dave IBM Eliza Yahoo Frank

  43. Maximize No. of 1-1 Edges Adobe Alice Amazon Bob Facebook Carol Google Dave A perfect match IBM Eliza Yahoo Frank

  44. Changing Bipartite to Maxflow • Direct edges from L to R, and set all capacities to 1. • Add source s, and link s to each node in L. • Add sink t, and link each node in R to t. 1 1 1 1 1

  45. Conversion to Maxflow Adobe Alice 1/1 1/1 1/1 Amazon Bob 1/1 1/1 1/1 1/1 Facebook Carol 1/1 1/1 1/1 s t 1/1 1/1 Google Dave 1/1 1/1 1/1 IBM Eliza 1/1 1/1 1/1 Yahoo Frank Other edges are 0/1

  46. 6. Edge Disjoint Paths • Find the max number of edge-disjoint s-t paths in a digraph. • two paths are edge-disjoint if they have no edge in common

  47. Solution • Two edge-disjoint s-t paths:

  48. Maxflow Formulation • Assign capacities of 1 to every edge. • Max no. of edge-disjoint s-to-t paths = maxflow 1/1 0/1 1/1 1/1 1/1 1/1 1/1 1/1 1/1 0/1 1/1 Other edges are 0/1

  49. 6.1. Network Connectivity • Related Problem: Find the min. no. of edges whose removal disconnects t from s. • A set of edges F disconnects t from s if each s-t paths uses at least one edge in F • removing F would make t unreachable from s Removing two edges breaks the s-t link

  50. The max no. of edge-disjoint s-t paths = min no. of edges whose removal disconnects t from s. utilizes the mincut

More Related