honors track: competitive programming & problem solving fun with graphs ii kevin verbeek

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

Upload: anna-west

Post on 17-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

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

Honors Track:Competitive Programming& Problem Solving

Fun with Graphs II

Kevin Verbeek

Page 2: 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

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

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

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

Motivation

Bipartite graphs: Model relations between two classes of objects

Examples Boys and girls: “boy x likes girl y”

A B

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

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

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

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

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

Bipartite matching

Problem

If every employee can complete at most one job, how many jobs can be completed?

A B

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

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

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

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

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

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?

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

Max-flow problem

Maximum flow problem Input: A graph with edge capacities cij and a source s and sink t

Output: The maximum flow fij satisfying the flow constraints

Flow constraints 0 ≤ fij ≤ cij “flow is within capacity”

Σk fki = Σk fik for all i ≠ s, t “flow in = flow out”

s = 0 t = 5

1

2

3

4

c01

c02

c12

c13

c14

c24

c35

c45

c43

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

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

0/2

0/2

0/1

0/2

0/3

0/2

0/1

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

Flow

Flow constraints 0 ≤ fij ≤ cij “flow is within capacity”

Σk fki = Σk fik for all i ≠ s, t “flow in = flow out”

Conventions fij = -fji

magnitude of flow = Σk fsk

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?

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

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

0/2

0/2

0/1

0/2

0/3

0/2

0/1

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

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

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

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

0/2

0/2

0/1

0/2

0/3

0/2

0/1

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

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?

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

Residual graph

Residual graph Graph with updated capacities reflecting current flow c’ij = cij – fij

Since fij = -fji 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

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

Residual graph

Residual graph Graph with updated capacities reflecting current flow c’ij = cij – fij

Since fij = -fji 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/10/1

0/2

0/2

0/20/2

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

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

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

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

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

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

2/3

0/2

2/2

0/1

2/2

2/3

2/2

0/1

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

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

2/3

0/2

2/2

0/1

2/2

2/3

2/2

0/1

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

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

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

Max-flow algorithm

Algorithm

MaxFlow(G, s, t)

1. Initialize fij = 0 for all i, j

2. while augmenting path exists do

3. Increase flow along augmenting path

4. return fij

How to compute augmenting path? DFS ➨ Ford-Fulkerson algorithm with O(E f*) running time BFS ➨ Edmonds-Karp algorithm with O(V E2) running time

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

Max-flow to bipartite matchingCan we solve bipartite matching with maximum flow?

A B

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

Max-flow to bipartite matchingCan we solve bipartite matching with maximum flow?

A B

s t

11

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

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

Max-flow to bipartite matchingCan we solve bipartite matching with maximum flow?

A B

s t

11

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

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

Max-flow to bipartite matchingCan we solve bipartite matching with maximum flow?

A B

s t

11

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

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

Max-flow implementation

Implementation Must keep track of edge data Must have full augmenting path information

class Node { ArrayList<Edge> 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}

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

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

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

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;}

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

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 }

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

Max-flow implementation

Edmonds-Karp

int augment(int s, int t) { ArrayList<Integer> queue = new ArrayList<Integer>(); // 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;}

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

Bipartite matching implementationBipartite 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<Integer> 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 …

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

Bipartite matching implementationboolean 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;}

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

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!

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

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

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

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

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

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

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

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 degreecapacity

constraint

If you think any of these keywords during modeling, consider max-flow!

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

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

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

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

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

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

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

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

12345678

12345678

Answer Make bipartite graph with each node on both sides

➨ bipartite double cover

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

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

12345678

12345678

Answer Make bipartite graph with each node on both sides

➨ bipartite double cover Compute maximum matching ➨ #paths = n - M

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

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 name Played Points

Team Rocket 2 3

Team Aqua 1 2

Team Magma 2 2

Team Galactic 3 2

Team Plasma 2 1

Upcoming games

Team Rocket – Team Plasma

Team Aqua – Team Magma

Team Magma – Team Galactic

Team Plasma – Team Aqua

Team Rocket – Team Aqua

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

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 name Played Points

Team Rocket 2 3

Team Aqua 1 2

Team Magma 2 2

Team Galactic 3 2

Team Plasma 2 1

Team Rocket – Team Plasma

Team Aqua – Team Magma

Team Magma – Team Galactic

Team Plasma – Team Aqua

Team Rocket – Team Aqua

TeamsMatches

2

2

2

2

2

2

1

2

2

3

Answer: Check if max-flow = 2 x #matches

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

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

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

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

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

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(V2 E) or O(V3) 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

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

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