Gale-Shapley Algorithm

Decision Sciences
Introduced by
David Gale, Lloyd Shapley
Year
1962
Type
algorithm
Family
Game-theoretic

What does it do?

The Gale-Shapley algorithm, introduced by David Gale and Lloyd Shapley in 1962, finds a stable matching between two groups, such as medical residents and hospitals or students and schools.

A matching is stable when no pair of agents on opposite sides would both prefer each other to their assigned partners. The procedure is a deferred acceptance process: one side proposes in turn, the other side provisionally holds the best offer it has received, and rejected proposers move down their lists. The standard procedure runs in O(n^2) time, where n is the number of agents on each side.

The intuition behind the method

Think of each resident ranking the hospitals and each hospital ranking the residents. In the first round, every resident applies to their top-ranked hospital, and each hospital provisionally holds its best applicant and rejects the rest. A rejected resident applies to their next choice, and a hospital that receives a better applicant releases the one it was holding. This continues until no resident has an untried hospital left, and the hospitals' final holds form the matching. The result has no blocking pair: no resident and hospital both prefer each other to the partners they were assigned.

History

David Gale and Lloyd Shapley introduced the stable marriage problem and its solution in their 1962 paper, showing that a stable matching always exists for two-sided markets. Their work started a large literature on matching theory. Alvin Roth's work on matching procedures examined how stability and truthful reporting interact. In 2012, Alvin Roth and Lloyd Shapley shared the Nobel Prize in Economics for their work on stable allocations and market design.

When to use it

Use the Gale-Shapley algorithm for two-sided one-to-one matching when both sides have complete, strict ranked preferences and stability is required, for example where an unstable pair could later renegotiate its match. Decide deliberately which side proposes, because the outcome favors that side. The description on this page applies to one-to-one matching, in which each agent holds at most one partner. Markets with several seats per hospital or school need a capacitated variant, which this page does not describe. Do not use the standard algorithm when preferences are incomplete, when agents must be matched as couples or in other joint arrangements, or when the main goal is to maximize total welfare rather than stability. The limitations and pitfalls below describe these cases.

Limitations and considerations

  • Not Pareto optimal: a stable matching need not be Pareto efficient, so some other matching could leave every agent at least as well off and at least one agent strictly better off.
  • Asymmetric incentives: proposing agents cannot gain by misreporting their preferences under the proposing-side procedure, but responding agents can sometimes gain by misrepresenting theirs. Roth's result shows that no matching procedure always yields a stable outcome and also gives every agent an incentive to report truthfully, so truthful reporting cannot be assumed on both sides.
  • Outcome depends on the proposing side: swapping which group proposes can change the matching, so this choice is a design decision in its own right.
  • Constraints are not built in: the standard algorithm does not handle couples or regional restrictions. Extensions exist, but they require modification and need not guarantee stability.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Stability is guaranteed: the final matching contains no blocking pair, so no two agents can both gain by leaving their assigned partners.
  • Bounded running time: the standard procedure runs in O(n^2) time, so it remains tractable as the number of agents grows.
  • Proposer-optimal result: among all stable matchings, each proposing agent obtains the best partner it could receive in any stable matching.
  • Ordinal input only: the procedure needs ranked preference lists and a fixed proposing side, not numerical utilities.

Applications

  • Medical residency matching: a closely related problem to the matching run by the NRMP, which pairs medical school graduates with residency programs.
  • School choice: assigning students to schools according to their preferences and capacity constraints. Such markets are closely related to the matching studied here but typically involve several seats per school.
  • Kidney exchange: a related two-sided compatibility matching problem studied in the same literature.
  • Labor market matching: connecting job seekers and employers in two-sided markets.
  • Online dating: a conceptually similar two-sided matching problem.

How it works

How it works

1

Each agent on both sides submits a complete, strict ranked preference list over the agents on the other side; ties must be broken before the run. In the standard form each agent has one seat, so each agent can hold at most one partner. Which side proposes is fixed in advance, and this choice shapes the final outcome.

2

Each proposer that is not currently held proposes to the highest-ranked agent on the other side to whom it has not yet proposed. A proposer with no remaining candidates stays unmatched.

3

Each receiver compares the new proposal with any partner it currently holds and keeps whichever it ranks higher, releasing the other. A receiver with no current partner provisionally accepts the proposal.

4

The algorithm stops when no proposer is free to make another proposal. The final provisional pairings form the matching, which is stable and gives each proposer its best stable partner. Worked example: residents Ana, Ben and Cem apply to hospitals X, Y and Z. Residents rank Ana: X, Y, Z; Ben: X, Z, Y; Cem: Y, X, Z. Hospitals rank X: Ben, Ana, Cem; Y: Ana, Cem, Ben; Z: Cem, Ben, Ana. In the opening proposals Ana and Ben both apply to X, and Cem applies to Y. X holds Ben and rejects Ana, while Y provisionally holds Cem. Ana then applies to Y, which prefers Ana to Cem, so Y holds Ana and releases Cem. Cem applies to X, which keeps Ben and rejects Cem. Cem then applies to Z, which holds Cem. No resident is free, so the algorithm terminates with Ana–Y, Ben–X and Cem–Z. Check: test every resident–hospital pair that is not matched together and confirm that the pair would not both prefer each other. Ana and X: X prefers its holder Ben, so the pair does not block. Ana and Z: Ana prefers Y, so the pair does not block. Ben and Y: Ben prefers X, so the pair does not block. Ben and Z: Ben prefers X, so the pair does not block. Cem and X: X prefers Ben, so the pair does not block. Cem and Y: Y prefers Ana, so the pair does not block. With no blocking pair, the matching is stable.

Common pitfalls

  • Treating stability as fairness: a stable matching can still be heavily skewed toward one side, so stability alone does not show that the outcome is equitable.
  • Confusing stability with welfare: a stable matching is not necessarily the one that maximizes total welfare.
  • Overlooking the proposing-side advantage: members of the proposing side tend to obtain higher-ranked partners than they would if the other side proposed.
  • Running with incomplete lists: the algorithm assumes complete preference lists, so results from incomplete lists need a modified procedure before they can be read as stable.

Frequently asked questions

Why is stability important in matching?

Without stability, matched pairs might later discover that they prefer each other to their assigned partners, which creates an incentive to break the matching. Stable matchings prevent such blocking pairs, so the matching can persist without renegotiation.

Does the Gale-Shapley algorithm always produce the same matching?

No. Different preference lists can yield different stable matchings, and which side proposes affects the result. The order in which agents on the proposing side make their proposals does not change the outcome. There is always a proposer-optimal stable matching, which is best for every proposing agent, and a responder-optimal stable matching, which is best for every receiving agent.

How does Gale-Shapley handle preferences over groups (e.g., couples)?

The standard algorithm does not directly handle couples or other joint constraints. Extensions for couples exist, but they are more complex and may not guarantee stability for all preference profiles.

Related methods

Sources

  1. Gale, D., & Shapley, L. S. (1962). College admissions and the stability of marriage. The American Mathematical Monthly, 69(1), 9-15.

    Open DOI reference
  2. Roth, A. E. (1984). The economics of matching: Stability and incentives. Mathematics of Operations Research, 7(4), 617-628.

    Open DOI reference