Honors Track: Competitive Programming & Problem Solving Fun with Graphs II Kevin Verbeek.

Slides:



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

1 EE5900 Advanced Embedded System For Smart Infrastructure Static Scheduling.
1 Maximum flow sender receiver Capacity constraint Lecture 6: Jan 25.
R. Johnsonbaugh Discrete Mathematics 5 th edition, 2001 Chapter 8 Network models.
1 Review of some graph algorithms Graph G(V,E) (Chapter 22) –Directed, undirected –Representation Adjacency-list, adjacency-matrix Breadth-first search.
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.
MAXIMUM FLOW Max-Flow Min-Cut Theorem (Ford Fukerson’s Algorithm)
CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007.
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 
1 Chapter 7 Network Flow Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Advanced Algorithm Design and Analysis (Lecture 8) SW5 fall 2004 Simonas Šaltenis E1-215b
Lectures on Network Flows
The max flow problem
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.
Approximation Algorithms Motivation and Definitions TSP Vertex Cover Scheduling.
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.
1 Ford-Fulkerson method Ford-Fulkerson(G) f = 0 while( 9 simple path p from s to t in G f ) f := f + f p output f Runs in time O(|f max | |E|) where f.
MAX FLOW CS302, Spring 2013 David Kauchak. Admin.
1 Introduction to Approximation Algorithms. 2 NP-completeness Do your best then.
Maximum Flow Chapter 26.
MST and Max Flow CS3233. Overview Two Graph Problems Minimum Spanning Tree Maximum Flow/Minimum Cut Problem One Data Structure Disjoint Sets.
Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).
MAX FLOW APPLICATIONS CS302, Spring 2012 David Kauchak.
Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Network Flow Longest Paths.
Maximum Flow Problem (Thanks to Jim Orlin & MIT OCW)
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.
15.082J & 6.855J & ESD.78J October 7, 2010 Introduction to Maximum Flows.
15.082J and 6.855J March 4, 2003 Introduction to Maximum Flows.
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.
CSEP 521 Applied Algorithms Richard Anderson Lecture 8 Network Flow.
CSE 421 Algorithms Richard Anderson Lecture 22 Network Flow.
Theory of Computing Lecture 12 MAS 714 Hartmut Klauck.
1 CPSC 320: Intermediate Algorithm Design and Analysis July 14, 2014.
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.
Honors Track: Competitive Programming & Problem Solving Seminar Topics Kevin Verbeek.
The Maximum Network Flow Problem
Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite.
TU/e Algorithms (2IL15) – Lecture 8 1 MAXIMUM FLOW (part II)
Honors Track: Competitive Programming & Problem Solving Push-Relabel Algorithm Claire Claassen.
The Maximum Network Flow Problem
CS4234 Optimiz(s)ation Algorithms
Lectures on Network Flows
Richard Anderson Lecture 23 Network Flow
Lecture 22 Network Flow, Part 2
Max Flow min Cut.
Special Graphs: Modeling and Algorithms
Bipartite Matching and Other Graph Algorithms
The Taxi Scheduling Problem
Instructor: Shengyu Zhang
Lecture 10 Network flow Max-flow and Min-cut Ford-Fulkerson method
Lecture 19-Problem Solving 4 Incremental Method
Flow Networks Topics Flow Networks Residual networks
Max Flow Min Cut, Bipartite Matching Yin Tat Lee
Flow Networks and Bipartite Matching
Algorithms (2IL15) – Lecture 7
Maximum Flow Neil Tang 4/8/2008
Lecture 22 Network Flow, Part 2
Special Graphs: Modeling and Algorithms
Maximum Bipartite Matching
Richard Anderson Lecture 22 Network Flow
Presentation transcript:

Honors Track: Competitive Programming & Problem Solving Fun with Graphs II Kevin Verbeek

Graph algorithms Standard Algorithms DFS BFS Single source shortest path All-pairs shortest path Minimum spanning tree Euler tour Bipartite matching Max-flow Fun with Graphs I Fun with Graphs II

Bipartite graphs Bipartite graph A graph G = (A U B, E) such that E ⊆ A x B A B Other definitions Bipartite graphs are graphs that are 2-colorable Bipartite graphs are graphs that do not have odd-length cycles

Motivation Bipartite graphs: Model relations between two classes of objects Examples Boys and girls: “boy x likes girl y” A B

Motivation Bipartite graphs: Model relations between two classes of objects Examples Boys and girls: “boy x likes girl y” Players and clubs: “player x wants to play in club y” A B

Motivation Bipartite graphs: Model relations between two classes of objects Examples Boys and girls: “boy x likes girl y” Players and clubs: “player x wants to play in club y” Employees and jobs: “employee x can perform job y” A B

