Discrete MathematicsPlanarity, Coloring and Matching4 of 4
Planarity, Coloring and Matching · 4

Stable Matching: The Proposal Algorithm

When every student is qualified for every hospital, finding a one-to-one matching is easy, so the real problem is how to pair them up once everyone ranks their choices from favorite to least favorite. A stable matching is a one-to-one pairing where no student and hospital both prefer each other over the partners they were assigned.

Rogue Pairs and Stable Matchings
students\text{students}
hospitals\text{hospitals}
123\htmlClass{lc-mute}{\mathclap{1}\hspace{1.94em}\mathclap{2}\hspace{1.94em}\mathclap{3}}
123\htmlClass{lc-mute}{\mathclap{1}\hspace{1.94em}\mathclap{2}\hspace{1.94em}\mathclap{3}}
XX
YY
ZZ
AA
ZZ
YY
XX
BB
XX
ZZ
YY
CC
XX
BB
AA
CC
YY
AA
BB
CC
ZZ
CC
BB
AA
Text solution
Write the preference listsEach student ranks the three hospitals from first choice to last choice, and each hospital ranks the three students:
StudentsA:X,Y,ZB:Z,Y,XC:X,Z,YHospitalsX:B,A,CY:A,B,CZ:C,B,A\displaystyle \begin{aligned} \text{Students}\qquad & A: X, Y, Z \\ & B: Z, Y, X \\ & C: X, Z, Y \\ \text{Hospitals}\qquad & X: B, A, C \\ & Y: A, B, C \\ & Z: C, B, A \end{aligned}
Check who wants to switch in A–X,  B–Z,  C–Y\assigned{A\text{–}X,\; B\text{–}Z,\; C\text{–}Y}Students AA and BB already have their first choices, while CC is paired with last choice Y\assigned{Y} and prefers both XX and ZZ.
Check whether a preferred hospital wants CC backHospital ZZ is paired with its second choice B\assigned{B} and ranks CC first, so C\fails{C} and Z\fails{Z} both want to leave their assigned partners for each other:
(C,Z)⏟rogue pair (instability):C prefers Z over Y,andZ prefers C over B\displaystyle \begin{aligned} & \underbrace{\fails{(C, Z)}}_{\text{rogue pair (instability)}}: \\ & \fails{C} \text{ prefers } \fails{Z} \text{ over } \assigned{Y}, \\ \text{and}\quad & \fails{Z} \text{ prefers } \fails{C} \text{ over } \assigned{B} \end{aligned}

Because (C,Z)\fails{(C, Z)} forms a rogue pair, the matching A–X,  B–Z,  C–Y\assigned{A\text{–}X,\; B\text{–}Z,\; C\text{–}Y} falls apart and is unstable.

Giving more people their first choice does not make a matching stable: A–Y,  B–Z,  C–X\assigned{A\text{–}Y,\; B\text{–}Z,\; C\text{–}X} gives three participants their first choice and still has the rogue pair (A,X)\fails{(A, X)}. A matching holds together only when there are zero rogue pairs.

A matching is stable only when no student and hospital would both rather be with each other than with their current partners.
1

The Proposal Algorithm

Locking in matches early causes regret: if Hospital ZZ commits to BB and then CC applies, ZZ would rather have CC. To build a stable matching without regret, the Proposal Algorithm (or Gale-Shapley algorithm) keeps every offer tentative ("Maybe") until the very end: unmatched students propose to the highest hospital left on their list, and each hospital holds onto its best offer so far while rejecting the rest.

