Bipartite Matching. Unweighted Bipartite Matching.

43
Bipartite Matching

Transcript of Bipartite Matching. Unweighted Bipartite Matching.

Page 1: Bipartite Matching. Unweighted Bipartite Matching.

Bipartite Matching

Page 2: Bipartite Matching. Unweighted Bipartite Matching.

Unweighted Bipartite Matching

Page 3: Bipartite Matching. Unweighted Bipartite Matching.

Definitions

Matching

Free Vertex

Page 4: Bipartite Matching. Unweighted Bipartite Matching.

Definitions

Maximum Matching: matching with the largest number of edges

Page 5: Bipartite Matching. Unweighted Bipartite Matching.

Definition

Note that maximum matching is not unique.

Page 6: Bipartite Matching. Unweighted Bipartite Matching.

Intuition

Let the top set of vertices be men Let the bottom set of vertices be women Suppose each edge represents a pair of

man and woman who like each other

Maximum matching tries to maximize the number of couples!

Page 7: Bipartite Matching. Unweighted Bipartite Matching.

Applications

Matching has many applications.

This lecture lets you know how to find maximum matching.

Page 8: Bipartite Matching. Unweighted Bipartite Matching.

Alternating Path Alternating between matching and non-matching edges.

a b c d e

f g h i j

d-h-e: alternating patha-f-b-h-d-i: alternating path starts and ends with free verticesf-b-h-e: not alternating pathe-j: alternating path starts and ends with free vertices

Page 9: Bipartite Matching. Unweighted Bipartite Matching.

Idea

“Flip” augmenting path to get better matching

Note: After flipping, the number of matched edges will increase by 1!

Page 10: Bipartite Matching. Unweighted Bipartite Matching.

Idea

Theorem (Berge 1975):A matching M in G is maximum iffThere is no augmenting path

Proof: () If there is an augmenting path, clearly not

maximum. (Flip matching and non-matching edges in that path to get a “better” matching!)

Page 11: Bipartite Matching. Unweighted Bipartite Matching.

Proof for the other direction

() Suppose M is not maximum. Let M’ be a maximum matching such that |M’|>|M|.

Consider H = MM’ = (MM’)-(MM’)i.e. a set of edges in M or M’ but not both

H has two properties: Within H, number of edges belong to M’ > number of edges

belong to M. H can be decomposed into a set of paths. All paths should

be alternating between edges in M and M’. There should exist a path with more edges from M’.

Also, it is alternating.

Page 12: Bipartite Matching. Unweighted Bipartite Matching.

Idea of Algorithm

Start with an arbitrary matching While we still can find an augmenting path

Find the augmenting path PFlip the edges in P

Page 13: Bipartite Matching. Unweighted Bipartite Matching.

Breadth-First Search Algorithm for Augmented Path

• Use Breadth-First Search:LEVEL(0) = some unmatched vertex r

for odd L > 0,LEVEL(L) = {u|{v,u} E – M

when v LEVEL(L -1) and when u in no lower level}

For even L > 0,LEVEL(L) = {u|{v,u} M

when v LEVEL(L -1) and u in no lower level}

• Assume G is bipartite graph with matching M.

Page 14: Bipartite Matching. Unweighted Bipartite Matching.

Proof of Breadth-First Search Algorithm for Augmented Path

• Cases(1) If for some odd L >0,

LEVEL(L) contains an unmatched vertex u

then the Breadth First Search tree T has an augmenting path from r to u(2) Otherwise no augmenting path exists, so

M is maximal.

Page 15: Bipartite Matching. Unweighted Bipartite Matching.

Labelling Algorithm

Start with arbitrary matching

Page 16: Bipartite Matching. Unweighted Bipartite Matching.

Labelling Algorithm

Pick a free vertex in the bottom

Page 17: Bipartite Matching. Unweighted Bipartite Matching.

Labelling Algorithm

Run BFS

Page 18: Bipartite Matching. Unweighted Bipartite Matching.

Labelling Algorithm

Alternate unmatched/matched edges

Page 19: Bipartite Matching. Unweighted Bipartite Matching.

Labelling Algorithm

Until a augmenting path is found

Page 20: Bipartite Matching. Unweighted Bipartite Matching.

Augmenting Tree

Page 21: Bipartite Matching. Unweighted Bipartite Matching.

Flip!

Page 22: Bipartite Matching. Unweighted Bipartite Matching.

Repeat

Pick another free vertex in the bottom

Page 23: Bipartite Matching. Unweighted Bipartite Matching.

Repeat

Run BFS

Page 24: Bipartite Matching. Unweighted Bipartite Matching.

Repeat

Flip

Page 25: Bipartite Matching. Unweighted Bipartite Matching.

Answer

Since we cannot find any augmenting path, stop!

Page 26: Bipartite Matching. Unweighted Bipartite Matching.

Overall algorithm

