Gale-Shapley algorithm Created 2026-10-14 Updated 2026-10-18
The Gale-Shapley algorithm constructs a stable matching by letting one side propose and the other hold its best offer so far. The word "deferred" is essential: a held proposal is tentative and can be replaced. David Gale and Lloyd Shapley presented this construction in their 1962 paper, College Admissions and the Stability of Marriage, §3, printed pp. 12–13; §4 extends it to college quotas and unacceptable pairs.
For the one-partner bipartite graph model, designate as proposers and as receivers:
  • Start with the empty matching in a graph. Each proposer keeps a pointer to its first untried acceptable partner.
  • Choose an unmatched vertex in a matching whose list is not exhausted. It proposes to its highest-ranked untried neighbour of a vertex and advances its pointer permanently.
  • If holds nobody, it holds . Otherwise compares with its currently held proposer, keeps the one it prefers, and rejects the other. A displaced proposer becomes unmatched and may propose again later.
  • Stop when every unmatched proposer has exhausted its list. The held pairs are the output matching in a graph.
Held proposers wait; only unmatched proposers act. A receiver can replace its held partner, whereas a proposer never tries the same edge twice. Isolated vertices simply remain unmatched. These rules also describe incomplete acceptable lists and unequal sides without adding fictitious acceptable partners.
For the preference lists under stable matching, choose available proposers in queue data structure order, initially , adding rejected proposers at the end. The proposals are , , , , . At the fourth proposal, replaces its held by because . This frees to try , producing .
Figure 1.
Five proposals in the Gale-Shapley algorithm: Y replaces a tentative partner before the final stable matching
. Details.
Reverse the proposing side. Starting in the order , the proposals are , , , . Now rejects because it already holds . The result is . Thus the choice of proposing side can change the output even with strict preferences and complete lists.
Why does the procedure work? Each edge receives at most one proposal, so at most proposals occur. At all times, the held edges form a matching in a graph, and each receiver holds its favourite proposer among all proposals received. A receiver's held partner can only improve according to its strict preference. If a proposer prefers to its final partner, or ends unmatched while is acceptable, it must already have proposed to . A rejection means then held someone better, and its final partner is at least as good. Such an edge cannot be a blocking pair in stable matching. This covers every possible blocking pair in stable matching, proving stability at termination.
With a queue data structure of unmatched proposers, a next-choice pointer for each list, and constant-time receiver-rank lookups, each proposal takes constant work. Using Big O notation, the time complexity is including initialization, provided list construction and rank preprocessing also take . Edge-indexed ranks with cross-referenced list entries, or expected constant-time hash lookups, allow sparse storage proportional to . Repeatedly scanning a receiver's preference list to compare offers would lose this bound. For complete lists with vertices per side, and the bound is . Initializing full rank tables itself costs time and space, even when only a sparse set of pairs is acceptable.
The Gale-Shapley algorithm does more than establish existence: with strict preferences, its output is a proposer-optimal stable matching.[ref] It is also a receiver-pessimal stable matching, as proved below. Both qualifications compare partners across stable matchings.