stable-matching-1
stable-matching-1
Stable matching
Elias Koutsoupias
Oxford, Hilary Term 2025
Stable matching [KT 1.1]
1
Matching med-school students to hospitals – overview
2
Stable matching problem: input
3
Example : input
n=3
Hospitals = {A, B, C}
Students = {X, Y, Z}
Preferences of hospitals (left) and students (right)
1 2 3 1 2 3
A X Y Z X B A C
B Y X Z Y A B C
C X Y Z Z A B C
4
Perfect matching
Definition
A matching M is a set of ordered pairs h − s with h ∈ H and s ∈ S,
such that
5
Example : perfect matching
n=3
Hospitals = {A, B, C}
Students = {X, Y, Z}
Preferences of hospitals (left) and students (right)
1 2 3 1 2 3
A X Y Z X B A C
B Y X Z Y A B C
C X Y Z Z A B C
6
Unstable pair
Definition
Given a perfect matching M, hospital h and student s form an
unstable pair if both:
7
Example : unstable pair
n=3
Hospitals = {A, B, C}
Students = {X, Y, Z}
Preferences of hospitals (left) and students (right)
1 2 3 1 2 3
A X Y Z X B A C
B Y X Z Y A B C
C X Y Z Z A B C
8
Stable matching problem
1 2 3 1 2 3
A X Y Z X B A C
B Y X Z Y A B C
C X Y Z Z A B C
1 2 3
A B C D
B C A D
C A D B
D C B A
1 2 3
A B C D
B C A D
C A B D
D A B C
A − B, C − D ⇒ B − C is unstable
A − C, B − D ⇒ A − B is unstable
A − D, B − C ⇒ A − C is unstable 11
Gale-Shapley deferred acceptance algorithm
Gale-Shapley(lists of preferences)
1 M=∅
2 while (some hospital h is unmatched and
hasn’t proposed to every student)
3 s = first student on h’s list to whom h has not yet proposed
4 if (s is unmatched)
5 Add h − s to matching M
6 else if (s prefers h to current partner h0 )
7 Replace h0 − s with h − s in matching M
8 else nothing (i.e., s rejects h)
9 return stable matching M.
12
Running time
Lemma
Algorithm terminates after at most n2 iterations of While loop.
Proof.
Each time through the While loop, a hospital proposes to a new
student. Thus, there are at most n2 possible proposals.
13
Examples with n(n − 1) + 1 steps
1 2 3 1 2 3
A X Y Z X B C A
B Y X Z Y C A B
C X Y Z Z A B C
1 2 3 4 1 2 3 4
A W X Y Z W B C D A
B X Y W Z X C D A B
C Y W X Z Y D A B C
D W X Y Z Z A B C D
14
Proof of correctness
Lemma
The Gale-Shapley algorithm finds a perfect matching.
Proof.
• Suppose, for sake of contradiction, that some hospital h is
unmatched upon termination of the Gale–Shapley algorithm
• Then some student, say s, is unmatched upon termination
• So s was never proposed to, because once proposed it becomes
matched and remains matched thereafter
• But, h proposes to every student, since h ends up unmatched
15
Proof of correctness
Lemma
The matching M returned by the Gale-Shapley algorithm is stable.
Proof.
Consider any pair hs that is not in M. We show that it is not unstable.
h never proposed to s: Therefore, h prefers its student in M to s.
h proposed to s: Therefore s rejected h at some point, which means
that s ended up with a more preferred hospital.
16
Multiple stable matchings
1 2 3 1 2 3
A X Y Z X B A C
B Y X Z Y A B C
C X Y Z Z A B C
1 2 3 1 2 3
A X Y Z X B A C
B Y X Z Y A B C
C X Y Z Z A B C
17
Valid partners
1 2 3 1 2 3
A X Y Z X B A C
B Y X Z Y A B C
C X Y Z Z A B C
18
Which stable matching?
• Is it a perfect matching?
• Is it stable?
Lemma
The Gale-Shapley algorithm returns the hospital-optimal assignment.
As a corollary, we get that the hospital-optimal assignment is stable.
19
Which stable matching?
20
Thuthfulness
21