Discrete Mathematics and Combinatorics

The Stable Marriage Problem and the Gale-Shapley Algorithm

Follow a step-by-step matching example to see why a stable pairing always exists and why it matters who makes the proposals.

Key takeaways

  • A matching is stable when no unmatched pair both prefer each other to the partners they were given.
  • The Gale–Shapley procedure has one side propose in order of preference while the other side holds its best offer so far.
  • It always finishes and always produces a stable matching.
  • The proposing side gets the best partners available in any stable matching, and the receiving side gets the worst.
  • Matching problems with only one group, such as pairing roommates, may have no stable solution.

The stable marriage problem asks how to pair the members of two equal-sized groups, each of whom ranks everyone in the other group, so that no two people would both rather be with each other than with their assigned partners. David Gale and Lloyd Shapley showed in 1962 that such a pairing always exists, and they gave a simple procedure that finds one.

The marriage wording is traditional. The same mathematics applies to students and mentors, trainees and programs, or any two sides with preferences about each other, and this article uses students and mentors.

What stability means

Suppose student Ana is assigned to mentor Xu, and student Ben to mentor Yi. If Ana would rather have Yi, and Yi would rather have Ana than Ben, then Ana and Yi have a reason to abandon the plan. They are called a blocking pair.

A matching with no blocking pair is stable. Stability does not mean everyone is happy. It means no two people can improve their situation by agreeing between themselves.

To test a proposed matching, go through each student. For every mentor the student ranks above the assigned one, ask whether that mentor ranks the student above their own assignment. A single yes means the matching is unstable. With n students there are fewer than n² such questions.

The procedure

  1. Each unmatched student proposes to the highest-ranked mentor who has not yet rejected them.
  2. Each mentor looks at any new proposals together with the offer currently held, keeps the one ranked highest and rejects the rest.
  3. Rejected students become unmatched again.
  4. Repeat until every student is held by a mentor.
  5. The held offers become final.

The key feature is that acceptance is tentative. A mentor who holds a student can trade up later. For this reason the method is also called deferred acceptance.

Deferred acceptance, round by round
Reject others Rejected students move down their lists. Hold one The best offer is kept, but only tentatively. Propose Each free student asks their best remaining choice. Compare Each mentor ranks new offers against the one held. 1 2 3 4 Propose Each free student asks their best remaining choice. 1 Compare Each mentor ranks new offers against the one held. 2 Hold one The best offer is kept, but only tentatively. 3 Reject others Rejected students move down their lists. 4

A worked example

Three students A, B, C and three mentors X, Y, Z have these rankings, best first:

Student Ranking Mentor Ranking
A X, Y, Z X B, A, C
B Y, X, Z Y A, B, C
C X, Y, Z Z A, B, C

Run the procedure with students proposing, one at a time:

A proposes to X.   X holds A.
B proposes to Y.   Y holds B.
C proposes to X.   X prefers A to C, so C is rejected.
C proposes to Y.   Y prefers B to C, so C is rejected.
C proposes to Z.   Z holds C.
Result: A–X, B–Y, C–Z.

Check stability. A and B have their first choices, so neither would move. C would prefer X or Y, but X prefers A and Y prefers B to C. There is no blocking pair.

Now let the mentors propose

Using the same lists with mentors proposing:

X proposes to B.   B holds X.
Y proposes to A.   A holds Y.
Z proposes to A.   A prefers Y to Z, so Z is rejected.
Z proposes to B.   B prefers X to Z, so Z is rejected.
Z proposes to C.   C holds Z.
Result: A–Y, B–X, C–Z.

This is also stable, and it is a different matching. Now X and Y have their first choices, while A and B have their second. The lists did not change. Only the direction of proposing did.

Checking all six possibilities

Three students and three mentors can be matched in only 3! = 6 ways, small enough to test completely.

Matching Stable? A blocking pair
A–X, B–Y, C–Z yes none
A–Y, B–X, C–Z yes none
A–X, B–Z, C–Y no B and Y
A–Y, B–Z, C–X no A and X
A–Z, B–X, C–Y no A and Y
A–Z, B–Y, C–X no A and X

