CS70 - Lecture 7 - Feb 2, 2011 - 10 Evans

Today we will look at an algorithm used to

    model traditional ways people pick their mates

        source of the name "Stable Marriage Problem"

    how some dating services really work

    how to match hospitals and interns 

    how to match jobs to processors

    how to match organ donor to recipients

    how to match list of somethings to list of something_elses...

Common set-up for all these problems:

  (1) You have a lists of n men (say) and n women

  (2) Each man and each woman has a ranked list

        of the n possible partners in order from "best" to "worst"

The goal is to create n pairs of men and women that is

the "best possible pairing" in some sense.

So we need a clear, numerical way to measure "best".

Consider the following rankings, for Men 1,2 and 3

and Women A, B and C:

Men  Ranking of Women               Women  Ranking of Men

1        A  B  C                                     A             2 1 3

2        B  A  C                                     B             1 2 3

3        A  B  C                                     C             1 2 3

One criterion for a pairing to satisfy, and our main one, is "stability":

Suppose a man M and woman W are partnered with different

people, but prefer one another: we call such a situation

unstable because M and W are motivated to leave their

assigned partners and pair up with one another.

For example, given the above rankings, here are two sets of pairings:

(1)   { (1,C), (2,B), (3,A) }

(2)   { (2,A), (1,B), (3,C) }

Pairing (1) is unstable because man 1 and woman B are not paired

with one another, but 1 prefers B to his assigned partner C,

and B prefers 1 to her assigned partner 2. 

We use the term "rogue couple" to refer this pair (1,B).

In contrast, pairing (2) is stable, i.e. there are no rogue couples: 

For every person not paired with their top-ranked partner,

that top-ranked partner is paired with someone they prefer more.

For example 1 and B are paired, but 1 prefers A. However

A is paired with 2 whom she prefers over 1, so (1,A) is not rogue. 

So our first goal is to determine whether a stable pairing exists,

and to find an algorithm to find it. Once we do that, if there is more

than one answer, we will ask which of the alternative stable