The Proposal Algorithm
students\text{students}
hospitals\text{hospitals}
123\htmlClass{lc-mute}{\mathclap{1}\hspace{1.94em}\mathclap{2}\hspace{1.94em}\mathclap{3}}
123\htmlClass{lc-mute}{\mathclap{1}\hspace{1.94em}\mathclap{2}\hspace{1.94em}\mathclap{3}}
XX
YY
ZZ
AA
ZZ
YY
XX
BB
XX
ZZ
YY
CC
XX
BB
AA
CC
YY
AA
BB
CC
ZZ
CC
BB
AA
1.unmatched students proposeto the highest hospital left2.hold the best offer so far: maybe;reject everyone else\begin{array}{ll} \text{1.} & \text{unmatched students propose} \\ & \text{to the highest hospital left} \\[6pt] \text{2.} & \text{hold the best offer so far: } \htmlClass{lc-pink}{\text{maybe}}\text{;} \\ & \text{reject everyone else} \end{array}
Text solution
Round 1, unmatched students propose to their top choiceAA and CC both propose to XX while BB proposes to ZZ; Hospital ZZ holds B\maybe{B} on "Maybe," and Hospital XX checks its list (B,A,CB, A, C), holds A\maybe{A}, and rejects CC:
Held on Maybe: X←A,Z←BCrossed off: C:X,Z,Y\displaystyle \begin{aligned} \text{Held on Maybe: } & \maybe{X \gets A},\quad \maybe{Z \gets B} \\ \text{Crossed off: } & C: \fails{\cancel{X}}, Z, Y \end{aligned}
Round 2, rejected student proposes to next choice and hospital trades upUnmatched student CC proposes to second choice ZZ, and because ZZ's list (C,B,AC, B, A) ranks CC above BB, ZZ trades up to C\maybe{C} and rejects BB:
Held on Maybe: X←A,Z←CCrossed off: B:Z,Y,X\displaystyle \begin{aligned} \text{Held on Maybe: } & \maybe{X \gets A},\quad \maybe{Z \gets C} \\ \text{Crossed off: } & B: \fails{\cancel{Z}}, Y, X \end{aligned}
Round 3, remaining unmatched student proposesUnmatched student BB proposes to second choice YY, which is empty and holds B\maybe{B}, leaving no student unmatched:
Final matching:A–X,B–Y,C–Z\displaystyle \begin{aligned} & \text{Final matching:} \\ & \kept{A\text{–}X},\quad \kept{B\text{–}Y},\quad \kept{C\text{–}Z} \end{aligned}
Verify stability by checking hospitals ranked above each student's matchStudent AA has first choice XX, so only BB (who prefers ZZ over Y\kept{Y}) and CC (who prefers XX over Z\kept{Z}) would want to switch, and neither hospital wants them over its final match:
(B,Z):Z holds 1st choice C (prefers C over B)(C,X):X holds 2nd choice A (prefers A over 3rd choice C)\displaystyle \begin{aligned} & (B, Z):\quad Z \text{ holds 1st choice } \kept{C} \\ & \quad \text{ (prefers } \kept{C} \text{ over } B\text{)} \\ & (C, X):\quad X \text{ holds 2nd choice } \kept{A} \\ & \quad \text{ (prefers } \kept{A} \text{ over 3rd choice } C\text{)} \end{aligned}

With zero rogue pairs, A–X,  B–Y,  C–Z\kept{A\text{–}X,\; B\text{–}Y,\; C\text{–}Z} is a stable matching.

As proposals and rejections play out step by step, each rejection pushes a student strictly down their preference list while each trade-up moves a hospital strictly up its list, settling on the same stable matching regardless of which unmatched student proposes next.

Every rejection pushes a student down their list while every hospital only trades up, settling on the same stable matching no matter which unmatched student proposes next.
2

Why the Algorithm Always Works

For any setup with NN students and NN hospitals, the Proposal Algorithm is guaranteed to stop in at most N2\kept{N^2} proposals with everyone matched in a stable matching.

Why the Algorithm Always Works
loop forever?\text{loop forever?}
rogue pair?\text{rogue pair?}
CC
XX
1\htmlClass{lc-mute}{1}
ZZ
2\htmlClass{lc-mute}{2}
YY
3\htmlClass{lc-mute}{3}
ZZ
1\htmlClass{lc-mute}{1}
CC
2\htmlClass{lc-mute}{2}
BB
3\htmlClass{lc-mute}{3}
AA

Every guarantee of the algorithm follows from two loop invariants that hold from the first proposal to the last:

Proposers (students):move strictly downtheir preference listswith each rejectionReceivers (hospitals):once offered, stay matchedand only trade up their lists\displaystyle \begin{aligned} & \text{Proposers (students):} \\ & \quad \text{move strictly down} \\ & \quad \text{their preference lists} \\ & \quad \text{with each rejection} \\ & \text{Receivers (hospitals):} \\ & \quad \text{once offered, stay matched} \\ & \quad \text{and only trade up their lists} \end{aligned}
Student\text{Student}
SS
HH
1st\htmlClass{lc-mute}{\text{1st}}
final\htmlClass{lc-green}{\text{final}}
Hospital\text{Hospital}
HH
SS
1st\htmlClass{lc-mute}{\text{1st}}
held then\htmlClass{lc-pink}{\text{held then}}
final\htmlClass{lc-green}{\text{final}}
Total proposals\displaystyle \text{Total proposals} ≤N2\displaystyle \le \kept{N^2}

When proving the algorithm stops, count proposals rather than cross-outs: a step where an empty hospital accepts an offer still advances the algorithm by 11 proposal even though nothing is crossed off.

