Lecture 7: Bipartite graphs: Matching, Kőnig’s theorem Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-01-24 University of British Columbia CPSC 490: Problem Solving in Computer Science
A bipartite graph is: and Y . • A graph with no odd-length cycle. (These are equivalent.) Goal: find a matching with maximum size. 1 Bipartite matching • A graph where can partition the nodes into V = X ∪ Y , where all edges go between X A matching is a subset of edges S ⊂ E such that no edges in S share a common node.
2 Bipartite matching
2 Bipartite matching
We can solve this with max flow! 3 Maximum bipartite matching √ Dinic’s runs in O ( | V || E | ) on this type of graph!
• Each applicant applies to a subset of jobs. • Each job accepts at most 1 applicant. • Each applicant works at most 1 job. 4 Problem 1 – Job hunting Input : A list of m jobs and n applicants. Output : the maximum number of applicants who can get a job.
• The nodes are applicants and jobs. Match jobs to applicants! 5 Problem 1 – Solution • If person i applied for job j , make an edge i → j .
We say M is “rearrangeable” if we can swap rows and columns to make all diagonal entries 1. 6 Problem 2 – Match what to what? Input : A n × n matrix M with entries either 0 or 1. Output : whether M is rearrangeable.
Observation: want to pick n 1’s such that each row has exactly a single 1, each column has exactly a single 1. Match rows to columns! • Leħt nodes are rows, right nodes are columns. • M is rearrangeable if there is a matching of size n . • Recovering the swaps: if row i matches column c i , swap column i and c i . 7 Problem 2 – Solution • Add edge i → j if M ij = 1.
Source: Northwestern Europe Regional Contest 2015 We want to make every answer distinct. 8 Problem 3 – Elementary math Input : n pairs of numbers ( a i , b i ) . We wish to construct an arithmetic quiz using the operations + , − , × . The numbers are already decided (the a i , b i ). Output : a possible assignment of operations, or determine it is impossible.
Match equations to their answers. • The leħt side represents each question. It’s possible if every question is matched. To get an assignment, look at the matching. 9 Problem 3 – Solution • For the pair ( a i , b i ) , connect it to the nodes a i + b i , a i − b i , a i × b i on the right side.
• We’re fans of Team 490. • There are n teams in the league, and some matches have already been played. • The team(s) with the highest number of wins at the end win. 10 Problem 4 – Pseudo-matching Input : the standings of a sports league, and the list of remaining games to be played. Output : whether it’s possible for Team 490 to win.
11 Games to be played • B wins its match with A , and C wins its two matches. • Team 490 wins its remaining 1 game to end up with 3 wins. Can team 490 be a winner in this scenario? Yes! C - A B - C A - B 490 - A 0 Team C 2 B 3 A 2 490 Number of wins Problem 4 – Pseudo-matching
12 Problem 4 – Solution
12 Problem 4 – Solution
• If there’s a team which already has more than w wins, it’s not possible. • Let Team 490 win all of its games, to end up with w wins. • Otherwise, use flow! 13 Problem 4 – Solution
Idea: “match” games to their winners, but cap the number of games each team can win. • Construct a bipartite graph. • Leħt side: the games which don’t involve 490 • Right side: the other teams • Connect the source to each game with capacity 1. allowed to win). It’s possible if there’s flow to every game. 14 Problem 4 – Solution • If a game g ij is played between teams i and j , add edges g ij → i , g ij → j . • Connect each team to the sink with capacity = w − w i (number of games team i is
15 Vertex cover Let G = ( V , E ) be a graph. A vertex cover is a subset of nodes C ⊂ V such that every edge e ∈ E has an endpoint in C . We’re interested in finding the minimum vertex cover.
In bipartite graphs, we have hope, thanks to Kőnig! In general graphs, finding a minimum vertex cover is NP-hard. 16 Vertex cover
17 Vertex covers in bipartite graphs Let C ⊂ V be a vertex cover. Consider any matching M ⊂ E in the graph. • Every edge in the matching has ≥ 1 endpoint in C . • Thus | C | ≥ | M | .
The maximum matching also has size 2! The minimum vertex cover has size 2. 18 Vertex covers in bipartite graphs
the size of a minimum vertex cover. Proof. This is max-flow min-cut in disguise! 19 Kőnig’s theorem Theorem. (Kőnig 1931) In a bipartite graph, the size of a maximum matching is equal to Maximum flow ⇔ maximum matching Minimum cut ⇔ minimum vertex cover!
Claim: all the nodes which are endpoints of cut edges form a vertex cover. Conversely, suppose Y is a vertex cover. If we cut the edges described above, then this forms a cut. This means that a minimum cut gives a minimum vertex cover! 20 Cut ⇔ Vertex cover Let X be a set of cut edges. All edges are s → u , v → t (otherwise capacity is infinite). • Suppose not. Then there is edge a → b which is not covered. • Then the flow path s → a → b → t wasn’t cut—contradiction.
• Run flow to find the maximum matching. 21 Finding the cover Let G = ( V , E ) be a bipartite graph, with partitions V = L ∪ R . • Let S = reachable nodes in the residual graph. • The cover is C = ( L \ S ) ∪ ( R ∩ S ) .
22 Finding the cover
22 Finding the cover
22 Finding the cover
22 Finding the cover
22 Finding the cover
covering any white squares. Tiles may overlap. 23 Problem 5 – Tiling Input : a grid of black and white squares. You have an unlimited number of 1 × k and k × 1 tiles for every k = 1 , 2 , 3 , . . . . Output : the minimum number of tiles needed to cover all the black squares while not
Observation: want to expand every tile as much as possible. Thus, every black cell is a black cell. 24 Problem 5 – Solution covered by ≤ 2 tiles – one horizontal and one vertical. Form a bipartite graph, connecting u → v if horizontal tile u and vertical tile v intersect in Answer = min vertex cover.
them. Finding a maximum independent set is NP-hard in general, but doable for bipartite graphs! 25 Maximum independent set Let G = ( V , E ) be a graph. A set of vertices S ⊂ V is an independent set if there are no edges between any pair of
The complement of any vertex cover is an independent set, and vice versa. Proof. 26 Maximum independent set ⇔ minimum vertex cover • ( ⇒ ) If u and v are both not in a vertex cover, then there is no edge between them. • ( ⇐ ) If ( u , v ) is an edge, at most one of u and v is in the independent set.
You are organizing an Easter egg hunt. You will place red eggs on red points and blue eggs on blue points. You need to place exactly N eggs, and want to maximize the distance between any red and blue eggs. blue eggs being closer than d . Source: Benelux Algorithm Programming Contest 2017 27 Problem 6 – Easter eggs Input : m red and blue points in the plane. Output : the maximum distance d so that you can place N eggs in total, with no red and
Binary search on d . For a given d , construct the following graph: • The leħt side has red points and the right side has blue points. • Connect a red point to a blue point if they’re closer than d away. • We need to find an independent set of size at least N in this graph. Graph is bipartite 28 Problem 6 – Solution ⇒ use Kőnig’s theorem!
Cloud tiles may be either land or water. Source: ACM-ICPC Pacific Northwest Regionals 2016 29 Problem 7 – Maximum islands Input : a grid with land, water, and cloud tiles. Output : the maximum number of islands consistent with the picture.
• For every cloud cell adjacent to land, turn it into a water cell. • For the remaining cloud cells, we want to make as many land cells as possible • Apply Kőnig’s theorem. 30 Problem 7 – Solution without any touching ⇒ maximum independent set! • The cloud cells form a grid ⇒ bipartite!
Recommend
More recommend