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.
For fixed strict preferences, a proposer-optimal stable matching gives each proposer its best partner obtainable in any stable matching, with unmatched ranked last. The Gale-Shapley algorithm achieves all these individual optima simultaneously. In the worked example, gives partners ranked , respectively; the alternative gives them ranks .
Suppose a first rejection of a pair belonging to some stable matching occurs when rejects for . Every partner prefers to has already rejected it, so none can be its partner in . If , then prefers to , while prefers to : a blocking pair in stable matching, a contradiction. Hence no rejected pair is stable, and each proposer receives its best stable partner. Under strict preferences this also makes the outcome independent of proposal order.
Stable matching in a bipartite graph Created 2026-10-14 Updated 2026-10-18
A stable matching in a bipartite graph pairs two groups so that no acceptable unpaired pair would both rather be together. Give a finite bipartite graph a strict preference ordering at each vertex over its neighbours of a vertex. An edge means that both endpoints regard the pair as acceptable; every acceptable partner ranks above being unmatched. Lists may be incomplete, and and may have unequal cardinalities. A matching in a graph is stable when it has no blocking pair in stable matching.
Write for the partner of a vertex , or if is an unmatched vertex in a matching. An unused edge is a blocking pair in stable matching precisely when
where denotes the strict preference at and ranks below every neighbour of a vertex. Both inequalities matter: wanting a different partner alone cannot upset a stable matching. A stable matching need not be a perfect matching; the Gale-Shapley algorithm still finds one with incomplete lists and unequal sides.
Here is an original example on the complete bipartite graph , with and . Read each row from best to worst:
There are two stable matchings,
For , the only unpaired partners preferred by left vertices are wanting and wanting . But prefers its partner to , and prefers its partner to . Hence neither is a blocking pair in stable matching. For , the left vertices want with , with or , and with or . In every case the right vertex prefers its current partner: has , has , and has .
By contrast, is a perfect matching with a blocking pair in stable matching : prefers to , while prefers to . The abandoned partners need not agree to the change; stability tests the incentives of the two endpoints of the unused edge.
There are permutations of right partners. The following table lists every blocking pair in stable matching for each perfect matching; the middle column gives the partners of in that order:
This exhausts the stable matchings: any nonperfect matching in a graph here leaves an unmatched vertex on each side, and those two form a blocking pair in stable matching because every cross-pair is acceptable. Notice that every left vertex prefers and every right vertex prefers . Stability can accommodate opposing interests without selecting a single universally preferred outcome.
A stable matching addresses the possibility of mutually beneficial departures. Its definition contains no numerical total social benefit or maximum-weight matching objective. For example, assigning benefit to each edge of and zero to every other edge makes the unique maximum-weight matching, yet still blocks it under the displayed preference relations. Such external benefits need not represent participants' rankings.