Download presentation
Presentation is loading. Please wait.
Published byAnna West Modified over 9 years ago
1
Honors Track: Competitive Programming & Problem Solving Fun with Graphs II Kevin Verbeek
2
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
3
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
4
Motivation Bipartite graphs: Model relations between two classes of objects Examples Boys and girls: “boy x likes girl y” A B
5
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
6
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
7
Bipartite matching Problem If every employee can complete at most one job, how many jobs can be completed? A B
8
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
9
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
10
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?
11
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 = 5 1 2 3 4 c 01 c 02 c 12 c 13 c 14 c 24 c 35 c 45 c 43
12
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 = 5 1 2 3 4 0/3 0/2 0/1 0/2 0/3 0/2 0/1
13
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 = 5 1 2 3 4 3/3 2/3 0/2 2/2 1/1 2/2 3/3 2/2 1/1 How can we increase the flow?
14
Increase flow on edge Idea: Increase flows on edges until we found the maximum flow s = 0 t = 5 1 2 3 4 0/3 0/2 0/1 0/2 0/3 0/2 0/1
15
Increase flow on edge Idea: Increase flows on edges until we found the maximum flow s = 0 t = 5 1 2 3 4 0/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?
16
Increase flow on paths Idea: Find path from s to t and increase flow on the entire path s = 0 t = 5 1 2 3 4 0/3 0/2 0/1 0/2 0/3 0/2 0/1
17
Increase flow on paths Idea: Find path from s to t and increase flow on the entire path s = 0 t = 5 1 2 3 4 2/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?
18
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 = 5 1 2 3 4 2/3 0/3 2/2 0/2 0/1 2/2 0/3 2/2 0/1
19
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 = 5 1 2 3 4 2/3 0/3 2/2 0/2 0/1 2/2 0/3 2/2 0/1 0/2
20
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 = 5 1 2 3 4 2/3 0/3 2/2 0/2 0/1 2/2 0/3 2/2 0/1
21
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 = 5 1 2 3 4 2/3 0/3 2/2 0/2 0/1 2/2 0/3 2/2 0/1
22
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 = 5 1 2 3 4 2/3 0/2 2/2 0/1 2/2 2/3 2/2 0/1
23
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 = 5 1 2 3 4 2/3 0/2 2/2 0/1 2/2 2/3 2/2 0/1
24
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 = 5 1 2 3 4 3/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
25
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
26
Max-flow to bipartite matching Can we solve bipartite matching with maximum flow? A B
27
Max-flow to bipartite matching Can we solve bipartite matching with maximum flow? A B s t 1 1 1 1 1 1 1 1 1 1 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
28
Max-flow to bipartite matching Can we solve bipartite matching with maximum flow? A B s t 1 1 1 1 1 1 1 1 1 1 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
29
Max-flow to bipartite matching Can we solve bipartite matching with maximum flow? A B s t 1 1 1 1 1 1 1 1 1 1 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
30
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 }
31
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 }
32
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; }
33
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 }
34
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; }
35
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 …
36
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; }
37
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!
38
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 3 3 2 2 1 2 3 2 1 Maximum flow = minimum cut
39
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
40
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
41
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!
42
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
43
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
44
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
45
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… 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 Answer Make bipartite graph with each node on both sides ➨ bipartite double cover
46
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… 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 Answer Make bipartite graph with each node on both sides ➨ bipartite double cover Compute maximum matching ➨ #paths = n - M
47
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
48
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 2 2 2 2 2 2 1 2 2 3 Answer: Check if max-flow = 2 x #matches
49
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
50
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
51
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
52
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.