Bipartite matching Problem If every employee can complete at most one job, how many jobs can be completed? A B

Bipartite matching Problem If every employee can complete at most one job, how many jobs can be completed? A B Bipartite matching Subset of edges without common vertices

Bipartite matching Problem If every employee can complete at most one job, how many jobs can be completed? A B Maximum bipartite matching Bipartite matching with maximum number of edges

Max-flow problem Maximum flow problem Intuition: Given a network of pipes and a source and sink, how much water can be transported from source to sink?

Max-flow problem Maximum flow problem Input: A graph with edge capacities c ij and a source s and sink t Output: The maximum flow f ij satisfying the flow constraints Flow constraints 0 ≤ f ij ≤ c ij “flow is within capacity” Σ k f ki = Σ k f ik for all i ≠ s, t “flow in = flow out” s = 0 t = c 01 c 02 c 12 c 13 c 14 c 24 c 35 c 45 c 43

Max-flow problem Why is this non-trivial? How about dynamic programming? Compute the maximum flow to each node Subproblems are not independent! s = 0 t = /3 0/2 0/1 0/2 0/3 0/2 0/1

Flow Flow constraints 0 ≤ f ij ≤ c ij “flow is within capacity” Σ k f ki = Σ k f ik for all i ≠ s, t “flow in = flow out” Conventions f ij = -f ji magnitude of flow = Σ k f sk s = 0 t = /3 2/3 0/2 2/2 1/1 2/2 3/3 2/2 1/1 How can we increase the flow?

Increase flow on edge Idea: Increase flows on edges until we found the maximum flow s = 0 t = /3 0/2 0/1 0/2 0/3 0/2 0/1

Increase flow on edge Idea: Increase flows on edges until we found the maximum flow s = 0 t = /3 3/3 0/2 0/1 0/2 0/3 0/2 0/1 Problem: We violate the flow constraints! How can we increase the flow without violating flow constraints?

Increase flow on paths Idea: Find path from s to t and increase flow on the entire path s = 0 t = /3 0/2 0/1 0/2 0/3 0/2 0/1

Increase flow on paths Idea: Find path from s to t and increase flow on the entire path s = 0 t = /3 0/3 2/2 0/2 0/1 2/2 0/3 2/2 0/1 Problem: The bottom edge is blocked! How can we avoid making bad choices?

Residual graph Graph with updated capacities reflecting current flow c’ ij = c ij – f ij Since f ij = -f ji reverse edges may appear s = 0 t = /3 0/3 2/2 0/2 0/1 2/2 0/3 2/2 0/1

Residual graph Graph with updated capacities reflecting current flow c’ ij = c ij – f ij Since f ij = -f ji reverse edges may appear Edges with 0 capacity should be ignored s = 0 t = /3 0/3 2/2 0/2 0/1 2/2 0/3 2/2 0/1 0/2

Augmenting paths Augmenting path Path in residual graph from s to t Increasing flow on augmenting path can reduce flow on some edges But the magnitude of the flow always grows s = 0 t = /3 0/3 2/2 0/2 0/1 2/2 0/3 2/2 0/1

Augmenting paths Augmenting path Path in residual graph from s to t Increasing flow on augmenting path can reduce flow on some edges But the magnitude of the flow always grows s = 0 t = /3 0/3 2/2 0/2 0/1 2/2 0/3 2/2 0/1

Augmenting paths Augmenting path Path in residual graph from s to t Increasing flow on augmenting path can reduce flow on some edges But the magnitude of the flow always grows s = 0 t = /3 0/2 2/2 0/1 2/2 2/3 2/2 0/1

Augmenting paths Augmenting path Path in residual graph from s to t Increasing flow on augmenting path can reduce flow on some edges But the magnitude of the flow always grows s = 0 t = /3 0/2 2/2 0/1 2/2 2/3 2/2 0/1

Augmenting paths Augmenting path Path in residual graph from s to t Increasing flow on augmenting path can reduce flow on some edges But the magnitude of the flow always grows s = 0 t = /3 2/3 0/2 2/2 1/1 2/2 3/3 2/2 1/1 Lemma An augmenting path exists if and only if the flow is not optimal

Max-flow algorithm Algorithm MaxFlow(G, s, t) 1. Initialize f ij = 0 for all i, j 2. while augmenting path exists do 3. Increase flow along augmenting path 4. return f ij How to compute augmenting path? DFS ➨ Ford-Fulkerson algorithm with O(E f*) running time BFS ➨ Edmonds-Karp algorithm with O(V E 2 ) running time

