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 and residents , each with a ranked preference list over the other side. We want to find a matching (e.g. ) 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 where prefers over its current resident and prefers over its current hospital .
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.
Hospitals
Residents
Runtime Analysis
We observe that no hospital makes an offer twice to any resident . This follows from the description of the algorithm. There are at most proposals total. Each iteration of the while loop makes exactly one proposal, the algorithm terminates in iterations.
We can achieve runtime with some preprocessing work. For each resident , we create a lookup table rank[r][h] that gives the rank of in 's preference list. This takes time and space to build, and we can compare whether prefers or by checking rank[r][h] < rank[r][h'] in 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 time. We take time for setup, and time for possible proposals. Thus, our overall time complexity is .
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 is unmatched at termination, what can you say about every resident on 's preference list?
Proof
We first observe that if a resident is matched at some step of Gale-Shapley, it remains matched (assuming hospitals are proposing). This follows from the fact that if resident is matched to some hospital at some point, it can only leave for a higher ranked .
Suppose by contradiction that some is unmatched at the end of G-S. This means that some resident is also unmatched. Since 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, must have exhausted its entire preference list. In particular, must have offered to at some point during the algorithm.
- Case 1: was unmatched, and makes an offer to . Then, becomes a pair in our matching. By our prior reasoning, once is matched, it remains matched until the end of G-S. This is a contradiction, since we assumed was unmatched at the end.
- Case 2: is matched to some , and makes an offer to . We observe that cannot be unmatched (because it must stay with or be matched with ), so this is a contradiction again.
Therefore, all are matched, so we have a perfect matching.
Claim 2: The output is stable.
Hint
Recall what it means for a pair 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 is not stable. Then, there exists some pair such that with in 's preference list and in 's preference list.
We first observe that must have made an offer to , since is a pair and in 's preference list. When made an offer to , two things could have happened:
- is unmatched. Then, are matched. If is in the final matching, this implies that in 's preference list (this is the only way that unmatches from and matches with ). This is a contradiction, because we assumed in 's preference list.
- is matched (when offers to ). Suppose that is matched to some . If , leaves for and becomes a pair. This return the situation described above, which we know leads to a contradiction. Thus, we assume rejects and stays with . This means that in 's preference list by transitivity. But is matched to (or some other hospital that prefers even more) and would never downgrade to , contradicting that .
Therefore, we have no unstable pairs, and Gale-Shapley outputs a stable matching.
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!