3

Proposers Versus Receivers

Even on the same preference lists, more than one stable matching can exist, and who proposes decides which one you get: the Proposal Algorithm always gives the proposers their best possible stable partner and the receivers their worst possible stable partner.

Proposers Versus Receivers
Who gets the better deal:proposers or receivers?\begin{array}{c} \text{Who gets the better deal:} \\ \text{proposers or receivers?} \end{array}
students\text{students}
hospitals\text{hospitals}
123\htmlClass{lc-mute}{\mathclap{1}\hspace{1.94em}\mathclap{2}\hspace{1.94em}\mathclap{3}}
123\htmlClass{lc-mute}{\mathclap{1}\hspace{1.94em}\mathclap{2}\hspace{1.94em}\mathclap{3}}
XX
YY
ZZ
AA
Z\htmlClass{lc-mute}{\cancel{Z}}
YY
XX
BB
X\htmlClass{lc-mute}{\cancel{X}}
ZZ
YY
CC
XX
BB
AA
CC
YY
AA
BB
CC
ZZ
CC
BB
AA
Text solution
Round 1 with hospitals proposingEach hospital proposes to the top student on its list (XX proposes to BB, YY proposes to AA, and ZZ proposes to CC), and because all three offers go to different students, no one is rejected and the algorithm stops immediately:
Hospitals propose:A–Y,B–X,C–Z\displaystyle \begin{aligned} & \text{Hospitals propose:} \\ & \kept{A\text{–}Y},\quad \kept{B\text{–}X},\quad \kept{C\text{–}Z} \end{aligned}
Compare partner ranks between the two runsListing which choice (1st,2nd,3rd1\text{st}, 2\text{nd}, 3\text{rd}) each participant receives shows that every hospital does at least as well, and every student does at least as poorly, when hospitals propose:
Students propose (A–X,  B–Y,  C–Z):Students get (1st,2nd,2nd),Hospitals get (2nd,2nd,1st)Hospitals propose (A–Y,  B–X,  C–Z):Students get (2nd,3rd,2nd),Hospitals get (1st,1st,1st)\displaystyle \begin{aligned} & \text{Students propose } \\ & (\kept{A\text{–}X},\; \kept{B\text{–}Y},\; \kept{C\text{–}Z}): \\ & \quad \text{Students get } (1\text{st}, 2\text{nd}, 2\text{nd}), \\ & \quad \text{Hospitals get } (2\text{nd}, 2\text{nd}, 1\text{st}) \\ & \text{Hospitals propose } \\ & (\kept{A\text{–}Y},\; \kept{B\text{–}X},\; \kept{C\text{–}Z}): \\ & \quad \text{Students get } (\fails{2\text{nd}}, \fails{3\text{rd}}, 2\text{nd}), \\ & \quad \text{Hospitals get } (\kept{1\text{st}}, \kept{1\text{st}}, 1\text{st}) \end{aligned}

Because a receiver never rejects a proposer who is its partner in some stable matching, three general facts always hold:

Order-independent:Proposal order amongunmatched proposers neverchanges the final matchingProposer-optimal:Every proposer gets theirbest partner across allstable matchingsReceiver-pessimal:Every receiver gets theirworst partner across allstable matchings\displaystyle \begin{aligned} & \text{Order-independent:} \\ & \quad \text{Proposal order among} \\ & \quad \text{unmatched proposers never} \\ & \quad \text{changes the final matching} \\ & \text{Proposer-optimal:} \\ & \quad \text{Every proposer gets their} \\ & \quad \kept{\text{best partner}}\text{ across all} \\ & \quad \text{stable matchings} \\ & \text{Receiver-pessimal:} \\ & \quad \text{Every receiver gets their} \\ & \quad \fails{\text{worst partner}}\text{ across all} \\ & \quad \text{stable matchings} \end{aligned}

Key concepts

You do not need to run the Proposal Algorithm when a problem already gives you a matching and asks if it is stable: go student by student, skip anyone who already has their first choice (like AA with XX), and check only the hospitals ranked above each remaining student's current match. A pair is a rogue pair only when both sides rank each other above their assigned partners, such as (C,Z)\fails{(C, Z)} when C\fails{C} prefers Z\fails{Z} over Y\assigned{Y} and Z\fails{Z} prefers C\fails{C} over B\assigned{B}.

