Stable Matching (Gale–Shapley)
Two sides each rank the other; we want a matching with no blocking pair — no two participants who both prefer each other to their assigned partners. The Gale–Shapley deferred-acceptance algorithm has proposers propose in preference order while receivers tentatively hold the best offer so far.
╌╌╌╌
Every problem so far in this module optimized a number — the most jobs, the fewest machines, the cheapest tree. Stable matching optimizes a relationship instead. Given two groups, each member of one ranking the members of the other, we want to pair them up so that the pairing holds: no two people, looking at their assigned partners, would both rather abandon them for each other. A pairing with such a defecting couple is unstable, and the couple is the proof of its instability. Gale and Shapley proved that a stable pairing always exists and that a simple greedy procedure — everyone proposes in order of preference, and each recipient keeps only the best proposal seen so far — always finds one. The procedure is greedy in spirit (each proposer tries its top remaining choice; each receiver locally improves), and like the rest of the module its correctness rests on an exchange-style argument. It is also deployed at national scale: the medical residency match and many school-assignment systems are this algorithm.
The stable marriage problem
Fix two disjoint sets of equal size , traditionally called the proposers
and the receivers (the older literature says men
and women
; we keep the
neutral roles). Each proposer ranks all receivers in a strict preference
list, and each receiver ranks all proposers. A matching is a
one-to-one pairing; it is perfect when everyone is matched.
The definition forbids exactly this instability: two people who each outrank the other's partner and would both defect. The algorithm must leave no such pair.
Here is matched to but prefers , and is matched to but prefers ; the dashed pair blocks. Stability is a weak requirement in one sense — it asks nothing about social welfare or total happiness — and a strong one in another: a single blocking pair condemns the whole matching. It is not obvious a stable matching always exists; for the closely related stable roommates problem (one set, everyone ranks everyone) it sometimes does not. The two-sided structure is what guarantees existence.
The Gale–Shapley algorithm
The algorithm is deferred acceptance. Proposers propose, but receivers never finally commit: a receiver holds onto its best proposal so far and stays free to trade up. Time runs in rounds; in each round some free proposer proposes to the most-preferred receiver it has not yet proposed to. That receiver, comparing the new offer to whatever it currently holds, keeps the better of the two and rejects the other. A rejected proposer becomes free again and will later propose further down its list. The deferral — receivers tentatively hold rather than accept — is the entire idea; it is what lets an early, hasty pairing dissolve when a better proposer arrives.
- 1initialize every proposer and receiver as free
- 2while some proposer is free and has not proposed to every receiver do
- 3first receiver on 's list to whom has not yet proposed
- 4if is free then
- 5match andtentative: may still trade up
- 6else if prefers to its current partner then
- 7freetrades up to
- 8match and
- 9else
- 10rejectsstays free, proposes lower next time
- 11return the set of matched pairs
Two invariants make the whole proof go, and both are visible in the loop. First, proposers descend their lists: a proposer only ever proposes to receivers strictly worse (for it) than ones it has already been rejected by, so over its life a proposer's offers move monotonically down. Second, receivers improve: once a receiver is matched it stays matched forever, and the partner it holds only ever gets better (for it), since it trades up and never down.
We run a small instance to see deferred acceptance trade up.
A trace on this instance, one proposal per row:
| Proposal | proposes to | currently holds | 's decision | Held pairs after |
|---|---|---|---|---|
| 1 | (free) | accept | ||
| 2 | (free) | accept | ||
| 3 | reject ( prefers ) | |||
| 4 | (free) | accept |
leads with its top choice , but already holds and ranks above , so is turned away and descends to , which is free. Everyone is now matched; no proposer was displaced, so the algorithm stops with , , . We will verify shortly that it is stable.
Termination and the bound
The bound is tight up to the leading constant: an adversarial instance can force
about proposals. With the right data structures every step is
— each proposer keeps a pointer to its next un-proposed receiver, and each
receiver answers do I prefer to my current partner?
by a precomputed
rank table indexed in constant time — so the whole algorithm runs in ,
linear in the size of the input, since the preference lists themselves already
have entries.
Perfection: everyone gets matched
Termination alone could leave some proposer free. It does not.
The key is invariant (ii): a receiver, once approached, never goes back to being free. A free-at-the-end proposer must have approached everyone, which would have matched everyone, leaving no room for it to be unmatched — a contradiction.
Stability: no blocking pair survives
The central theorem. Termination and perfection only say we have a perfect matching; this says the one we have holds together.
The two cases are exhaustive and complementary, and they split exactly along which
side is satisfied.
If never asked , then is already with someone it
likes at least as much as . If did ask and is not with , then
upgraded to someone it likes more than . The figure below traces Case 2, the
subtler one: a rejection only ever moves a receiver up its list, so once it has
rejected it can never again prefer to what it holds.
Together the three theorems show: deferred acceptance always terminates, always returns a perfect matching, and that matching is always stable. A stable matching exists for every instance, constructively.
The asymmetry: proposer-optimal, receiver-pessimal
An instance can have many stable matchings, and the algorithm always lands on a very specific one — the best possible for the proposing side, simultaneously the worst possible for the receiving side. To state it, call a receiver a valid partner of a proposer if some stable matching pairs them.
The mirror statement is immediate.
So the side that does the proposing wins. The figure shows the gap: the same instance admits two stable matchings, and proposer-optimal picks the one where the proposers get their better valid partners and the receivers their worse.
The asymmetry carries a design lesson: in deploying such a system you choose which side proposes, and that choice is not neutral. It also has a strategic consequence — a proposer can never gain by misreporting its list, but a receiver sometimes can, a fact that shaped how the real matches were engineered.
Applications
A near-identical procedure assigns tens of thousands of people every year.
The residency match (NRMP). Graduating medical students and residency programs each rank the other, and the National Resident Matching Program computes a stable assignment by deferred acceptance — historically program-proposing, later changed to applicant-proposing precisely because of the optimality asymmetry above: the side that proposes gets its best stable outcome, and reformers wanted that to be the students. Programs admit several residents, so the real system is the hospitals/residents generalization (receivers have capacity ), but the mechanism, proofs, and stability guarantee carry over almost verbatim.
School choice. Many cities assign students to public schools with a deferred-acceptance mechanism: students (or families) submit ranked lists, schools have priorities and capacities, and the algorithm produces a stable, and under the student-proposing version strategy-proof for students, assignment. The practical appeal is the theory itself: stability means no student-school pair is left mutually preferring each other over their assignments, so no family can credibly complain that a swap was available and denied.
These deployments use every theorem above: existence (a stable matching always exists, so the system can always output something), the bound scaled up (it runs fast at national size), and proposer-optimality (the chosen proposing side provably gets its best stable outcome, which is why who proposes became a policy decision).
Market design and the Nobel-winning legacy
Stable matching launched a whole field.
A Nobel Prize in market design. Gale and Shapley's 1962 paper College Admissions and the Stability of Marriage
was pure combinatorics, but its impact
came decades later.1 Alvin Roth showed the National Resident Matching Program
had, since 1952, been running essentially the deferred-acceptance algorithm, and
that it was stability that kept the market from unraveling; he then
redesigned the residency match and, with Elliott Peranson, the mechanisms behind
school-choice systems in New York and Boston. Roth and Shapley shared the 2012
Nobel Memorial Prize in Economics for the theory of stable allocations and the practice of market design.
2 The basis is this lesson's three theorems:
a stable matching always exists (so the system can always output one), it is
computable fast, and it is proposer-optimal (so who proposes is a policy choice).
Hospitals/residents and the rural-hospitals theorem. Real matches are many-to-one: each hospital admits several residents. The hospitals/residents generalization keeps deferred acceptance almost verbatim (a hospital holds its best applicants so far), and a structural fact emerges — the rural hospitals theorem (Roth, 1986): the set of residents left unmatched, and the number of positions each hospital fills, is identical across every stable matching.3 An under-subscribed rural hospital cannot improve its intake by gaming which stable matching is chosen; if it is short-staffed in one, it is short-staffed in all. This is why the policy fights are over who proposes (the optimality asymmetry) rather than over which stable matching to pick.
Strategy-proofness and its limits. Proposer-optimal deferred acceptance is strategy-proof for the proposing side (Dubins & Freedman, 1981; Roth, 1982): no proposer can gain by misreporting its list.4 But the receiving side can sometimes gain by lying, and no stable mechanism is strategy-proof for everyone at once — a limit that shapes every real deployment. A greedy algorithm from a 1962 math paper became, essentially verbatim, national infrastructure.
Takeaways
- A stable matching is a perfect pairing of two equal-size sides with no blocking pair — no proposer and receiver who each prefer the other to their assigned partner.
- Gale–Shapley deferred acceptance: free proposers propose down their lists; each receiver tentatively holds its best offer so far and trades up, rejecting the rest. Two invariants drive everything — proposers' offers descend, receivers' held partners improve.
- It terminates in at most proposals (no proposer asks the same receiver twice), returns a perfect matching (an unmatched proposer would have asked everyone, matching everyone), and that matching is stable (any unmatched pair has a satisfied side).
- The output is proposer-optimal and receiver-pessimal: every proposer gets its best valid partner and every receiver its worst, independent of the order proposals are made.
- The mechanism runs the residency match and many school-choice systems; whichever side proposes provably gets its best stable outcome, making who proposes a deliberate policy lever.
Footnotes
- Gale, D. & Shapley, L. S. (1962),
College admissions and the stability of marriage,
American Mathematical Monthly 69(1), 9–15 — introduces the stable-marriage problem and the deferred-acceptance algorithm, proving a stable matching always exists. ↩ - Roth, A. E. (2002),
The economist as engineer: game theory, experimentation, and computation as tools for design economics,
Econometrica 70(4), 1341–1378; and the 2012 Sveriges Riksbank Prize in Economic Sciences awarded to Roth and Shapley for the theory of stable allocations and market design. ↩ - Roth, A. E. (1986),
On the allocation of residents to rural hospitals: a general property of two-sided matching markets,
Econometrica 54(2), 425–427 — the rural-hospitals theorem: the set of matched agents is invariant across all stable matchings. ↩ - Dubins, L. E. & Freedman, D. A. (1981),
Machiavelli and the Gale–Shapley algorithm,
American Mathematical Monthly 88(7), 485–494; and Roth, A. E. (1982),The economics of matching: stability and incentives,
Mathematics of Operations Research 7(4), 617–628 — strategy-proofness of deferred acceptance for the proposing side, and the impossibility of full strategy-proofness for both sides. ↩
╌╌ END ╌╌