Neeldhara
  • About
  • Research
    • Overview
    • People
    • Publications
    • Surveys
  • Teaching
    • Courses
    • Materials
  • Lists
    • Puzzles
    • Bookmarks
  • Exposition
    • Talks
    • Videos
  • Events
  • Blog

Stable Matchings

  • Data Structures and Algorithms
    • Data Structures and Structured Data
    • Representing Sequences
    • Representing Graphs
    • On Cards and Graphs
    • Walking Around via Euler Tours
    • Navigating Graphs
    • Stable Matchings
    • Balanced BSTs

On this page

  • The Problem
  • Developing a Solution
  • The Algorithm and its Properties
  • References

Stable Matchings

dsanotes
lecturenotes
Published

November 26, 2022

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\}V={m1​,…,mn​} of nnn men and W={w1,…,wn}W = \{w_1, \ldots, w_n\}W={w1​,…,wn​} of nnn 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 VVV and WWW.

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 MMM that we seek is that there is no man and woman who are unmatched in MMM who prefer each other over their matched partners. To this end, we first define the notion of a blocking pair with respect to MMM:

Blocking Pair

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

  • aaa and bbb are not matched to each other in MMM,
  • aaa prefers bbb over M(a)M(a)M(a),
  • bbb prefers aaa over M(b)M(b)M(b)

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

Unstable Matching

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

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)Ω(n2) blocking pairs. What’s the maximum number possible?

A matching without blocking pairs is called stable.

Stable Matching

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

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

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

Verifying that a matching is stable.

Given a matching MMM between nnn men and nnn women and their preferences over each other, it is possible to verify if MMM is stable in O(n2)O(n^2)O(n2) 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 recieved.

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 recieves 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 WW⋆​⊆W) recieved 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_\starW0​=W∖W⋆​) recieved no proposals and are still single;
  • some men (say V⋆⊆VV_\star \subseteq VV⋆​⊆V) had their proposals accepted and they are no longer single ;
  • some men (say V0=V∖V⋆V_0 = V \setminus V_\starV0​=V∖V⋆​) were turned down and are still single.

If W0=∅W_0 = \emptysetW0​=∅, that’s… well, that’s awesome, because what that means is all men had distinct choices for their top preference, and so V0=∅V_0 = \emptysetV0​=∅ 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 \emptysetW0​=∅, 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 mmm decides to not approach www because www is matched to some m⋆m^\starm⋆ in the first round. Then mmm will eventually be matched to someone who’s worse than www, and if www happened to prefer mmm over m⋆m^\starm⋆, then (m,w)(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 mmm propose to their next best option irrespective of whether they are single or not. From the woman’s perspective, they are going to recieve 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)O(n2) proposals have been made)
  • outputs a perfect matching MMM 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⩽n2 . Also note that no man mmm 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 engagments at any stage of the algorithm always forms a valid matching), but there are only n−1n-1n−1 men other than www: so this is not feasible.

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

It remains to show that MMM is stable. But if MMM has a blocking pair (m,w)(m,w)(m,w), then www was proposed to by mmm before mmm proposed to M(w)M(w)M(w), and since (m,w)(m,w)(m,w) are not matched in MMM, it must be the case that www prefers M(w)M(w)M(w) over mmm, so (m,w)(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 MMM between nnn men and nnn women and their preferences over each other, it is possible to find a stable matching MMM in O(n3)O(n^3)O(n3) 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 mmm, their optimal match as the best woman that mmm can be matched to in any stable matching. It turns out that MMM matches all men to their optimal matches!

The Men Are Lucky

Let MMM be the output of the GS algorithm. For all men mmm, there is no stable matching M⋆M^\starM⋆ where mmm prefers M⋆(m)M^\star(m)M⋆(m) over M(m)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 MMM be the output of the GS algorithm. There is no matching M⋆M^\starM⋆ for which it is true that all men mmm prefer M⋆(m)M^\star(m)M⋆(m) over M(m)M(m)M(m).

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

References

Numberphile video about the algorithm

Numberphile video about the proof

Notes on implementation details


© 2022 • Neeldhara Misra • Credits •

 

Corrections? Please leave a comment here or a PR in this repository, thanks!

I’d rather be a failure at something I love than a success at something I hate.

George Burns

You live and you learn — at any rate, you live.

Douglas Adams

A problem worthy of attack proves its worth by fighting back.

Paul Erdos

×