Overview

2 Gale-Shapley Algorithm

The chapter introduces the Gale-Shapley algorithm as a classic stable matching method for pairing two equal-sized groups according to their preferences, such as students with universities or users in dating platforms. Its central goal is to produce a matching with no unstable pair—meaning no unmatched pair would both prefer each other over their assigned matches. The explanation emphasizes that the algorithm always finds a stable outcome when the groups are of equal size, and that its “deferred acceptance” style works by letting one side propose while the other side temporarily holds the best offer seen so far.

Through step-by-step examples, the chapter shows how proposals, acceptances, and rejections unfold until everyone is matched. In the simple case, each proposer gets their first choice immediately, while in the more realistic case, conflicts force participants to move down their preference lists and try again until a stable arrangement emerges. The process is illustrated as iterative and deterministic: once a proposal is rejected, that pairing is not revisited, and the algorithm eventually terminates with a complete stable matching.

The chapter also highlights an important asymmetry: the side that proposes has an advantage and tends to receive the best possible stable outcome for itself, while the receiving side may settle for less preferred matches. This proposer bias is a key property of the algorithm and matters in real applications such as medical residency matching, ride-sharing, online dating, and resource allocation problems. Finally, it notes that the algorithm runs in quadratic time in the worst case, making it efficient enough for many practical systems while still guaranteeing stability and predictable behavior.

shows universities proposing their first preferences. X proposes to Alice, Y to Bob, and Z to Carol.
The receiver's preference list is checked, where Alice prefers X, Bob prefers Y, and Carol prefers Z. Final matching is completed once all students accept the proposal.
Round 1 where students are proposers. Alice, Bob, and Carol propose to their first choices from the list. A conflict is seen between Bob and Carol, where both students opt for university X. X prefers Bob over Carol and rejects Carol.
In Round 2, Carol proposes to her second choice while Alice and Bob keep their previous choices only. A conflict is seen again between Alice and Carol with university Y. Y prefers Alice over Carol, and Carol is politely rejected again.
In Round 3, Carol is left with one choice, university Z. Z is not matched with anyone and prefers Carol too. Z accepts Carol, and matching is complete without conflict.
Proposers and receivers are interchanged. The matching completes in one pass itself. Universities get their first preference matched, but students do not control the match.
Students accept the proposals, as there are not better offers to choose from. In the final matching, students are matched to their second and third choices compared to when students were proposers.

Summary

  • The Gale-Shapley algorithm performs steps to pair participants such that the matching is based on preferences and no pair wants someone other than their existing match.
  • This algorithm is also called the Deferred Acceptance or Propose-and-Reject Algorithm.
  • Each set of participants, like students and universities, has a ranked list of preferences in decreasing order. There are proposers and receivers who propose and accept/reject the proposals.
  • Stable matching in this algorithm is defined as every participant in the matching cannot switch to a new participant based on their preference order.
  • The algorithm favors the proposers who are matched with their best preference, while the receivers are matched with the preference they do not have control to change. This is by design and works best where the proposing side has to be favored in the result.
  • The output guarantees termination, and every participant is matched at the end of the algorithm. The time complexity in worst case is O(N^2)
  • Everyone always gets matched, and all matches are stable. Until finalized, matches are temporary and decided later, making it deterministic.
  • Online dating, ride sharing, delivery platforms, and college admissions are a few real applications where the Gale-Shapley algorithm is used fundamentally.

FAQ

What problem does the Gale-Shapley algorithm solve?The Gale-Shapley algorithm solves the stable matching problem: it matches two equally sized groups, such as students and universities, based on ranked preferences so that no unmatched pair would both prefer each other over their assigned matches.
Why is stability important in Gale-Shapley matching?Stability matters because it prevents blocking pairs. A matching is stable when there is no proposer-receiver pair that would rather be matched with each other than with their current assignments, which avoids instability and constant rematching.
What does “deferred acceptance” mean in this algorithm?Deferred acceptance means proposals are not treated as final immediately. A receiver may tentatively accept a proposal, but later reject it if a more preferred proposer arrives, allowing the matching to improve until it becomes stable.
How does the proposal-and-rejection process work?Each free proposer goes down their preference list and proposes to the next option not yet tried. If the receiver is free, they accept tentatively; if already matched, they keep the better proposer according to their ranking and reject the other.
What happens in the example when multiple students propose to the same university?When more than one student proposes to the same university, the university compares them using its preference list and keeps the one it prefers most. The rejected student stays free and must propose to their next choice in the next round.
Does Gale-Shapley always produce a complete matching?Yes, when the two groups are equal in size and everyone has a complete preference list, the algorithm guarantees that all participants will be matched by the end.
Who benefits most from Gale-Shapley: the proposers or the receivers?The proposing side benefits most. The algorithm is proposer-optimal among all stable matchings, meaning proposers get the best stable outcome available to them, while receivers may end up with less preferred matches.
What is the time complexity of the Gale-Shapley algorithm?In the worst case, the algorithm runs in O(N²) time, where N is the number of participants on each side. This happens because each proposer may need to make proposals to many or all receivers.
What real-world applications use Gale-Shapley matching?It is used in scenarios like college admissions, the National Resident Matching Program (NRMP), online dating and matchmaking, ride-sharing and delivery assignment, and other preference-based allocation problems.
Why does the result change when universities propose instead of students?Because Gale-Shapley is biased toward the proposing side. If universities propose, they get the more favorable stable outcome, while students may receive lower-ranked choices than they would if they were the proposers.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Algorithms Every Programmer Should Know ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Algorithms Every Programmer Should Know ebook for free