Skip to main content

Stable Matching

Introduction

Stable matching is a framework for pairing two groups when each participant has ranked preferences over the other side. Classic examples include matching men to women or hospital to residents. We will use hospital and resident example in our notes.

We have hospitals h1,,hnh_1, \dots, h_n and residents r1,,rnr_1, \dots, r_n, each with a ranked preference list over the other side. We want to find a matching MM (e.g. M={(h1,r16),(h14,r7),}M = \{(h_1, r_{16}), (h_{14}, r_7), \dots \}) satisfying two properties:

  • Perfect: All hospitals and residents appear exactly once in our final matching. Intuitively, no one is left unmatched and no one is assigned more than one partner.
  • Stable: There should be no unmatched pair who would both prefer each other over their assigned partners. Formally, there should be no blocking pair: a hospital-resident pair (h,r)M(h, r) \notin M where hh prefers rr over its current resident rr' and rr prefers hh over its current hospital hh'.

Gale-Shapley Algorithm

Initialize M = {}
while there is an unmatched hospital h:
let r = first resident on h's list not yet offered to
if r is unmatched:
add (h, r) to M
else if r prefers h to current match h':
remove (h', r) from M
add (h, r) to M
end while
output M

Visualization Demo

Gale-Shapley Framework

Start with everyone unmatched

Always choose the lowest-ID unmatched hospital, and have it propose down its preference list until it is matched.

Step 1 / 10

Hospitals

H1free
Ranking
R1>R2>R3>R4
H2free
Ranking
R1>R2>R3>R4
H3free
Ranking
R1>R2>R3>R4
H4free
Ranking
R2>R3>R4>R1

Residents

R1free
Ranking
H3>H2>H1>H4
R2free
Ranking
H4>H2>H1>H3
R3free
Ranking
H2>H1>H4>H3
R4free
Ranking
H1>H4>H3>H2

Runtime Analysis

We observe that no hospital hh makes an offer twice to any resident rr. This follows from the description of the algorithm. There are at most n2n^2 proposals total. Each iteration of the while loop makes exactly one proposal, the algorithm terminates in O(n2)O(n^2) iterations.

We can achieve O(n2)O(n^2) runtime with some preprocessing work. For each resident rr, we create a lookup table rank[r][h] that gives the rank of hh in rr's preference list. This takes O(n2)O(n^2) time and space to build, and we can compare whether rr prefers hh or hh' by checking rank[r][h] < rank[r][h'] in O(1)O(1) time. In addition, for each hospital, we maintain a pointer corresponding to the next resident on the list it will propose.

This computation will allow us to process each while loop iteration in O(1)O(1) time. We take O(n2)O(n^2) time for setup, and O(1)O(1) time for O(n2)O(n^2) possible proposals. Thus, our overall time complexity is O(n2)O(n^2).

Correctness

We prove that Gale-Shapley produces a stable perfect matching. We show each of these properties separately; try them yourself before reading the solutions!

Claim 1: The output is a perfect matching.

Hint

Suppose we don't have a perfect matching. If hh is unmatched at termination, what can you say about every resident on hh's preference list?

Proof

We first observe that if a resident rr is matched at some step of Gale-Shapley, it remains matched (assuming hospitals are proposing). This follows from the fact that if resident rr is matched to some hospital hh at some point, it can only leave hh for a higher ranked hh'.

Suppose by contradiction that some hh is unmatched at the end of G-S. This means that some resident rr is also unmatched. Since hh is unmatched at termination, it was never permanently accepted by an resident, meaning that it was rejected by every resident it offered to. Since the algorithm only terminates when no unmatched hospital has a resident left to offer to, hh must have exhausted its entire preference list. In particular, hh must have offered to rr at some point during the algorithm.

  • Case 1: rr was unmatched, and hh makes an offer to rr. Then, (h,r)(h, r) becomes a pair in our matching. By our prior reasoning, once rr is matched, it remains matched until the end of G-S. This is a contradiction, since we assumed rr was unmatched at the end.
  • Case 2: rr is matched to some hh', and hh makes an offer to rr. We observe that rr cannot be unmatched (because it must stay with hh' or be matched with hh), so this is a contradiction again.

Therefore, all hh are matched, so we have a perfect matching. \square

Claim 2: The output is stable.

Hint

Recall what it means for a pair (h,r)M(h, r) \notin M to be a blocking pair. Argue by contradiction why no such pair can exist in the output of Gale-Shapley!

Proof

Suppose for contradiction's sake that MM is not stable. Then, there exists some pair (h,r)M(h, r) \notin M such that (h,r),(h,r)M(h, r'), (h', r) \in M with h>hh > h' in rr's preference list and r>rr > r' in hh's preference list.

We first observe that hh must have made an offer to rr, since (h,r)(h, r') is a pair and r>rr > r' in hh's preference list. When hh made an offer to rr, two things could have happened:

  • rr is unmatched. Then, (h,r)(h, r) are matched. If (h,r)(h', r) is in the final matching, this implies that h>hh' > h in rr's preference list (this is the only way that rr unmatches from hh and matches with hh'). This is a contradiction, because we assumed h>hh > h' in rr's preference list.
  • rr is matched (when hh offers to rr). Suppose that rr is matched to some h2h_2. If r:h>h2r: h > h_2, rr leaves h2h_2 for hh and (h,r)(h, r) becomes a pair. This return the situation described above, which we know leads to a contradiction. Thus, we assume rr rejects hh and stays with h2h_2. This means that h2>h>hh_2 > h > h' in rr's preference list by transitivity. But rr is matched to h2h_2 (or some other hospital that rr prefers even more) and would never downgrade to hh', contradicting that (h,r)M(h', r) \in M.

Therefore, we have no unstable pairs, and Gale-Shapley outputs a stable matching. \square

Extensions

In our visualization demo, we always chose the unmatched hospital with the lowest ID to be the next one to offer to a resident. But in reality, this doesn't matter; the output of Gale-Shapley is "unique", the output of Gale-Shapley is the same regardless of the order that the unmatched hospitals are processed.

Claim 3: The output is independent of hospital ordering.

Proof

Proof coming soon!

Since the output of G-S is independent of ordering, we can ask: which stable matching does it always produce? It turns out there is an answer to this!

Claim 4: Hospital-proposing G-S is hospital-optimal.

Proof

Proof coming soon!

Practice

Problem 1. (T/F) The output of Gale-Shapley is the same whether you have hospitals or residents offering. If true, provide a proof; if false, provide a counterexample.

Solution

Solution coming soon!