Stable Marriage¶
- Title:
Stable Marriage - Judge / source:
Canonical Gale-Shapley benchmark - Original URL: https://www.ams.org/publicoutreach/feature-column/fc-2015-03
- Secondary topics:
Deferred acceptance,Blocking pairs,Proposer-optimal stable matching - Difficulty:
medium - Subtype:
Equal-size stable matching with complete strict preferences - Status:
solved - Solution file: stablemarriage.cpp
Why Practice This¶
This is the cleanest first in-repo flagship for Stable Marriage.
The benchmark is intentionally canonical instead of judge-idiosyncratic:
nproposersnaccepters- each side gives one strict permutation of the other side
- print one stable matching
So the hard part is exactly the lane itself:
- recognize that stability is the objective
- trust deferred acceptance
- keep the boundary against assignment and cardinality matching sharp
Recognition Cue¶
Reach for Gale-Shapley when:
- each side ranks the other side
- every participant must end matched
- the statement forbids blocking pairs, envy pairs, or mutually preferred deviations
- any stable witness is accepted
The strongest smell here is:
- "find a stable matching under two-sided preferences"
That is exactly this lane.
Problem-Specific Route¶
This benchmark does not want:
- Hopcroft-Karp, because the objective is not maximum number of pairs
- Hungarian, because there is no additive cost matrix to optimize
- min-cost flow, because the whole point is stability under preferences, not transport under capacities
The clean route is:
- let proposers walk their lists from best to worst
- let each accepter keep only the best proposal seen so far
- requeue any rejected proposer
- stop when nobody is free with remaining options
That is exactly Gale-Shapley deferred acceptance.
Core Idea¶
The useful monotone fact is:
- once accepter
arejects proposerp,awill never end with anyone worse thanp
So rejections are permanent.
That means each proposer advances only forward through the list, and the whole process makes at most n^2 proposals.
When the queue empties, no blocking pair can remain:
- if proposer
ppreferred accepteraover the final partner, thenpmust have proposed toaearlier - so
arejectedpin favor of someone preferred more - therefore
acannot preferpover the final partner
That contradiction is the stability proof.
Complexity¶
- Gale-Shapley with inverse rank tables:
O(n^2) - memory:
O(n^2)for the two preference tables plus accepter-rank lookup
Input / Output Contract For This Repo¶
This repo's canonical benchmark uses:
- one integer
n - next
nlines: proposers' preference lists, each a permutation of1..n - next
nlines: accepters' preference lists, each a permutation of1..n
The solution prints:
nlines- line
iprintsi matched_partner - both ids are
1-based
Any stable perfect matching is acceptable for this benchmark contract. The repo solution prints the proposer-optimal one produced by Gale-Shapley.
Pitfalls / Judge Notes¶
- Stability is not the same as minimum total rank sum.
- The proposing side matters.
- Precompute
rank[a][p]so one preference comparison isO(1). - Convert to
0-based internally even if the I/O stays1-based.
Reusable Pattern¶
- Topic page: Stable Marriage
- Practice ladder: Stable Marriage ladder
- Starter template: gale-shapley-stable-marriage.cpp
- Notebook refresher: Stable Marriage hot sheet
- Compare points:
- Matching
- Hungarian / Assignment Problem
- This note adds: the clean canonical benchmark where stability itself is the target, not only a modeling detail under some larger flow or assignment story.
Solutions¶
- Code: stablemarriage.cpp