Skip to main content

This site is currently under development.

3. Stable Matchings

The Problem

The problem of pairing up people and/or resources shows up a lot:

  • Matching students who graduate high school to seats in various colleges.

  • Matching students who graduate college to employers for jobs, internships, residencies, and so on.

  • Getting actors and actresses to commit to work together for films amidst various constraints.

  • Matching organ donors to patients accounting for constraints of compatibility and timing.

  • Matching men and women in a dating/marriage market.

We are going to look at one particular abstraction that captures many of the scenarios above (among others). Suppose we have a set V={m1,…,mn}V = \{m_1, \ldots, m_n\} of nn men and W={w1,…,wn}W = \{w_1, \ldots, w_n\} of nn women, where each man (respectively, woman) has a strict and complete ranking over the women (respectively, men). Our goal is to find a matching between the men and the women, which is to say, a bijection between VV and WW.

Now, a natural question at this point is: what kind of matchings do we want to find? Unconstrained, there are plenty of matchings that we can choose from. Which one is the "best"?

Upon a moment's reflection you might come up with several ideas. We are going to focus on a fundamental game-theoretic approach to identifying what we desire from the matchings we seek. What we will demand of the matching MM that we seek is that there is no man and woman who are unmatched in MM who prefer each other over their matched partners. To this end, we first define the notion of a blocking pair with respect to MM:

Blocking Pair

Given a matching MM between nn men and nn women, if a∈Va \in V and b∈Wb \in W, then (a,b)(a,b) forms a blocking pair if ALL of the following hold:

  • aa and bb are not matched to each other in MM,

  • aa prefers bb over M(a)M(a),

  • bb prefers aa over M(b)M(b)

The presence of a blocking pair (a,b)(a,b) implies that the matching MM is unlikely to "sustain": aa and bb both have an incentive to break off the alliances suggested by the matching MM and "elope" with each other instead. Thus, matchings that have blocking pairs are called unstable.

Unstable Matching

A matching MM is said to be unstable if there is a blocking pair with respect to MM.

An Example of a Matching with no blocking pairs.

Consider the preferences given below (where the first column in each row is the ID of the men and the women, and the remaining columns indicate the rank):

Men 1 2 3 4
1 2 4 1 3
2 3 1 4 2
3 2 3 1 4
4 4 1 3 2
Women 1 2 3 4
1 2 1 4 3
2 4 3 1 2
3 1 4 3 2
4 2 1 4 3

and the matching given by: M = (1,4), (2,3), (3,2), (4,1). Notice that this matching has, in fact, no blocking pairs.

How many blocking pairs can we have?

Come up with a stable matching instance that has Ω(n2)\Omega(n^2) blocking pairs. What's the maximum number possible?

A matching without blocking pairs is called stable.

Stable Matching

A matching MM is said to be stable if there are no blocking pairs with respect to MM.

It's easy to verify if a given matching MM is stable: for all men mm we look up all women ww that mm ranks higher than M(m)M(m), and check if ww also ranks mm higher than M(w)M(w): if yes, then we can declare MM unstable since (m,w)(m,w) is a blocking pair. If we find no blocking pairs after having checked all men mm, then we can declare that MM is stable.

This takes at most O(n2)O(n^2) time, assuming that ranks can be retrieved in constant time. Thus we have the following.

Verifying that a matching is stable.

Given a matching MM between nn men and nn women and their preferences over each other, it is possible to verify if MM is stable in O(n2)O(n^2) time.

The next natural questions are:

  • Do stable matchings always exist?

    • If yes: can we always find them efficiently?

    • If not: can we efficiently find matchings that minimize the number of blocking pairs?

This is a good place to pause and ponder!

Developing a Solution

It turns out that (somewhat surprisingly!) stable matchings indeed always exist!

The algorithm for finding a stable matching works as follows. To begin with, we say that all men and women are single, i.e, unmatched to anyone so far.

Anticipating our need for stability, we take a greedy approach: the men attempt to match up to their best option by proposing to them. But notice that this may not be immediately workable: possibly multiple men have the same choice for their top option. This puts the ball in the woman's court, so to speak, and again in the interest of being eventually stable, it's intuitive that the woman will choose to align with the best offer she has among the proposals she's received.

Also, since we finally want everyone to be matched, we will also ensure that proposals are not rejected simply because they seem unattractive in absolute terms: if a woman who's single receives one or more proposals, she will accept the best among them, no matter how good or bad they are.

