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

Slides:



Advertisements
Similar presentations
Maximum flow Main goals of the lecture:
Advertisements

Lecture 5: Network Flow Algorithms Max-Flow Min-Cut Single-Source Shortest-Path (SSSP) Job Sequencing.
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
1 EE5900 Advanced Embedded System For Smart Infrastructure Static Scheduling.
1 Maximum flow sender receiver Capacity constraint Lecture 6: Jan 25.
1 Review of some graph algorithms Graph G(V,E) (Chapter 22) –Directed, undirected –Representation Adjacency-list, adjacency-matrix Breadth-first search.
Algorithm Design and Analysis (ADA)
MAX FLOW APPLICATIONS CS302, Spring 2013 David Kauchak.
Introduction To Algorithms CS 445 Discussion Session 8 Instructor: Dr Alon Efrat TA : Pooja Vaswani 04/04/2005.
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2012 Lecture 19.
The Maximum Network Flow Problem. CSE Network Flows.
Chapter 10: Iterative Improvement The Maximum Flow Problem The Design and Analysis of Algorithms.
1 Maximum Flow w s v u t z 3/33/3 1/91/9 1/11/1 3/33/3 4/74/7 4/64/6 3/53/5 1/11/1 3/53/5 2/22/2 
Advanced Algorithm Design and Analysis (Lecture 8) SW5 fall 2004 Simonas Šaltenis E1-215b
1 COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf.
HW2 Solutions. Problem 1 Construct a bipartite graph where, every family represents a vertex in one partition, and table represents a vertex in another.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Lecture 4 Tuesday, 2/19/02 Graph Algorithms: Part 2 Network.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 4 Tuesday, 10/2/01 Graph Algorithms: Part 2 Network.
Maximum Flows Lecture 4: Jan 19. Network transmission Given a directed graph G A source node s A sink node t Goal: To send as much information from s.
Flows sourcesink s t Flows sourcesink edge-weights = capacities.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2004 Lecture 5 Wednesday, 10/6/04 Graph Algorithms: Part 2.
CSE 421 Algorithms Richard Anderson Lecture 22 Network Flow.
MAX FLOW CS302, Spring 2013 David Kauchak. Admin.
Maximum Flow Chapter 26.
Lecture 5: Network Flow Algorithms Single-Source Shortest-Path (SSSP) (Dijkstra's Algorithm) Max Flow - Min Cut (Ford-Fulkerson) Job Sequencing.
Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).
1 COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf.
CS 473Lecture ?1 CS473-Algorithms I Lecture ? Network Flows Finding Max Flow.
MAX FLOW APPLICATIONS CS302, Spring 2012 David Kauchak.
Max Flow – Min Cut Problem. Directed Graph Applications Shortest Path Problem (Shortest path from one point to another) Max Flow problems (Maximum material.
Honors Track: Competitive Programming & Problem Solving Fun with Graphs II Kevin Verbeek.
Maximum Flow Chapter 26. Flow Concepts Source vertex s – where material is produced Sink vertex t – where material is consumed For all other vertices.
CS223 Advanced Data Structures and Algorithms 1 Maximum Flow Neil Tang 3/30/2010.
Chapter 7 April 28 Network Flow.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 25.
Algorithm Design and Analysis (ADA)
Chapter 7 May 3 Ford-Fulkerson algorithm Step-by-step walk through of an example Worst-case number of augmentations Edmunds-Karp modification Time complexity.
1 EE5900 Advanced Embedded System For Smart Infrastructure Static Scheduling.
Data Structures & Algorithms Network Flow Richard Newman based on book by R. Sedgewick.
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
Contest Algorithms January 2016 Describe shortest path trees, SSSP, APSP, and three algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall 9. Shortest Paths.
CSEP 521 Applied Algorithms Richard Anderson Lecture 8 Network Flow.
1 Network Flow CSC401 – Analysis of Algorithms Chapter 8 Network Flow Objectives: Flow networks –Flow –Cut Maximum flow –Augmenting path –Maximum flow.
Fall 2003Maximum Flow1 w s v u t z 3/33/3 1/91/9 1/11/1 3/33/3 4/74/7 4/64/6 3/53/5 1/11/1 3/53/5 2/22/2 
CSE 421 Algorithms Richard Anderson Lecture 22 Network Flow.
Prof. Swarat Chaudhuri COMP 382: Reasoning about Algorithms Fall 2015.
Theory of Computing Lecture 12 MAS 714 Hartmut Klauck.
A directed graph G consists of a set V of vertices and a set E of arcs where each arc in E is associated with an ordered pair of vertices from V. V={0,
Maximum Flow Problem Definitions and notations The Ford-Fulkerson method.
Maximum Flow - Anil Kishore Graph Theory Basics. Prerequisites What is a Graph Directed, Weighted graphs How to traverse a graph using – Depth First Search.
Contest Algorithms January 2016 Describe a MST and the Prim and Kruskal algorithms for generating one. 8. Minimal Spanning Trees (MSTs) 1Contest Algorithms:
The Maximum Network Flow Problem
TU/e Algorithms (2IL15) – Lecture 8 1 MAXIMUM FLOW (part II)
Maximum Flow Chapter 26.
Maximum Flow Chapter 26.
CS4234 Optimiz(s)ation Algorithms
Algorithm Design and Analysis
Richard Anderson Lecture 23 Network Flow
Instructor: Shengyu Zhang
Lecture 10 Network flow Max-flow and Min-cut Ford-Fulkerson method
Flow Networks Topics Flow Networks Residual networks
Richard Anderson Lecture 23 Network Flow
Flow Networks General Characteristics Applications
Flow Networks and Bipartite Matching
Fundamental Data Structures and Algorithms
Algorithms (2IL15) – Lecture 7
Network Flow CSE 373 Data Structures.
EE5900 Advanced Embedded System For Smart Infrastructure
Richard Anderson Lecture 22 Network Flow
Lecture 22 Network Flow, Part 2
Presentation transcript:

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

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

Answer /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

 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 = = 10 outflow at v = = 10

A maxflow f in G is 19 Another Example

 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

 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

2.1 Example: initial G  Gf /20 0/10 0/20 0/10 0/ To make Gf less messy, 0 weighted edges are not drawn G to 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) Find an Augmented Path P in Gf One possible augmented path P: 0  1  2  Bottleneck b = min of edge weights in P = 20

 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

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

Regenerate Gf from Updated G /20 0/10 20/20 0/10 20/30 G to Gf remove 0 weight edges

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

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

Regenerate Gf from Updated G /20 10/10 20/20 10/10 10/30 G to Gf remove 0 weight edges

Find Augmented Path 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/10 20/20 10 /10 10/30 maxflow f = 30 = ∑ outflow of 0 = ∑ inflow of 3 = ∑ all bottlenecks b's = = 30

2.2 Another Example s v3 v4 t 16 v1 v

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

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 v s v3 v4 t v1 v

GfG b = 4

maxflow f = 23 (also = ∑ all b's) GfG b = 7 b =

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

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 )

 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

First Example Again

Generate Initial Gf /20 0/10 0/20 0/10 0/ G to Gf remove 0 weight edges

Gf  Gf b = 10 bfs prevents loops in the augmented path between s and t

Gf  Gf maxflow f = 30 = ∑ all b's b = 10

 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. MaxFlow ekData1.txt

 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

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

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

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

// 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

Example Execution Contest Algorithms:10. MaxFlow ekData1.txt

Example (Fig 4.24, CP) Contest Algorithms:10. MaxFlow ekData.txt

Execution Contest Algorithms:10. MaxFlow37

 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

Example capacity = = 34 don't count edges from B to A capacity = = 30 The minimum cut ( mincut ) problem: find a cut of minimum capacity.

 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

 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:

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

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

 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

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

 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

 Two edge-disjoint s-t paths: Solution

 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

 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

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