Max-flow to bipartite matching Can we solve bipartite matching with maximum flow? A B

Max-flow to bipartite matching Can we solve bipartite matching with maximum flow? A B s t Notes Graph must be directed! Flow is at most O(V) ➨ Ford-Fulkerson is O(E V) Can play with capacities for more general setting

Max-flow to bipartite matching Can we solve bipartite matching with maximum flow? A B s t Notes Graph must be directed! Flow is at most O(V) ➨ Ford-Fulkerson is O(E V) Can play with capacities for more general setting

Max-flow to bipartite matching Can we solve bipartite matching with maximum flow? A B s t Notes Graph must be directed! Flow is at most O(V) ➨ Ford-Fulkerson is O(E V) Can play with capacities for more general setting

Max-flow implementation Implementation Must keep track of edge data Must have full augmenting path information class Node { ArrayList adj; boolean visited; Edge parent; // reference to edge to parent to update flow int flow; // only for Edmonds-Karp } class Edge { int target; int capacity; int flow; // residual capacity can be derived from flow and capacity Edge back; // needed to update flow in reverse direction }

Max-flow implementation Reading Graph void main() { int N = sc.nextInt(); V = new Node[N]; // in C++ initialize visited and clear adj int s = sc.nextInt(); s--; int t = sc.nextInt(); t--; // read per-vertex input if applicable… int M = sc.nextInt(); for (int i = 0; i < M; i++) { // reading edges int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); // capacity a--; b--; // if input is 1-based instead of 0-based Edge e1 = new Edge(b, c); // make new edge Edge e2 = new Edge(a, 0); // capacity 0 if directed, c if undirected! e1.back = e2; e2.back = e1; // set back edges V[a].adj.add(e1); V[b].adj.add(e2); // also if directed }

Max-flow implementation int maxFlow(int s, int t) { int totalFlow = 0; for (int i = 0; i < V.length; i++) { for (Edge e: V[i].adj) e.flow = 0; // reset flow } while (true) { for (int i = 0; i < V.length; i++) { V[i].visited = false; V[i].parent = null; // reset node info } flow = augment(s, t); if (flow == 0) break; totalFlow += flow; int x = t; while (x != s) { // update flow on augmenting path V[x].parent.flow -= flow; V[x].parent.back.flow += flow; x = V[x].parent.target; } return totalFlow; }

Max-flow implementation Ford Fulkerson int augment(int i, int t) { if (V[i].visited) return 0; // no flow here V[i].visited = true; if (i == t) return Integer.MAX_VALUE; // infinite flow from node to itself for (Edge e: V[i].adj) { if (e.capacity – e.flow <= 0) continue; // residual capacity = 0 int f = augment(e.target, t); // Recurse if (f > 0) { f = Math.min(f, e.capacity – e.flow); // take min with residual cap. V[e.target].parent = e.back; // set parent return f; } return 0; // no flow to t found }

Max-flow implementation Edmonds-Karp int augment(int s, int t) { ArrayList queue = new ArrayList (); // can use ArrayDeque V[s].visited = true; V[s].flow = Integer.MAX_VALUE; V[t].flow = 0; queue.add(s); for (int i = 0; i < queue.size(); i++) { int k = queue.get(i); if (k == t) break; // could break at discovery instead for (Edge e: V[k].adj) { if (e.capacity – e.flow <= 0 || V[e.target].visited) continue; V[e.target].flow = Math.min(V[k].flow, e.capacity – e.flow); V[e.target].visited = true; V[e.target].parent = e.back; queue.add(e.target); } return V[t].flow; }

Bipartite matching implementation Bipartite matching Can use max-flow for implementation … … but a simplified version is much easier! All edges have capacity one No real need to add super-source/super-sink class Node { ArrayList adj; // edges are not weighted, so just integers boolean visited; // only for vertices in A int match; // only for vertices in B (matching vertex in A, initially -1) } Node[] A, B; // Two arrays of nodes, one for each side … // Reading edges is very standard (only bipartite graph edges) int a = sc.nextInt(); int b = sc.nextInt(); a--; b--; A[a].adj.add(b); // only edges from A to B …

Bipartite matching implementation boolean augment(int i) { if (A[i].visited) return false; A[i].visited = true; for (Integer j: A[i].adj) { if (B[j].match == -1 || augment(B[j].match)) { // check for augmenting path B[j].match = i; // directly updates matching return true; } return false; } int matching() { for (int i = 0; i < B.length; i++) B[i].match = -1; // reset matching int M = 0; // matching size for (int i = 0; i < A.length; i++) { // incremental approach for (int j = 0; j < A.length; j++) A[j].visited = false; // reset DFS if (augment(i)) M++; // if augmenting path, then increase matching } return M; }

Application When do I use max-flow or bipartite matching? Problems are rarely in standard form Must model problem as max-flow or bipartite matching Standard problems Maximum flow Minimum cut in a graph Bipartite matching Independent set in bipartite graph Vertex cover in bipartite graph … but often the problem is not standard!

Max-flow min-cut Minimum Cut Input: A graph G = (V, E) and a source s and sink t Output: The size of the minimum cut (S, V-S) with s ∈ S and t ∈ V-S The size of a cut is the total weight of the edges crossing the cut “The paths to close off to disconnect s and t” s t Maximum flow = minimum cut

Maximum independent set Maximum Independent set Input: A graph G = (V, E) Output: Max size set S ⊆ V such that no edge between nodes in S A B Complexity General graphs: NP-hard Bipartite graphs: |S| = |A| + |B| - M, where M is maximum matching

Minimum vertex cover Minimum Vertex Cover Input: A graph G = (V, E) Output: Min size set S ⊆ V such that each edge adjacent to node in S A B Complexity General graphs: NP-hard Bipartite graphs: |S| = M, where M is maximum matching

Recognize flow problems How to recognize other maximum flow problems? Very hard Try to see the problem as a graph problem Model the problem in different ways Relevant keywords match assign(ment) bounded degree capacity constraint If you think any of these keywords during modeling, consider max-flow!

Wrong Answer Problem Given a set of potential answers in a crossword puzzle, where horizontal answers are disjoint (same for vertical), determine the size of the largest subset of answers that can be correct simultaneously

Wrong Answer Solution Build a graph of conflicting answers ➨ conflict graph Horizontal answers cannot be conflicting (same for vertical) Conflict graph is bipartite! solution winner leiden bapc Answer: Size of maximum independent set of conflict graph

Minimum Path Cover in DAG Problem Given a directed acyclic graph (DAG), determine the minimum number of directed paths required to cover all the nodes of the graph

Minimum Path Cover in DAG Solution Every used edge reduces #paths by one ➨ maximize #edges used Every node can have at most one ingoing and one outgoing edge Bounded degree… Answer Make bipartite graph with each node on both sides ➨ bipartite double cover

Minimum Path Cover in DAG Solution Every used edge reduces #paths by one ➨ maximize #edges used Every node can have at most one ingoing and one outgoing edge Bounded degree… Answer Make bipartite graph with each node on both sides ➨ bipartite double cover Compute maximum matching ➨ #paths = n - M

Competition Problem Given the current set of a competition, where for each match the winning team gets 2 points, and both teams get 1 point in case of a tie, determine which teams can still win the competition Team namePlayedPoints Team Rocket23 Team Aqua12 Team Magma22 Team Galactic32 Team Plasma21 Upcoming games Team Rocket – Team Plasma Team Aqua – Team Magma Team Magma – Team Galactic Team Plasma – Team Aqua Team Rocket – Team Aqua

Competition Solution Determine answer for a single team ➨ e.g. Team Galactic Clearly Team Galactic should win all its matches ➨ 4 points How to distribute the points for remaining matches? Team namePlayedPoints Team Rocket23 Team Aqua12 Team Magma22 Team Galactic32 Team Plasma21 Team Rocket – Team Plasma Team Aqua – Team Magma Team Magma – Team Galactic Team Plasma – Team Aqua Team Rocket – Team Aqua Teams Matches Answer: Check if max-flow = 2 x #matches

Taxi schedule Problem Given a list of taxi reservations with times and locations (and how fast one can travel between two locations), determine how many taxis are needed to handle all reservations

Taxi schedule Problem Given a list of taxi reservations with times and locations (and how fast one can travel between two locations), determine how many taxis are needed to handle all reservations Solution Can determine if one reservation can be handled after another This forms a directed acyclic graph Answer is minimum path cover of this DAG

Skipped topics Multiple sources/sinks Easy: add super-source and/or super-sink Stable Marriage problem Find weighted matching without mutual better options Push-relabel algorithm Another max-flow algorithm with O(V 2 E) or O(V 3 ) running time Circulation problem Flow variant with source/sink constraints and edge lower bounds Maximum weighted bipartite matching Bipartite matching on weighted bipartite graph Minimum cost flow Flow variant with costs on edges

Exercise Problems Practice BAPC 2010 J – Wrong Answer BAPC 2012 C – Chess Competition EAPC 2011 C – Attack of the giant n-pus BAPC 2009 H – No smoking, please BAPC 2014 A – Avoiding the Apocalypse NWERC 2011 F – Pool Construction