Massachusetts Institute of Technology Handout 28 6.042J/18.062J: Mathematics for Computer Science October 8, 1998 Professors Nancy Lynch and Srinivas Devadas

Supplement to Lecture 6

1 The Dating Game

Suppose that there are an equal number of boys and girls in a town. We want to find a good matching; that is, a good pairing of boys and girls into dating couples. We suppose that each boy has a ranking of all the girls according to how much he likes them, and each girl has a similar ranking of all the boys. If a boy and a girl that we do not match-up each ranks the other above his or her current date, then they are called a rogue couple. A matching with no rogue couples is called a stable matching. Our goal is to devise and analyze an algorithm that takes the boys’ and girls’ rankings as input and produces a stable matching as output.

The following notation will be used throughout the lecture. There are N girls and N boys. The set of girls is called G, and the set of boys is called B. A matching is a set of pairs (g, b), where g ∈ G and b ∈ B, such that every girl and boy appears in exactly one pair in the set.

We define rank_{b}(g) to be the rank that boy b assigns to girl g. For example, if rank_{b}(g) = 2, then girl g is boy b’s second favorite. Similarly, rank_{g}(b) indicates the rank that girl g assigns to boy b. (This notation may be confusing because a boy assigns the numerically highest rank to his least-favorite girl.) In addition, we define pref_{b}(k) to be boy b’s k-th favorite girl, and pref_{g}(k) to be girl g’s k-th favorite boy.

With this notation, a pair (g, b) is called a rogue couple in a matching M if (g, b′) ∈ M, (b, g′) ∈ M, and the following two conditions hold:

rank_{g}(b) < rank_{g}(b′)
rank_{b}(g) < rank_{b}(g′)

2 The Algorithm

We will use the following algorithm to generate a matching. The input is the set of all boys’ and girls’ rankings. In general terms, each girl pursues one boy at a time, and each boy rejects pursuers that he does not want to date.

More precisely, a girl always pursues the boy that she ranks best among those boys that have not yet rejected her. If a boy is currently pursued by more than one girl, then he may reject any of his pursuers other than his favorite among them. The algorithm terminates when no more rejections are possible. (We will show that this implies that the algorithm terminates when every boy has a single pursuer.) The output is the matching that pairs each boy with his final pursuer.

Note that this algorithm is underspecified. One boy may swiftly reject all pursuers other than his favorite, while another boy tries to amass as large a pool of pursuers as possible before issuing any rejections. One might suppose that by carefully timing his rejections, a boy might better his odds of ending up with the girl of his dreams. We will show that this is not the case; the output of the algorithm is independent of the timing of rejections.

3 A State Machine Model

We can model the dating game with a state machine as follows.

Let cur_{g} be the rank that girl g assigns to the boy that she is currently pursuing. As a special case, if the girl has been rejected by all the boys and is pursuing no one, then we let cur_{g} = N + 1. A state of the state machine corresponds to a particular assignment to all of the variables cur_{g}. For example, the initial state corresponds setting all of the cur_{g} variables equal to 1.

Before describing the transitions of the state machine, we introduce some derived variables.

• For each girl g, let cur-boy_{g} be the boy that girl g is currently pursuing, namely pref_{g}(cur_{g}). If the girl g has been rejected by all suitors, and thus cur_{g} = N + 1, then cur-boy_{g} is defined to be the null value Λ.

• For each boy b, we define best_{b} to be the rank that boy b assigns to his favorite current pursuer. If boy b currently has no pursuers, then best_{b} is defined to be N + 1.

• For each boy b, we define best-girl_{b} to be boy b’s favorite current pursuer. That is, best-girl_{b} = pref_{b}(best_{b}). If boy b is not pursued by any girl, then best-girl_{b} is defined to be Λ.

Each state machine transition corresponds to a boy rejecting one of his pursuers other than his favorite among them. Formally, from each state where cur-boy_{g} = b and best-girl_{b} ≠ g, there is a transition to the state the differs only in that cur_{g} is incremented by 1. Intuitively, this means that if girl g is pursuing boy b and boy b’s favorite pursuer is someone else, then he can reject her, forcing her to move down to the next guy on her preference list.

4 Basic Algorithm Properties

We will prove a sequence of invariants about the matching algorithm.

Invariant 1 If rank_{g}(b) < cur_{g} then best_{b} < rank_{b}(g).

Intuitively, this says that if girl g ranks boy b better than the boy that she is currently pursuing, then boy b currently has a girl g′ that he likes better than g.

Proof. The proof uses the Invariant Thorem. The invariant holds in the start state, because the hypothesis rank_{g}(b) < cur_{g} is always false; initially, every girl pursues her favorite boy, and so cur_{g} = 1. Now suppose that the invariant holds before a transition. The invariant can hold for one of two reasons: either the hypothesis is false or else the conclusion is true.

First, suppose that the invariant holds prior to a transition because the hypothesis is false; that is rank_{g}b ≥ cur_{g} prior to the transition. Note that rank_{g}b is the ranking that girl g assigns boy b and thus does not change. If the hypothesis remains false after the transition, then the invariant still holds after the transition. On the other hand, the hypothesis can only become true if rank_{g}b = cur_{g} prior to the transition (which implies that girl g is pursing boy b) and then cur_{g} is increased (which implies that boy b rejects girl g). But boy b can only reject girl g if he has another pursuer that he likes better. This implies that best_{b} < rank_{b}(g) both before and after the transition. Therefore, the invariant is true after the transition because the conclusion is true.

