Matching & Mechanism Design

Animate the Gale-Shapley algorithm and explore stable matching step by step

Stable Matching

The stable matching problem asks: given two groups with ranked preferences over each other, can we find a matching where no pair would prefer to break their current matches to be together instead?

In 1962, David Gale and Lloyd Shapley proved the answer is always yes, and gave an elegant algorithm to find such a matching. Their work earned Shapley the 2012 Nobel Prize in Economics (shared with Alvin Roth, who applied the theory to real-world markets like medical residency matching and school choice).

Gale-Shapley Algorithm

Step through the algorithm one proposal at a time. Proposers (left) propose to their most preferred accepter who hasn't rejected them yet. Accepters hold the best proposal they've received and reject the rest. Green highlights show current matches; red strikethroughs show rejections.

Proposer Preferences

Alice:Elm St.Pine Rd.Maple Ln.Oak Ave.
Bob:Oak Ave.Pine Rd.Maple Ln.Elm St.
Carol:Elm St.Oak Ave.Maple Ln.Pine Rd.
Dave:Elm St.Maple Ln.Oak Ave.Pine Rd.

Accepter Preferences

Elm St.:DaveCarolAliceBob
Oak Ave.:DaveAliceCarolBob
Pine Rd.:BobDaveCarolAlice
Maple Ln.:DaveAliceBobCarol

Key insight: The algorithm always terminates with a stable matching. It is proposer-optimal — each proposer gets the best partner they could achieve in any stable matching. Click "New Instance" to generate fresh random preferences and watch how the algorithm adapts.

Properties of Stable Matching

  • Stability — no blocking pair exists (no two people who would rather be matched to each other)
  • Proposer-optimal — the proposing side gets their best stable partner
  • Accepter-pessimal — the accepting side gets their worst stable partner
  • Strategy-proof for proposers — proposers can't benefit from misreporting their preferences
  • Polynomial time — at most n² proposals needed for n pairs

Key Takeaways

  • Stable matching always exists — Gale-Shapley guarantees it
  • Side matters — the proposing side gets the better deal
  • Real-world impact — used for medical residencies, school choice, organ donation