After one round of proposals, the situation is as follows:

  • some women (say W⋆⊆WW_\star \subseteq W) received one or more proposals and picked the best offer, and are no longer single;

  • some women (say W0=W∖W⋆W_0 = W \setminus W_\star) received no proposals and are still single;

  • some men (say V⋆⊆VV_\star \subseteq V) had their proposals accepted and they are no longer single;

  • some men (say V0=V∖V⋆V_0 = V \setminus V_\star) were turned down and are still single.

If W0=∅W_0 = \emptyset, that's awesome, because what that means is all men had distinct choices for their top preference, and so V0=∅V_0 = \emptyset as well and we already have a matching where everyone has their best possible match: so this is clearly very stable and we are done.

On the other hand, it's possible that W0≠∅W_0 \neq \emptyset, which is to say that some women are still single, and therefore there are some single men as well. Now, to make progress, single men go back to the drawing board and propose again. Now, a natural question at this point is the following:

Should the currently single men try their luck again with women who've rejected them?

Well, since they were rejected for good reason (said women had better offers), and the reason has not gone away, it is clearly a waste of time for men to go back to women who've rejected them. So what they do instead is to propose to the next best option. Now: what if their next best option is not a single woman? Well, suppose mm decides to not approach ww because ww is matched to some m⋆m^\star in the first round. Then mm will eventually be matched to someone who's worse than ww, and if ww happened to prefer mm over m⋆m^\star, then (m,w)(m,w) will eventually be a blocking pair.

However, recall that this is exactly the situation we want to avoid. So we're going to have mm propose to their next best option irrespective of whether they are single or not. From the woman's perspective, they are going to receive proposals again, and we'll let them pick the best offer, even if it means breaking off their current engagement.

After this second round of proposals we again have some single men and women, and some engagements. Note that the following invariant is true:

Any woman who was not single at the end of the first round remains engaged at the end of the second round as well.

Men, on the other hand, may become single again. At this point, we simply continue as before: at the end of every round, the single men continue to propose to their current best option, and the women continue to accept the best offer that they have. We continue this until there are no single men left.

The Algorithm and its Properties

Here's pseudocode (borrowed from Wikipedia) summarizing the algorithm, which is due to Gale and Shapley. The version below is morally equivalent to our description above, except that all proposals happen one by one, and it turns out that this way of looking at it simplifies the analysis.

algorithm stable_matching is
    Initialize m ∈ M and w ∈ W to free
    while ∃ free man m who has a woman w to propose to do
        w := first woman on m's list to whom m has not yet proposed
        if ∃ some pair (m', w) then
            if w prefers m to m' then
                m' becomes free
                (m, w) become engaged
            end if
        else
            (m, w) become engaged
        end if
    repeat

It turns out that this procedure:

  • terminates in finite time (in fact, after O(n2)O(n^2) proposals have been made)

  • outputs a perfect matching MM between the men and the women that has no blocking pairs.

The first property follows from the fact that men never propose twice to the same woman, so the number of proposals made is ⩽n2\leqslant n^2. Also note that no man mm is rejected by all women: this can only happen if all the women have found better engagements (which are necessarily distinct --- notice that the set of engagements at any stage of the algorithm always forms a valid matching), but there are only n−1n-1 men other than ww: so this is not feasible.

So we know that the while loop terminates, and that once it does we have a matching MM.

It remains to show that MM is stable. But if MM has a blocking pair (m,w)(m,w), then ww was proposed to by mm before mm proposed to M(w)M(w), and since (m,w)(m,w) are not matched in MM, it must be the case that ww prefers M(w)M(w) over mm, so (m,w)(m,w) cannot be a blocking pair after all.

This brings us to the following claim:

Finding a stable matching

Given a matching MM between nn men and nn women and their preferences over each other, it is possible to find a stable matching MM in O(n3)O(n^3) time.

The output of the Gale-Shapley algorithm has a couple of interesting properties. First off, it turns out that the output is not just stable, but also qualitatively very good. Let us define, for a man mm, their optimal match as the best woman that mm can be matched to in any stable matching. It turns out that MM matches all men to their optimal matches!

The Men Are Lucky

Let MM be the output of the GS algorithm. For all men mm, there is no stable matching M⋆M^\star where mm prefers M⋆(m)M^\star(m) over M(m)M(m).

Also, the output of GS is weakly Pareto optimal, which is to say that there is no matching (stable or otherwise), where all the men are better off.

The GS matching is weakly PO

Let MM be the output of the GS algorithm. There is no matching M⋆M^\star for which it is true that all men mm prefer M⋆(m)M^\star(m) over M(m)M(m).

We state these claims above without proof. The interested reader should look them up!

References