Start with an arbitrary matching (e.g., empty matching) Repeat forever

For all free vertices in the bottom, do bfs to find augmenting paths

If found, then flip the edges If fail to find, stop and report the maximum matching.

Page 27: Bipartite Matching. Unweighted Bipartite Matching.

Time analysis

We can find at most |V| augmenting paths (why?)

To find an augmenting path, we use bfs! Time required = O( |V| + |E| )

Total time: O(|V|2 + |V| |E|)

Page 28: Bipartite Matching. Unweighted Bipartite Matching.

Improvement

We can try to find augmenting paths in parallel for all free nodes in every iteration.

Using such approach, the time complexity is improved to O(|V|0.5 |E|)

Page 29: Bipartite Matching. Unweighted Bipartite Matching.

Weighted Bipartite Graph

46 63

Page 30: Bipartite Matching. Unweighted Bipartite Matching.

Weighted Matching

46 6

3

Score: 6+3+1=10

Page 31: Bipartite Matching. Unweighted Bipartite Matching.

Maximum Weighted Matching

46 6

3

Score: 6+1+1+1+4=13

Page 32: Bipartite Matching. Unweighted Bipartite Matching.

Augmenting Path (change of definition) Any alternating path such that total score of unmatched

edges > that of matched edges The score of the augmenting path is

Score of unmatched edges – that of matched edges

46 6

3

Note: augmenting path need not start and end at free vertices!

Page 33: Bipartite Matching. Unweighted Bipartite Matching.

Idea for finding maximum weight matching Theorem: Let M be a matching of

maximum weight among matchings of size |M|.

If P is an augmenting path for M of maximum weight,

Then, the matching formed by augmenting M by P is a matching of maximum weight among matchings of size |M|+1.

Page 34: Bipartite Matching. Unweighted Bipartite Matching.

Overall Algorithm

Start with an empty matching Repeat forever

Find an augmenting path P with maximum score If the score > 0, then flip the edges Otherwise, stop and report the maximum weight

matching.

Page 35: Bipartite Matching. Unweighted Bipartite Matching.

Time analysis

The same! Time required = O(|V|2 + |V| |E|)

Page 36: Bipartite Matching. Unweighted Bipartite Matching.

Stable Marriage Problem

Page 37: Bipartite Matching. Unweighted Bipartite Matching.

Stable Marriage Problem

Given N men and N women, each person list in order of preference all the people of the opposite sex who would like to marry.

Problem:Engage all the women to all the men in such a

way as to respect all their preferences as much as possible.

Page 38: Bipartite Matching. Unweighted Bipartite Matching.

Stable?

A set of marriages is unstable if two people who are not married both prefer each other than their

spouses

E.g. Suppose we have A1 B3 C2 D4 E5. This is unstable since A prefer 2 more than 1 2 prefer A more than C

A B C D E

2

5

1

3

4

1

2

3

4

5

2

3

5

4

1

1

3

2

4

5

5

3

2

1

4

1

E

A

D

B

C

2

D

E

B

A

C

3

A

D

B

C

E

4

C

B

D

A

E

5

D

B

C

E

A

Page 39: Bipartite Matching. Unweighted Bipartite Matching.

Naïve solution

Starting from a feasible solution. Check if it is stable.

If yes, done! If not, remove an unstable couple.

Is this work?

Page 40: Bipartite Matching. Unweighted Bipartite Matching.

Naïve solution (2)

Does not work! E.g.

A1 B3 C2 D4 E5 A2 B3 C1 D4 E5 A3 B2 C1 D4 E5 A3 B1 C2 D4 E5

A B C D E

2

5

1

3

4

1

2

3

4

5

2

3

5

4

1

1

3

2

4

5

5

3

2

1

4

1

E

A

D

B

C

2

D

E

B

A

C

3

A

D

B

C

E

4

C

B

D

A

E

5

D

B

C

E

A

Page 41: Bipartite Matching. Unweighted Bipartite Matching.

Solution

1. Let X be the first man.2. X proposes to the best woman in the

remaining on his list. (Initially, the first woman on his list!)

3. If α is not engaged Pair up (X, α). Then, set X=next man and goto 1.

4. If α prefers X more than her fiancee Y, Pair up (X, α). Then, set X=Y and goto 1.

5. Goto 1

Page 42: Bipartite Matching. Unweighted Bipartite Matching.

Example

A B C D E

2

5

1

3

4

1

2

3

4

5

2

3

5

4

1

1

3

2

4

5

5

3

2

1

4

1

E

A

D

B

C

2

D

E

B

A

C

3

A

D

B

C

E

4

C

B

D

A

E

5

D

B

C

E

A

A B C D E

2

5

1

1

2

3

4

2

3

5

1

3

5

3

2

Page 43: Bipartite Matching. Unweighted Bipartite Matching.

Time analysis

If there are N men and N women,O(N2) time