pairings is "better" (and for whom it is better! Hint: life isn't fair).

Here is a simple, reasonable sounding, and incorrect algorithm to find a stable pairing:

   (1) start with any pairing

   (2) if there is a rogue couple, pair them up, as well as 

         their abandoned mates

   (3) repeat until there are no more rogue couples

To see that this idea is not enough, consider the simpler situation of

roommate selection, where we have 2n people (all of the same gender, say)

to pair up as roommates, so any person can pair up with any other person.

Here is a ranking:

Person   Ranking of others

A              B  C  D

B              C  A  D

C             A  B   D

D             could be anything

In this case, any pairing has a rogue couple, because there is a "circle of preferences":

A prefers B who prefers C who prefers A. So in the pairings

{ (A,B), (C,D) }    the couple (B,C) is rogue

{ (A,C), (B,D) }    the couple (A,B) is rogue

ASK&WAIT: Any other possibilities?

                      { (A,D), (B,C) }    the couple (A,C) is rogue

So we need a different idea, and one that depends one there being n men and n women.

TMA = "Traditional Marriage Algorithm"

Warning: Men and Women are treated differently!


  (1)  Each Man asks the first Woman on his list, who has not already rejected him, to marry him

  (2)  Each Woman takes the list of Men who have just proposed to her, says "Maybe" to her

         top-ranked Man, and rejects the rest

  (3)  Each rejected Man crosses the Woman who rejected him off his list

until each Man has been told "Maybe" by some Woman (whom he then marries)

Let us run this algorithm for the  list above

First Round:

    1 proposes to A,  2 proposes to B,  3 proposes to A

    A says Maybe to 1, rejects 3; B says Maybe to 2; C does nothing

    3 crosses A off his list

Second Round

    1 proposes to A (again), 2 proposes to B (again), 3 proposes to B

    A says Maybe to 1 (again), B says Maybe to 2 (again), rejects 3; C does nothing

    3 crosses B off his list

Third Round

    1 proposes to A (again),  2 proposes to B (again), 3 proposes to C

    A says Maybe to 1 (again), B says Maybe to 2 (again), C says Maybe to 3

No one rejected, time to plan the party…

So the resulting pairing is  { (1,A), (2,B), (3,C) }, and you may confirm that

there are no rogue couples, so it is a stable pairing.

There are two questions about this algorithm:

(1) Does it always terminate?

(2) Does it always return a stable pairing?

Theorem 1: TMA always terminates, after at most n^2 steps.

Proof: The trick is to find a measure of progress, 

that we can show always increases at every step of the algorithm,

until it is large enough that we know we have to be done.  For TMA, we will count

the total number of rejections. After each pass through the loop,

there is at least one rejection, or else the algorithm terminates, because each

Man has been told "Maybe" by one Woman. So after each pass through the

loop, the number of rejections increases by at least 1, or else the

algorithm terminates. How many rejections can there be? Since each Man

can be rejected by a Woman just once (whereupon he crosses her off his

list and never asks her again) and the n Men each have n Woman on their 

lists to begin with, there can be at most n^2 rejections before every Woman 

has been crossed off all the lists, and every Man has been told "Maybe".  

(The actual number of rejections may be much smaller when the algorithm

terminates, even 0, but it certainly can't be bigger than n^2.)

End of Proof. 

Theorem 2: TMA always returns a stable pairing.

To prove this, we need the following crucial

Improvement Lemma: Suppose Woman W says Maybe to Man M in Round j.

Then in later rounds, whichever Man M' to whom she says Maybe will rank

at least as high in her list as M. (It is possible that M' = M or not.)

Intuitive Proof: Once Man M has been told Maybe by Woman W, he will

keep coming back to propose to W every day, until she rejects him for

someone she ranks higher. So the ranking of the person W says Maybe to

can only increase.

Proof by induction on k = number of round, for k >= j:

   P(k) = "Man chosen by W in round k ranked at least as high as Man chosen by W in round j"

   Base case: P(j) = "Man chosen by W in round j ranked at least as high as Man in round j" - true!

   Induction step:

        P(k) -> man chosen by W in round k ranked at least as high as Man chosen by W in round j

                -> since the man M who W chose in round k proposes to W again in round k+1, whoever

                     W choses in round k+1 will rank at least as high as M

                ->  P(k+1). 

This completes the proof of the Improvement Lemma.

Proof of Theorem 2: We need to prove there is no rogue couple in the pairing when the

algorithm terminates. We use proof by contradiction: Suppose (M,W) is in the final

pairing, but M prefers W* to W, and (M,W*) is a rogue couple. That means that M proposed 

to W* before he proposed to W, but W* rejected him. By the Improvement Lemma, whoever W* 

ended up marrying, call him M*, ranked at least as high as M. So it is not the case that

W* prefer M to her mate M*, so (M,W*) is not a rogue couple, contradiction. 

To summarize so far, we have an algorithm for the stable marriage problem that

(1) is guaranteed to stop after n^2 steps (how much does each step cost?)

(2) is guaranteed to return a stable pairing, i.e. without rogue couples who prefer

one another to their assigned mates.

Next we ask: Is the answer unique, i.e. is there only one stable pairing?

And if not, is there another success metric (numerical measure of "happiness")

that is maximized by the algorithm?

Ex: Consider the following sets of preferences:

Men    Ranking of Women                    Women   Ranking of Men

1          A B C D                                         A              1 3 2 4

2          A D C B                                         B              4 3 2 1

3          A C B D                                         C              2 3 1 4

4          A B C D                                         D              3 4 2 1

You can confirm that there are exactly two stable pairings:

S =   { (1,A), (2,D), (3,C), (4,B) }  and

T =   { (1,A), (2,C),  (3,D), (4,B) }

How do we compare these two pairings? It depends on your point of view:

From 1's point of view, S and T are equally good, because 1 gets his top choice, A

From 2's point of view, S is better than T, because 2 prefers D to C

From 3's point of view, S is better than T, because 3 prefers C to D

From 4's point of view, S and T are also equal, because 4 gets his second choice B

So from all the Men's points of view S is at least as good as (same or better than) T.

How about the women's points of view?

From A's point of view S and T are equally good because A gets her top choice, 1

From B's point of view, S and T are also equally good because B gets her top choice, 4

From C's point of view, T is better than S, because C prefers 2 to 3.

From D's point of view, T is also better than S, because D prefers 3 to 2.

So from all the Women's point of view T is at least as good as (same or better than) S.

In other words, there are two possible stable outcomes:  One makes the Men as happy

as possible, and the Women as unhappy as possible. The other one makes the Women

happy and the Men unhappy. Which one do you think the "Traditional Marriage

Algorithm" will pick? This is not just accidentally true for this example,

it is always true (is this a surprise?)

Def: We call a stable pairing "male optimal" if it simultaneously maximizes the rank of 

each Man's selected mate. In other words, consider all possible stable pairings,

and for each Man compute the highest rank of any woman W paired with him in

any pairing. A pairing is "male optimal" if every man is simultaneously paired with

a Woman that has this highest rank.

Def: We call a stable pairing "female pessimal" if it simultaneously minimizes the rank

each Woman's selected mate. In other words, consider all possible stable pairings as above,

and for each Woman compute the lowest rank of any man M paired with her in

any pairing. A pairing is "female pessimal" if every woman is simultaneously paired with

a Man that has this lowest rank.

Theorem 3. Of all possible stable pairings, TMA chooses one that is simultaneously

male optimal and female pessimal. In other words the men are as happy as possible,

and the women as unhappy as possible.