How Gale-Shapley builds a stable matching

Short answer: The Gale-Shapley deferred-acceptance algorithm builds a stable matching of two sides that have ranked each other. One side proposes, in preference order, and the other side keeps the better proposal so far and rejects the rest. A rejected proposer asks the next choice. The result is a perfect stable matching when every agent ranks every agent on the other side, it is optimal for the proposing side, and it runs in O(n²) proposals.


What the problem actually asks

A matching is unstable when some agent on the left and some agent on the right both prefer each other to the partners they were given. Stability forbids that pair. Several stable matchings can exist. The algorithm’s job is to find one, and the proposing convention decides which one: the proposers get the best partner they can have in any stable matching, and the receivers get the worst partner they can have in any stable matching.

Deferred acceptance is the mechanism. A receiver does not lock the first proposal. The receiver locks the best proposal seen so far and releases the previous tentative partner, who becomes free to propose again.


What the algorithm guarantees

With complete preference lists and equal numbers on both sides, the algorithm returns a perfect stable matching after at most n² proposals, because each proposer walks down a list that is never rewound. The matching is proposer-optimal and receiver-pessimal. Extra memory is the preference tables, the next-proposal index of each proposer, and the tentative match of each receiver.

Incomplete lists and unequal sides return a matching that leaves some agents unmatched and is still stable among the admissible pairs. Ties in preferences need a separate rule. The basic algorithm assumes a total order on each list. A receiver who rejects strategically can sometimes end better than the receiver-pessimal matching. A proposer cannot gain by submitting a false order, which is the proposer strategy-proofness of deferred acceptance. That incentive fact is about the mechanism, and it assumes the other side plays the algorithm as stated.


The working steps

  1. Mark every agent free. Each proposer has a pointer to the head of a preference list.
  2. While some proposer is free and still has someone left to ask, that proposer proposes to the next agent on the list.
  3. If the receiver is free, the receiver tentatively accepts.
  4. If the receiver prefers the new proposer to the tentative match, the receiver rejects the old match, who becomes free, and accepts the new one.
  5. If the receiver prefers the tentative match, the proposal is rejected and the proposer stays free to continue down the list.

The optimality claim is for the proposing side, and only among stable matchings. It is not the maximum-weight matching of numeric scores unless those scores were used to build the rankings and stability, rather than weight, is the objective. Cardinality matching without preferences is Hopcroft-Karp. Assignment with additive costs is the Hungarian algorithm.


Approaches that look simpler and fail

Approach Why it fails when a receiver locks the first proposal
Accept the first proposal permanently The matching can contain a blocking pair. Deferred acceptance exists so a later, better proposal can release an earlier one.
Let both sides propose at once with no extra rule The one-sided proposal order is the proof of stability and of proposer optimality. Simultaneous proposals need a different algorithm.
Claim the result is optimal for both sides The receivers get their worst stable partner. A different stable matching can improve every receiver and worsen some proposer.
Rewind a proposer’s list after a rejection Each pair is proposed at most once. Rewinding can loop, and the O(n²) bound counted a forward scan.

Numeric min-cost assignment belongs to the Hungarian algorithm. Unweighted maximum cardinality belongs to Hopcroft-Karp. Gale-Shapley is the algorithm when the input is ordinal preferences and the constraint is stability.


A working example

This Python fragment runs deferred acceptance. Proposers are A and B, receivers are X and Y. A prefers X then Y. B prefers X then Y. X prefers B then A. Y prefers A then B. The proposer-optimal stable matching pairs A with Y and B with X. It prints A-Y B-X.

proposers = {
    "A": ["X", "Y"],
    "B": ["X", "Y"],
}
receivers = {
    "X": ["B", "A"],
    "Y": ["A", "B"],
}
free = ["A", "B"]
next_choice = {"A": 0, "B": 0}
match = {}  # receiver -> proposer
while free:
    p = free.pop(0)
    r = proposers[p][next_choice[p]]
    next_choice[p] += 1
    if r not in match:
        match[r] = p
    else:
        cur = match[r]
        if receivers[r].index(p) < receivers[r].index(cur):
            match[r] = p
            free.append(cur)
        else:
            free.append(p)
pairs = sorted(f"{p}-{r}" for r, p in match.items())
assert pairs == ["A-Y", "B-X"]
print(*pairs)

Where it is used

The National Resident Matching Program assigns physicians to hospitals with a deferred-acceptance algorithm of the Gale-Shapley type. School-choice systems use the same stable match. A matching that ignores stability can be blocked by a pair who both prefer each other.


When you can drop this algorithm

Drop Gale-Shapley when there are no preferences and any maximum matching will do, or when the objective is a sum of numeric costs. Keep it for two-sided rankings, and decide explicitly which side proposes, because that choice selects the stable matching.


Why this stays published

We publish this so a search for Gale-Shapley returns deferred acceptance, proposer optimality, and the O(n²) bound. Gale-Shapley builds a stable matching by deferred acceptance in O(n²) proposals, and the matching is the best stable matching for the side that proposes.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *