CPSC 490: Problem Solving in Computer Science A bipartite graph is: - - PowerPoint PPT Presentation

cpsc 490 problem solving in computer science
SMART_READER_LITE
LIVE PREVIEW

CPSC 490: Problem Solving in Computer Science A bipartite graph is: - - PowerPoint PPT Presentation

Lecture 7: Bipartite graphs: Matching, Knigs 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 .


slide-1
SLIDE 1

CPSC 490: Problem Solving in Computer Science

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

slide-2
SLIDE 2

Bipartite matching

A bipartite graph is:

  • A graph where can partition the nodes into V = X ∪ Y, where all edges go between X

and Y.

  • A graph with no odd-length cycle. (These are equivalent.)

A matching is a subset of edges S ⊂ E such that no edges in S share a common node. Goal: find a matching with maximum size.

1

slide-3
SLIDE 3

Bipartite matching

2

slide-4
SLIDE 4

Bipartite matching

2

slide-5
SLIDE 5

Maximum bipartite matching

We can solve this with max flow! Dinic’s runs in O( √ |V||E|) on this type of graph!

3

slide-6
SLIDE 6

Problem 1 – Job hunting

Input: A list of m jobs and n applicants.

  • Each applicant applies to a subset of jobs.
  • Each job accepts at most 1 applicant.
  • Each applicant works at most 1 job.

Output: the maximum number of applicants who can get a job.

4

slide-7
SLIDE 7

Problem 1 – Solution

Match jobs to applicants!

  • The nodes are applicants and jobs.
  • If person i applied for job j, make an edge i → j.

5

slide-8
SLIDE 8

Problem 2 – Match what to what?

Input: A n × n matrix M with entries either 0 or 1. We say M is “rearrangeable” if we can swap rows and columns to make all diagonal entries 1. Output: whether M is rearrangeable.

6

slide-9
SLIDE 9

Problem 2 – Solution

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.
  • Add edge i → j if Mij = 1.
  • M is rearrangeable if there is a matching of size n.
  • Recovering the swaps: if row i matches column ci, swap column i and ci.

7

slide-10
SLIDE 10

Problem 3 – Elementary math

Input: n pairs of numbers (ai, bi). We wish to construct an arithmetic quiz using the operations +, −, ×. The numbers are already decided (the ai, bi). We want to make every answer distinct. Output: a possible assignment of operations, or determine it is impossible.

Source: Northwestern Europe Regional Contest 2015

8

slide-11
SLIDE 11

Problem 3 – Solution

Match equations to their answers.

  • The leħt side represents each question.
  • For the pair (ai, bi), connect it to the nodes ai + bi, ai − bi, ai × bi on the right side.

It’s possible if every question is matched. To get an assignment, look at the matching.

9

slide-12
SLIDE 12

Problem 4 – Pseudo-matching

Input: the standings of a sports league, and the list of remaining games to be played.

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

Output: whether it’s possible for Team 490 to win.

10

slide-13
SLIDE 13

Problem 4 – Pseudo-matching

Team Number of wins 490 2 A 3 B 2 C Games to be played 490 - A A - B B - C C - A Can team 490 be a winner in this scenario? Yes!

  • Team 490 wins its remaining 1 game to end up with 3 wins.
  • B wins its match with A, and C wins its two matches.

11

slide-14
SLIDE 14

Problem 4 – Solution

12

slide-15
SLIDE 15

Problem 4 – Solution

12

slide-16
SLIDE 16

Problem 4 – Solution

  • Let Team 490 win all of its games, to end up with w wins.
  • If there’s a team which already has more than w wins, it’s not possible.
  • Otherwise, use flow!

13

slide-17
SLIDE 17

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.
  • If a game gij is played between teams i and j, add edges gij → i, gij → j.
  • Connect each team to the sink with capacity = w − wi (number of games team i is

allowed to win). It’s possible if there’s flow to every game.

14

slide-18
SLIDE 18

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.

15

slide-19
SLIDE 19

Vertex cover

In general graphs, finding a minimum vertex cover is NP-hard. In bipartite graphs, we have hope, thanks to Kőnig!

16

slide-20
SLIDE 20

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

17

slide-21
SLIDE 21

Vertex covers in bipartite graphs

The minimum vertex cover has size 2. The maximum matching also has size 2!

18

slide-22
SLIDE 22

Kőnig’s theorem

  • Theorem. (Kőnig 1931) In a bipartite graph, the size of a maximum matching is equal to

the size of a minimum vertex cover.

  • Proof. This is max-flow min-cut in disguise!

Maximum flow ⇔ maximum matching Minimum cut ⇔ minimum vertex cover!

19

slide-23
SLIDE 23

Cut ⇔ Vertex cover

Let X be a set of cut edges. All edges are s → u, v → t (otherwise capacity is infinite). Claim: all the nodes which are endpoints of cut edges form a vertex cover.

  • Suppose not. Then there is edge a → b which is not covered.
  • Then the flow path s → a → b → t wasn’t cut—contradiction.

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

slide-24
SLIDE 24

Finding the cover

Let G = (V, E) be a bipartite graph, with partitions V = L ∪ R.

  • Run flow to find the maximum matching.
  • Let S = reachable nodes in the residual graph.
  • The cover is C = (L \ S) ∪ (R ∩ S).

21

slide-25
SLIDE 25

Finding the cover

22

slide-26
SLIDE 26

Finding the cover

22

slide-27
SLIDE 27

Finding the cover

22

slide-28
SLIDE 28

Finding the cover

22

slide-29
SLIDE 29

Finding the cover

22

slide-30
SLIDE 30

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 covering any white squares. Tiles may overlap.

23

slide-31
SLIDE 31

Problem 5 – Solution

Observation: want to expand every tile as much as possible. Thus, every black cell is 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 a black cell. Answer = min vertex cover.

24

slide-32
SLIDE 32

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 them. Finding a maximum independent set is NP-hard in general, but doable for bipartite graphs!

25

slide-33
SLIDE 33

Maximum independent set ⇔ minimum vertex cover

The complement of any vertex cover is an independent set, and vice versa. Proof.

  • (⇒) 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.

26

slide-34
SLIDE 34

Problem 6 – Easter eggs

Input: m red and blue points in the plane. 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. Output: the maximum distance d so that you can place N eggs in total, with no red and blue eggs being closer than d.

Source: Benelux Algorithm Programming Contest 2017

27

slide-35
SLIDE 35

Problem 6 – Solution

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

⇒ use Kőnig’s theorem!

28

slide-36
SLIDE 36

Problem 7 – Maximum islands

Input: a grid with land, water, and cloud tiles. Cloud tiles may be either land or water. Output: the maximum number of islands consistent with the picture.

Source: ACM-ICPC Pacific Northwest Regionals 2016

29

slide-37
SLIDE 37

Problem 7 – Solution

  • 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

without any touching ⇒ maximum independent set!

  • The cloud cells form a grid ⇒ bipartite!
  • Apply Kőnig’s theorem.

30