Second, suppose that the invariant holds prior to a transition because the conclusion is true; that is, best_{b} < rank_{b}(g). Since boy b never rejects his favorite pursuer, best_{b} can never increase. But rank_{b}(g) is just the rank that boy b assigns to girl g and can not change. Therefore, the invariant holds after the transition because the conclusion remains true.

Since the invariant holds in the start state and is preserved under all transitions, the invariant holds for all reachable states by the Invariant Theorem.

Invariant 2 For each girl g, cur_{g} ≤ N.

In other words, every girl is always pursing some boy; no girl is ever rejected by all the boys and left with nothing to do.

Proof.

1. Assume not; that is, in some reachable state, cur_{g} = N + 1.

2. FALSE

1.1 For all boys b, rank_{g}(b) < cur_{g} (because cur_{g} = N + 1).

1.2 For all boys b, best_{b} < rank_{b}(g) (by Invariant 1).

1.3 For all boys b, best-girl_{b} ∉ {g, Λ} (by previous step and definition of best-girl_{b}).

1.4 For all pairs of distinct boys b and b′, best-girl_{b} ≠ best-girl_{b′} (by definition of best-girl). 1.5 Q. E. D. (there are N boys, but only N − 1 girls).

3. Q. E. D. (by contradiction)

Theorem 1 The algorithm terminates.

Proof. The sum of the cur_{g} variable increases by 1 under each transtion. The sum of these variables is initially N, and the maximum sum is N^{2}. Therefore, the maximum number of transitions before the algorithm terminates is N^{2} − N.

(To do this with a termination function, we would want a decreasing derived variable. We could always define the termination function to be N^{2} − ∑_{g} cur_{g}. Or we could just realize that this is a variation on the notion of a termination function that also works.)

Now we show that when the algorithm terminates, everyone is paired. Recall that the algorithm terminates when no more rejections are possible; that is, every boy is pursued by at most one girl.

Invariant 3 If a (reachable) state is terminating, then:

1. For every girl g, cur-boy_{g} ≠ Λ.

2. For every boy b, best-girl_{b} ≠ Λ

Proof. The first assertion holds by Invariant 2. For the second assertion, note that in a terminating state every boy is pursued by at most one girl. Since every girl is still pursuing some boy by the first assertion, every boy must be paired with exactly one girl.

Finally, we show that the algorithm creates a stable matching.

Invariant 4 If a (reachable) state is terminating, then the pairing defined by cur-boy_{g} and best-girl_{b} is stable.

Proof.

1. Assume not; that is, there exist b, b′ ∈ B and g, g′ ∈ G such that best-girl_{b} = g, cur-boy_{g} = b, best-girl_{b′} = g′, cur-boy_{g′} = b′, and b and g form a rogue couple.

  2. FALSE

(a) rank_{b}(g′) < rank_{b}(g) = best_{b} and rank_{g′}(b) < rank_{g′}(b′) = cur_{g′} (by definition of rogue couple).

(b) best_{b} < rank_{b}(g′) (by Invariant 1 and second part of (a)).

(c) best_{b} > rank_{b}(g′) (by first part of (a)).

(d) Q. E. D. (by comparing 3 and 4)

3. Q. E. D. (by contradiction)

5 Fairness

This algorithm is totally biased in favor of the girls. For a given set of boys’ and girls’ preferences, there are many possible stable matchings. There is no stable matching in which any girl is paired with a boy that she likes better than the one she is assigned by the algorithm. On the other hand, there is no stable matching in which any boy is paired with a girl that he like less than the one is assigned by the algorithm. Thus, the algorithm is said to be “optimal” for the girls and “pessimal” for the boys.

Let Poss be the set of all pairs (b, g) such that there exists a stable matching in which boy b is paired with girl g. Then the claim that the algorithm is optimal for girls can be formulated as follows.

Claim 5.1 If a girl g prefers a boy b to her match assigned to her by the algorithm, than b isn’t even possible for her; that is (b, g) ∉ Poss.

This claim is a consequence of the following invariant, which states that girls only ever “cross-off” boys that they could never be paired with anyway.

Invariant 5 For every boy b and every girl g, if rank_{g}(b) < cur_{g}, then (b, g) ∉ Poss.

Proof. By the invariant theorem:

1. The invariant holds vacuously in the start state, because cur_{g} = 1 for all g.

2. The invariant is preserved under all transitions.

(a) Assume that the invariant is not preserved by some step.

     (b) FALSE

i. Fix a step that doesn’t preserve it, so the invariant holds before the step but not after. Let b and g be the boy and girl for which the invariant is false after the transition.

ii. This must be a step where boy b rejects girl g, making cur_{b} greater than rank_{g}(b) even though (b, g) ∈ Poss. Fix g′ such that b is rejecting g in favor of g′. Fix a stable matching M, (b, g) ∈ M. Fix b′, the mate of g′ in M.

iii. (b, g′) form a rogue couple in M. A. b prefers g′ to g (by ?). B. g′ prefers b to b′. C. Assume not, that g′ prefers b′ to b, that is, rank_{g′}(b) < rank_{g′}(b). D. rank_{g′}(b) = cur_{g′} (before or after the transition). Because g′ is currently

after b. E. rank_{g′}(b′) < cur_{g′}. By previous two steps, 2.2.3.2.1 and 2.2.3.2.2 F. (b′, g′) ∉ Poss. By the invariant applied to the pre-state. 5. (b′, g′) ∉ P By previous step and definition of Poss. 6. F, comparing 2.2.3.2.5 and 2.2.2 7. QED, contradiction

3. QED, definition of rogue pair 4. QED, rogue pair (2.2.3) vs. good pairing (2.2.2). 3. QED, Contradiction 3. QED, by Invariant Theorem