Stable Marriage Problem Solver

Gale-Shapley Algorithm · Deferred Acceptance · Stable Matching

Find a stable matching for n couples using the Gale-Shapley algorithm. View step-by-step proposals, rejections, and verify stability with no blocking pairs.

Quick Examples

Proposers (Rows) rank Acceptors 1–n

Row i = Proposer i's ranking (1=top choice)

Acceptors (Rows) rank Proposers 1–n

Row j = Acceptor j's ranking (1=top choice)

Labels: Proposers are P1, P2, … Pn  •  Acceptors are A1, A2, … An

What Is the Stable Marriage Problem?

The Stable Marriage Problem (SMP) is a classic combinatorial optimization problem introduced by mathematicians David Gale and Lloyd Shapley in their landmark 1962 paper "College Admissions and the Stability of Marriage." The problem asks: given n men and n women, each with a strict ranking of all members of the opposite group, can we always find a stable matching — and if so, how?

A matching is stable if no unmatched pair (m, w) both prefer each other over their current partners. Such a pair is called a blocking pair — they have a rational incentive to defect from their assigned matches. A matching with no blocking pairs is called stable, and Gale and Shapley proved that a stable matching always exists and can be found efficiently.

The Gale-Shapley Algorithm (Deferred Acceptance)

The Gale-Shapley algorithm, also known as the Deferred Acceptance algorithm, finds a stable matching through an iterative proposal process:

  • Round 1: Every free proposer proposes to their top-ranked acceptor.
  • Each acceptor: If unengaged, tentatively accepts the proposal. If already engaged, compares the new proposer to the current partner — keeps the preferred one and rejects the other.
  • Subsequent rounds: Each rejected proposer proposes to their next preferred acceptor (who hasn't yet rejected them).
  • Termination: The algorithm ends when no free proposers remain. All tentative engagements become the final stable matching.

The key insight is deferred acceptance — acceptors never commit permanently, only tentatively holding the best offer received. This guarantees that every proposer eventually finds a match and that the result is stable.

Optimality Theorem

The Gale-Shapley algorithm has a remarkable optimality property proven by Gale and Shapley:

  • Proposer-optimal: When proposers initiate, every proposer is matched to their best possible partner across all stable matchings. No proposer can do better in any stable matching.
  • Acceptor-pessimal: The same matching is simultaneously the worst stable matching for acceptors — each acceptor gets their worst stable partner.
  • Acceptor-optimal: Swapping roles (acceptors propose) gives the best stable matching for acceptors and worst for proposers.

This means there is a fundamental tension: whoever proposes gets a strategic advantage. The structure of all stable matchings forms a distributive lattice — one extreme being the proposer-optimal matching and the other the acceptor-optimal matching.

Time Complexity: O(n²)

The Gale-Shapley algorithm runs in O(n²) time. Each of the n proposers makes at most n proposals over the course of the algorithm (once rejected by an acceptor, they never propose to the same acceptor again). With at most n² total proposals and O(1) processing per proposal (using precomputed rank lookup tables for acceptors), the total work is O(n²). This is optimal since reading the input itself requires O(n²) time.

Real-World Applications

  • NRMP Medical Residency Match: The National Resident Matching Program has used a variant of Gale-Shapley since 1952 to match ~40,000 medical graduates to hospital residency programs annually in the USA.
  • College Admissions: Many university admission systems use stable matching variants. Gale and Shapley's original paper was titled "College Admissions and the Stability of Marriage."
  • School Choice Programs: New York City (since 2003) and Boston use deferred acceptance to assign ~80,000 students to high schools each year.
  • Kidney Exchange: Matching organ donors to compatible recipients in kidney exchange programs, where compatibility constraints replace preference rankings.
  • Job Markets: Matching candidates to firms in markets like law firm clerkships, economics PhD placement, and medical specialty fellowships.

Gale and Shapley's work was recognized with the 2012 Nobel Memorial Prize in Economic Sciences, awarded to Lloyd Shapley and Alvin Roth (who extended the theory to practice).

Algorithm Reference Table

Property Proposer-Optimal Acceptor-Optimal
Who proposesProposersAcceptors
Best forProposersAcceptors
Worst forAcceptorsProposers
Stable?YesYes
Time complexityO(n²)O(n²)
Max proposals

Frequently Asked Questions

What is the Stable Marriage Problem?
The Stable Marriage Problem asks: given n men and n women each with a ranking of the opposite group, find a stable matching — one where no man-woman pair both prefer each other over their assigned partners (no blocking pair). Gale and Shapley proved in 1962 that a stable matching always exists for any input preferences.
What is the Gale-Shapley algorithm?
The Gale-Shapley algorithm (Deferred Acceptance) solves the Stable Marriage Problem in O(n²). Proposers sequentially propose to their best remaining choice; acceptors tentatively hold their best offer and reject others. Rejected proposers move to their next choice. The algorithm terminates when all proposers are matched, guaranteeing a stable, proposer-optimal result.
What does proposer-optimal vs acceptor-optimal mean?
When proposers propose (standard Gale-Shapley), the result is proposer-optimal: every proposer gets the best partner they can obtain in any stable matching. Acceptors simultaneously get their worst possible stable partner. Running the algorithm with acceptors proposing reverses this — acceptors get their best stable match and proposers get their worst.
What is a blocking pair?
A blocking pair is an unmatched (proposer Pi, acceptor Aj) pair where Pi prefers Aj over their current match AND Aj prefers Pi over their current match. Both have an incentive to abandon their assigned partners and match with each other, destabilizing the matching. A stable matching has zero blocking pairs.
What is the time complexity of the Gale-Shapley algorithm?
The Gale-Shapley algorithm runs in O(n²) time. Each of the n proposers makes at most n proposals total (they never re-propose to a rejected acceptor), giving at most n² proposals. Using precomputed rank lookup tables for acceptors makes each proposal O(1), so the total runtime is O(n²) — optimal since reading the preference lists alone requires O(n²) time.
What are the real-world applications of stable matching?
Stable matching is used in the NRMP medical residency match (matching ~40,000 graduates annually), New York City high school admissions (~80,000 students), Boston school choice, kidney exchange programs matching donors to recipients, law firm clerkship matching, and economics PhD job placement. Shapley and Roth won the 2012 Nobel Prize in Economics for this work.
Is the stable matching always unique?
No. There can be multiple stable matchings for the same preferences. Gale-Shapley finds the proposer-optimal or acceptor-optimal extremes of the stable matching lattice depending on who proposes. Try this solver's two modes on the same input — if the results differ, there are at least two stable matchings. When results are identical, that unique matching is the only stable one.