When you need to construct a stable matching, run the Proposal Algorithm: unmatched proposers propose to the highest remaining choice on their list, while receivers hold their best offer so far on "Maybe" and reject the rest (for example, XX holds A\maybe{A} and rejects CC, so CC crosses off X\fails{X} and proposes to ZZ). Whenever you need to prove why the algorithm terminates or why a rogue pair cannot form, use the two loop invariants: proposers move strictly down their lists (so no student proposes to the same hospital twice, giving total proposals≤N2\text{total proposals} \le \kept{N^2}) and receivers never become empty once offered and only trade up (so any hospital crossed off a student's list ends up with someone it prefers over that student). Finally, switching who proposes flips the advantage from one side to the other: the algorithm is always proposer-optimal and receiver-pessimal, so running it once with students proposing and once with hospitals proposing gives the best and worst stable partners every participant can possibly have.

4

Practice

1

Three students (A,B,CA, B, C) and three hospitals (X,Y,ZX, Y, Z) have the following preference lists: Students: A:Y,X,ZA: Y, X, Z; B:X,Y,ZB: X, Y, Z; C:X,Z,YC: X, Z, Y. Hospitals: X:A,B,CX: A, B, C; Y:B,C,AY: B, C, A; Z:C,A,BZ: C, A, B. Determine whether the matching AA with ZZ, BB with XX, and CC with YY is stable. If it is unstable, list every rogue pair.

Start with each student who did not get their first choice: which hospitals are ranked higher on their list than their assigned partner?

Student BB is matched with their first choice (X\assigned{X}), so BB forms no rogue pairs.
Student AA is matched with third choice Z\assigned{Z} and prefers YY and XX:
(A,Y):Y prefers its match C(2nd) over A (3rd)  ⟹  not a rogue pair(A,X):X prefers A (1st)over its match B (2nd)  ⟹  rogue pair (A,X)\displaystyle \begin{aligned} (A, Y) &: Y \text{ prefers its match } \assigned{C} \\ & \quad \text{(2nd) over } A \text{ (3rd)} \\ & \implies \text{not a rogue pair} \\ (A, X) &: X \text{ prefers } A \text{ (1st)} \\ & \quad \text{over its match } \assigned{B} \text{ (2nd)} \\ & \implies \text{rogue pair } \fails{(A, X)} \end{aligned}
(Watch out: a common slip is listing (A,Y)(A, Y) just because AA prefers YY, or stopping after finding (A,X)\fails{(A, X)} without checking Student CC.)
Student CC is matched with third choice Y\assigned{Y} and prefers XX and ZZ:
(C,X):X prefers its match B(2nd) over C (3rd)  ⟹  not a rogue pair(C,Z):Z prefers C (1st)over its match A (2nd)  ⟹  rogue pair (C,Z)\displaystyle \begin{aligned} (C, X) &: X \text{ prefers its match } \assigned{B} \\ & \quad \text{(2nd) over } C \text{ (3rd)} \\ & \implies \text{not a rogue pair} \\ (C, Z) &: Z \text{ prefers } C \text{ (1st)} \\ & \quad \text{over its match } \assigned{A} \text{ (2nd)} \\ & \implies \text{rogue pair } \fails{(C, Z)} \end{aligned}
The matching is unstable, with two rogue pairs: (A,X)\fails{(A, X)} and (C,Z)\fails{(C, Z)}.
2

Run the Proposal Algorithm with students proposing on the following preference lists: Students: A:X,Y,ZA: X, Y, Z; B:X,Y,ZB: X, Y, Z; C:Y,X,ZC: Y, X, Z. Hospitals: X:C,A,BX: C, A, B; Y:A,B,CY: A, B, C; Z:A,B,CZ: A, B, C. Give the final stable matching and the total number of proposals made before the algorithm stops.

After Round 1 leaves BB rejected by XX, trace one unmatched student's proposal at a time and remember that a hospital holding a student on "Maybe" will still trade up if a higher-ranked student proposes.

Proposals 1–3 (Round 1)AA and BB propose to XX (XX holds A\maybe{A} and rejects BB), and CC proposes to YY (YY holds C\maybe{C}). (Watch out: do not lock CC in with YY permanently. If you make BB skip YY and propose to ZZ, you get the wrong matching A–X,B–Z,C–YA\text{–}X, B\text{–}Z, C\text{–}Y in 44 proposals.)
Proposal 4Unmatched BB proposes to next choice YY; YY prefers BB over CC, so YY holds B\maybe{B} and rejects CC.
Proposal 5Unmatched CC proposes to next choice XX; XX prefers CC over AA, so XX holds C\maybe{C} and rejects AA.
Proposal 6Unmatched AA proposes to next choice YY; YY prefers AA over BB, so YY holds A\maybe{A} and rejects BB.
Proposal 7Unmatched BB proposes to next choice ZZ; empty ZZ holds B\maybe{B}, leaving everyone matched.
(Watch out: count every proposal made, not just the 44 rejections/cross-outs or the 55 rounds.)
Final stable matching: AA with YY, BB with ZZ, and CC with XX, in 7\kept{7} proposals.
3

Run the Proposal Algorithm with hospitals proposing on the preference lists from Problem 2. Using the proposer-optimal and receiver-pessimal properties of the algorithm, determine how many different stable matchings exist for these lists.

After finding the hospital-proposing matching, compare each student's best possible stable partner (from Problem 2) against their worst possible stable partner (from this run).

Round 1XX proposes to CC (CC holds X\maybe{X}), while YY and ZZ both propose to AA. Checking Student AA's list (X,Y,ZX, Y, Z), AA holds Y\maybe{Y} and rejects ZZ. (Watch out: use Student AA's preference list to choose between YY and ZZ, not the hospitals' lists.)
Round 2Unmatched ZZ proposes to second choice BB, and empty BB holds Z\maybe{Z}, giving AA with YY, BB with ZZ, and CC with XX.
Because the student-proposing matching gives every student their best partner in any stable matching and the hospital-proposing matching gives every student their worst partner in any stable matching, and both runs produce A–Y,  B–Z,  C–X\kept{A\text{–}Y,\; B\text{–}Z,\; C\text{–}X}, no student can have any other stable partner. Exactly 1\kept{1} stable matching exists.
4

In a matching problem with NN students and NN hospitals, suppose Student AA ranks Hospital XX first and Hospital XX ranks Student AA first. Prove that AA and XX must be matched to each other in every stable matching. Then suppose Student BB ranks XX first and Hospital YY second, while Hospital YY ranks AA first and Student BB second. Prove that BB and YY must also be matched to each other in every stable matching.

To prove a pair must be matched in every stable matching, assume for contradiction that some matching separates them and check whether they form a rogue pair.

Why AA and XX must be pairedSuppose in some matching that AA and XX are not matched together. (Watch out: merely tracing Round 1 of the student-proposing algorithm only shows AA and XX meet in that one run; using rogue pairs proves it for every stable matching at once.) Since XX is AA's first choice, AA is matched to a hospital ranked below XX and prefers XX. Since AA is XX's first choice, XX is matched to a student ranked below AA and prefers AA. Thus (A,X)\fails{(A, X)} is a rogue pair and the matching is unstable, so every stable matching must pair AA with XX.
Why BB and YY must be pairedBecause every stable matching pairs AA with XX, no stable matching can pair BB with XX or YY with AA. If a stable matching did not pair BB with YY, then BB would be matched to a hospital ranked below second choice YY (since first choice XX is taken by AA), and YY would be matched to a student ranked below second choice BB (since first choice AA is taken by XX). That would make (B,Y)\fails{(B, Y)} a rogue pair, so every stable matching must also pair BB with YY.
5

With NN students and NN hospitals, we saw that the Proposal Algorithm makes at most N2\kept{N^2} proposals (9\kept{9} when N=3N = 3). Use the two loop invariants of the algorithm to prove that the last hospital to receive an offer only ever receives 11 proposal, and therefore the total number of proposals can never exceed (N−1)N+1=N2−N+1(N - 1)N + 1 = \kept{N^2 - N + 1} (which is 7\kept{7} when N=3N = 3, as achieved in Problem 2).

By the receiver invariant, how many hospitals are holding a student at the exact moment the last empty hospital receives its very first proposal?

By the receiver invariant, once a hospital receives its first proposal, it holds a student from that step onward and is never empty again.
Because there are NN students and NN hospitals, at the exact step when the NN-th (last empty) hospital receives its first proposal, all NN hospitals are simultaneously holding a student. That leaves 00 students unmatched, so the algorithm stops immediately and that last hospital receives at most 11 proposal.
By the proposer invariant, no student proposes to the same hospital twice, so each of the other N−1N - 1 hospitals receives at most NN proposals (one from each of the NN students). (Watch out: do not limit every hospital to N−1N - 1 proposals, which would give N(N−1)=6N(N - 1) = 6 for N=3N = 3. The first N−1N - 1 hospitals can each receive offers from all NN students before the last rejected student finally proposes to the last empty hospital.)
Summing across all hospitals, the total number of proposals is at most (N−1)N+1=N2−N+1(N - 1)N + 1 = \kept{N^2 - N + 1}.