Take the fourth row. A would rather have X than Y, and X would rather have A than C, so the two have every reason to leave together.

Exactly two matchings survive, and they are the two the procedure found from its two directions. With larger groups there can be many stable matchings between these extremes, and the number of all matchings, n!, soon becomes too large to check one by one. For 10 pairs it is already 3,628,800, while the procedure needs at most 100 proposals.

Why it always works

It ends. Nobody proposes to the same mentor twice. With n students and n mentors there are at most n² proposals.

Everyone gets matched. Once a mentor receives an offer, they hold someone from then on. If some student were rejected by all n mentors, every mentor would be holding a different student. That is n students held, leaving nobody over.

The result is stable. Suppose student s prefers mentor m to the final assignment. Then s proposed to m earlier and was rejected, in favor of someone m ranked higher. A mentor's held offer only improves over time, so m ends with someone preferred to s. So s and m cannot block.

The argument is a good model of proof by tracking a quantity that moves in one direction.

Who benefits

Gale and Shapley showed that when students propose, each student gets the best mentor they could have in any stable matching. Remarkably, the order in which free students make their proposals does not affect the result.

The flip side, noted in later work, is that each mentor gets the worst student they could have in any stable matching. In the example, mentor X ends with A in the student-proposing version but could have had B.

Honesty also depends on the side. In the standard model, no student can gain by misreporting preferences to the proposing-student procedure. A mentor sometimes can, though doing it requires good knowledge of everyone else's lists.

Real markets

Many mentors, hospitals or schools can accept several people. The procedure extends easily: each holds offers up to its capacity.

The best-known application is the matching of medical graduates to residency positions in the United States through the National Resident Matching Program. A centralized procedure was used there before Gale and Shapley's paper, and Alvin Roth later analyzed it and helped redesign it in the 1990s into an applicant-proposing method that also handles complications such as couples. Couples can make it possible that no stable matching exists. Some school districts use deferred acceptance to assign students to schools.

Shapley and Roth received the 2012 Nobel Memorial Prize in Economic Sciences for work on stable allocations and market design. Rules in particular programs change, so check the organization's own description for current practice.

Where stability can fail

Suppose there is a single group that must pair off, as with roommates. Take four people. A ranks B first, B ranks C first, C ranks A first, and all three rank D last. Whoever is paired with D is ranked first by one of the other two, and that person would also rather escape their own partner. So every pairing has a blocking pair.

With two distinct groups, this kind of cycle cannot defeat the procedure.

Common mistakes when running it by hand

  • Making an acceptance permanent too early. A mentor holding a student has not committed. Keep checking new proposals against the held one until nobody is left proposing.
  • Letting a rejected student ask the same mentor again. Cross the name off. Lists are only walked downward.
  • Comparing by the proposer's ranking. When a mentor chooses between two students, only the mentor's list counts at that moment.
  • Assuming first choices are guaranteed to the proposing side. Proposers do as well as any stable matching allows, which may still be far down their lists if many of them want the same few partners. If all students rank mentors identically, the mentors' preferences decide everything.

Connections and limits

Stability differs from maximizing total satisfaction. A matching that gives the largest sum of preference scores may contain a blocking pair. It also differs from maximum bipartite matching, which asks only for as many acceptable pairs as possible, and from Hall's marriage theorem, which says when everyone on one side can be matched at all.

The number of stable matchings for a given set of lists can grow exponentially with n, yet Gale–Shapley finds one in a number of steps proportional to n². It is a case where a greedy-looking rule works, because its commitments stay reversible. For the economic logic of designing rules that reward truthfulness, see second-price auctions.

Common questions

What if lists are incomplete?

If people may leave unacceptable partners off, the procedure still yields a stable matching, though some may remain unmatched. In every stable matching for the same strict lists, the same people end up matched.

What about ties?

Ties can be broken arbitrarily to get a matching that is stable in a weak sense, but the choice of tie-break can affect who benefits and how many get matched.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 45 articles in Discrete Mathematics and Combinatorics →

Latest news on this topic