Presentation is loading. Please wait.

Presentation is loading. Please wait.

Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite.

Similar presentations


Presentation on theme: "Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite."— Presentation transcript:

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

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

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

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

5 A maxflow f in G is 19 Another Example

6  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) { bottleneck b = minimum of edge weights in P; augment flow along P in G using b; regenerate Gf from updated G; } report maxflow f in G; } 2. Ford-Fulkerson (FF) Algorithm

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

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

9  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) Find an Augmented Path P in Gf 0 1 2 3 20 10 20 10 30 One possible augmented path P: 0  1  2  3 2030 20 Bottleneck b = min of edge weights in P = 20

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

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

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

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

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

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

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

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

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

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

20 GfG b = 4

21 maxflow f = 23 (also = ∑ all b's) GfG b = 7 b = 4 2 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 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) { bottleneck b = minimum of edge weights in P; augment flow along P in Gf using b; maxflow += bottleneck; } print maxflow; } 3. Edmonds-Karp (EK) Algorithm Running time: O(V· E 2 )

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 0 1 2 3 20 10 20 10 30

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

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

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

29  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 Input Data Format Contest Algorithms:10. MaxFlow29 4 0 3 2 1 20 2 10 2 2 30 3 10 1 3 20 0 ekData1.txt

30  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 Data Structures Contest Algorithms:10. MaxFlow30

31 public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java EdmondsKarp "); 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() Code Contest Algorithms:10. MaxFlow31 see EdmondsKarp.java

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 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. MaxFlow32

33 public static ArrayList 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 queue = new LinkedList (); queue.add(s); visited[s] = true; parentOf[s] = -1; : Contest Algorithms:10. MaxFlow33

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 path = new ArrayList (); 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. MaxFlow34

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

36 Example (Fig 4.24, CP) Contest Algorithms:10. MaxFlow36 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 ekData.txt

37 Execution Contest Algorithms:10. MaxFlow37

38  A cut is 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. 4. The Mincut Problem

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

40  The maxflow problem  find a flow of maximum value  The mincut problem  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 Maxflow == Mincut

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

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

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

44  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. Changing Bipartite to Maxflow 1 1 1 1 1

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

46  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 6. Edge Disjoint Paths

47  Two edge-disjoint s-t paths: Solution

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

49  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 6.1. Network Connectivity 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


Download ppt "Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite."

Similar presentations


Ads by Google