Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf ·...

15
Maximal matchings: from optimal dating to set theory CS21b: Structure and Interpretation of Computer Programs Autumn 2010

Transcript of Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf ·...

Page 1: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

Maximal matchings:from optimal dating to set theory

CS21b: Structure and Interpretation of Computer ProgramsAutumn 2010

Page 2: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

Maximal matchings:from optimal dating to set theory

CS21b: Structure and Interpretation of Computer ProgramsAutumn 2010

Page 3: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

The problem

Bob Carol

Ted Alice

A group of men and women have mutually agreed possible, with potential conflicts, i.e. (m,w1), (m,w2).

You are a matchmaker. You need to optimize the number of couples.

Page 4: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

The problem

Bob Carol

Ted Alice

2 matches: good

A group of men and women have mutually agreed possible, with potential conflicts, i.e. (m,w1), (m,w2).

You are a matchmaker. You need to optimize the number of couples.

Page 5: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

The problemA group of men and women have mutually agreed possible, with potential conflicts, i.e. (m,w1), (m,w2).

You are a matchmaker. You need to optimize the number of couples.

Bob Carol

Ted Alice

1 match: not so good

What’s a good algorithm?

Page 6: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

The problem instance: a bipartite graphM1 W1

M2 W2

M3 W3

M4 W4

M5 W5

...

Mk Wl

Page 7: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

Algorithm idea

Begin with an edge (a matching of 1 couple).

Grow the matching by adding and removing couples appropriately.

How?

By finding augmenting paths.

Page 8: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

Augmenting pathsM1 W1

M2 W2

M3 W3

M4 W4

M5 W5

...

Mk Wl

3 couples included in current matching

4 possible couples not included in current matching

Idea: exchange couples on the path, not in the matching, for couples on the path, currently in the matching. We gain one couple.

Page 9: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

Augmenting pathsM1 W1

M2 W2

M3 W3

M4 W4

M5 W5

...

Mk Wl

4 couples included in current matching

3 possible couples not included in current matching

Idea: exchange couples on the path, not in the matching, for couples on the path, currently in the matching. We gain one couple.

Page 10: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

Augmenting pathsM1 W1

M2 W2

M3 W3

M4 W4

M5 W5

...

Mk Wl

4 couples included in current matching

3 possible couples not included in current matching

Good idea.

But how do you find an

augmenting path?

Is there always one?

Idea: exchange couples on the path, not in the matching, for couples on the path, currently in the matching. We gain one couple.

Page 11: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

There’s always an augmenting pathif the matching isn’t maximal

Claim:Let L (larger) and S (smaller) be two matchings where L has more couples than S. Then S can be extended by an augmenting path.

Observation:Ignore couples both in L and in S---for the moment, they stay in.Look only at remaining couples in L and S (still more in L than in S!), ignoring the rest: you see disjoint paths of alternating L and S edges

. . . . . .L LS S

What kinds of paths are there?

Corollary:If we have a matching that is not maximal, we can always find an augmenting path.

Page 12: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

. . . . . .L LS S

What kinds of disjoint paths are there?

(Remember: L=large, S=small; disjoint means no one is on two paths.)

1. Circular: Bob loves Carol, who loves Ted, who loves Alice, who loves Bob. (Romantic comedy.)

2. Paths with equal number of L- and S-edges: LSLSLS...LS

3. Paths with more S-edges than L-edges: SLSLSL...SLS

4. Augmenting paths with more L-edges than S-edges: LSLSLS...LSL

Note: if there were only paths of types 1-3, L would not be larger than S. So there has to be an augmenting path (type 4)!

Page 13: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

. . . . . .L LS S

The algorithm, from 50,000 feet (no Scheme here...)

1. Start with an empty set of edge from the possible ones in the problem instance.

2. Use depth-first search on the graph to search for an augmenting path.

3. If you find an augmenting path, exchange the edges, rinse and repeat step 2.

4. If you don’t find an augmenting path, it means the match you have is of optimal size. Stop.

Page 14: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

. . . . . .f fg g

What’s this got to do with set theory?The Schroeder-Bernstein Theorem

A new problem:Suppose you have sets of men (M) and women (W), each possibly infinite.Everyone loves someone unique.

Each man chooses a woman: no two men choose the same woman.(An injective map f: M --> W.)

Each woman chooses a man: no two women choose the same man.(An injective map g: W --> M.)

Your job: match EVERYBODY up.(A bijective map h: M --> W.)

Page 15: Maximal matchings: from optimal dating to set theorymairson/Courses/cs21b/Lectures/Maximal.pdf · One way infinite starting with a woman: gfgfgfg... 4. Two way infinite: ...fgfgfgfgf...

. . . . . .f fg g

The Schroeder-Bernstein Theorem

Each man chooses a woman: no two men choose the same woman.(An injective map f: M --> W.)

Each woman chooses a man: no two women choose the same man.(An injective map g: W --> M.)

What kinds of disjoint chains (paths) are there?

1. Finite cycles.2. One way infinite starting with a man: fgfgfg...3. One way infinite starting with a woman: gfgfgfg...4. Two way infinite: ...fgfgfgfgf...

In cases 1,2,4, take h=f, and in case 3, take h=g-1