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.
Let be the set allowed by the budget constraint. A demand correspondence is understood to choose only feasible bundles: . Define the direct revealed preference relation by
The indirect revealed preference relation is its reflexive transitive closure. Thus precisely when there is a finite chain whose successive comparisons satisfy . Chains of length zero are included. This makes a preorder on all of , including alternatives that are never chosen. The associated strict preference is
If a convention defines indirect revelation using only positive-length chains, one must add the diagonal before the intersection assertion in part (c) can hold: every complete weak preference relation contains the diagonal.
The congruence axiom requires that an affordable alternative indirectly revealed at least as desirable as a chosen alternative is itself chosen. In the notation of part (a),
Equivalently, a chosen bundle and an affordable but unchosen bundle satisfy . Since already holds, this means . The axiom therefore distinguishes a genuine strict preference for a chosen bundle from indifference between two chosen bundles; it applies to the entire demand correspondence, including budgets with several chosen alternatives.
First pass from the preorder to a partial order. Define the indifference relation by and . Reflexivity and transitivity make this an equivalence relation. On the quotient set , put
Transitivity makes this independent of the representatives, and the quotient relation is antisymmetric, hence a partial order.
We need its order-extension property, including freedom to order incomparable classes. Here is the argument. Order all partial order extensions of by inclusion. The union of a chain of extensions is again a partial order: any finite list of comparisons needed to check an axiom occurs together in one member of the chain. Zorn's lemma therefore supplies a maximal extension. If two classes remain incomparable, adjoin and take the transitive closure of a relation. The only possible new comparisons have the form . A cycle would require a previously existing path , contradicting incomparability. The enlarged relation is still a partial order, contradicting maximality. Consequently the maximal extension is a total order. This also proves the Szpilrajn extension theorem used here.
Lift such a total order to the original bundles by comparing their equivalence classes. The lifted preference relation is complete and transitive, contains , and preserves its strict preference: if , their distinct classes satisfy , and antisymmetry of the extended order prevents the reverse comparison. In particular, the class is nonempty and .
For the reverse inclusion, take a pair with . If , then , so every member of strictly ranks above and excludes . If neither comparison holds, their classes are incomparable. First adjoin , which creates no cycle by the preceding argument, and then extend to a total order. Its lift belongs to and again excludes . Thus every pair outside is outside at least one member of , proving
The intersection proof is an order-theoretic fact about any preorder. The congruence axiom additionally ensures exact rationalization of the observed demand correspondence: every chosen bundle weakly outranks every affordable bundle, while an affordable unchosen bundle is strictly worse by part (b). Hence each completion's budget-maximizing set is exactly the observed chosen set.
. The distinction is small but strict: their normalized expected utilities are and , respectively.
By the Von Neumann-Morgenstern utility theorem, use a representing utility function and apply a positive affine function to set and . This is possible because the given strict preference implies . The two specified indifference relations then give
Taking the expected utility of each lottery yields
Their difference is , establishing the stated strict preference. These numbers measure utility, not monetary reward or an assumed ordering of the subscripts.
Here is the underlying argument directly in terms of the axioms. The independence axiom for lottery preferences permits substitution of indifferent lotteries inside a common mixture. Replacing by its given mixture of , and then replacing , followed by reduction of compound lotteries, gives
and consequently
The transitive relation axiom combines the successive substitutions. Finally, mixture monotonicity says that among mixtures of a strictly better and a strictly worse outcome, the greater probability of the better outcome is strictly preferred; this follows from the independence axiom for lottery preferences and transitivity. Since and , the comparison follows. Complete lottery preferences and continuity of lottery preferences are part of the representation theorem's hypotheses; the direct substitution calculation uses the already supplied indifferences and does not need to invoke continuity of lottery preferences again.[ref]
No: these two strict comparisons violate the independence axiom for lottery preferences. Let denote the certain zero-money lottery. Reduction of compound lotteries gives
For example, the second expression assigns probability to the nonzero reward and to zero. The claimed must therefore imply
which is , contradicting the other claimed strict preference.
The same obstruction is visible in any proposed expected utility representation. Subtract from the utility function, and write and . The first comparison requires , hence , whereas the second requires . No real satisfy both. This argument requires neither risk neutrality nor an assumption that the utility function is increasing in money.
Yes: the stated order has an expected utility representation. For outcomes whose monetary amounts are , respectively, choose
Then
Since , this utility function gives exactly the required strict preferences.
The distinction is that the expected utility hypothesis does not by itself require the utility function to increase with monetary amount. This nonmonotone monetary utility ranks the outcome paying 2 below the outcome paying 1. If increasing monetary utility were imposed as an additional assumption, the stated order would fail: requires because
That additional monotonicity assumption is not printed in this part.
Preference relation 2026-10-14
A weak preference relation records that one alternative is at least as desirable as another. Rational preferences are complete and transitive. Their strict preference and indifference relation are the asymmetric and symmetric parts. On a consumption space, continuity is expressed by a closed set of weakly ranked pairs.
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.
For fixed strict preferences, the same output of the Gale-Shapley algorithm gives each receiver its worst partner obtainable in any stable matching, treating unmatched as last. "Worst" is restricted to stable possibilities: a receiver might still receive its favourite acceptable partner if every stable matching assigns it that partner. Reversing the proposing side gives receivers their best stable partners instead.
For a short proof, let be a proposer-optimal stable matching and any other stable matching. If a receiver preferred to , then proposer optimality would make prefer to , since the partners differ and preferences are strict. The pair would block . Therefore no receiver prefers its partner in to its partner in . In the worked example, right-side ranks in are for , whereas gives them . Neither outcome is universally best for both sides.
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.
Strict preference 2026-10-14
The strict preference associated with a weak preference relation retains comparisons that do not hold in the reverse direction.