Goal: given a set of preferences among hospitals and med-school students, design a self-reinforcing admissions process
Assignment with no unstable pairs
Hospital \(h\) and student \(s\) form an unstable pair if both:
Input: a set of \(n\) hospitals \(H\) and a set of \(n\) students \(S\) (one student per hospital for now)
| 1st | 2nd | 3rd | 1st | 2nd | 3rd | |||
|---|---|---|---|---|---|---|---|---|
| Atlanta | Xavier | Yolanda | Zeus | Xavier | Boston | Atlanta | Chicago | |
| Boston | Yolanda | Xavier | Zeus | Yolanda | Atlanta | Boston | Chicago | |
| Chicago | Xavier | Yolanda | Zeus | Zeus | Atlanta | Boston | Chicago |
1st: most favorite; 3rd: least favorite
A matching \(M\) is a set of ordered pairs \(h\-s\) with \(h \in H\) and \(s \in S\) such that
A matching \(M\) is perfect if \(|M| = |H| = |S| = n\)
A perfect matching \(M = \{ A\-Z, B\-Y, C\-X \}\)
| 1st | 2nd | 3rd | 1st | 2nd | 3rd | |||
|---|---|---|---|---|---|---|---|---|
| Atlanta | Xavier | Yolanda | Zeus | Xavier | Boston | Atlanta | Chicago | |
| Boston | Yolanda | Xavier | Zeus | Yolanda | Atlanta | Boston | Chicago | |
| Chicago | Xavier | Yolanda | Zeus | Zeus | Atlanta | Boston | Chicago |
Given a perfect matching \(M\), hospital \(h\) and student \(s\) form an unstable pair if both:
Key point: An unstable pair \(h\-s\) could each improve by joint action
\(A\-Y\) is an unstable pair for matching \(M = \{ A\-Z, B\-Y, C\-X \}\)
| 1st | 2nd | 3rd | 1st | 2nd | 3rd | |||
|---|---|---|---|---|---|---|---|---|
| Atlanta | Xavier | Yolanda | Zeus | Xavier | Boston | Atlanta | Chicago | |
| Boston | Yolanda | Xavier | Zeus | Yolanda | Atlanta | Boston | Chicago | |
| Chicago | Xavier | Yolanda | Zeus | Zeus | Atlanta | Boston | Chicago |
Here, Atlanta and Yolanda could make a side deal that would benefit both of them, but would leave Zeus and Boston in a bad position
Which pair is unstable in matching \(M = \{ A\-X, B\-Z, C\-Y \}\)?
\(A\-Y\)
\(B\-X\)
\(B\-Z\)
None of the above
| 1st | 2nd | 3rd | 1st | 2nd | 3rd | |||
|---|---|---|---|---|---|---|---|---|
| Atlanta | Xavier | Yolanda | Zeus | Xavier | Boston | Atlanta | Chicago | |
| Boston | Yolanda | Xavier | Zeus | Yolanda | Atlanta | Boston | Chicago | |
| Chicago | Xavier | Yolanda | Zeus | Zeus | Atlanta | Boston | Chicago |
A stable matching is a perfect matching with no unstable pairs.
Stable matching problem: given the preference lists of \(n\) hospitals and \(n\) students, find a stable matching (if one exists).
\(A\-Y\) is an unstable pair for matching \(M = \{ A\-Z, B\-Y, C\-X \}\)
| 1st | 2nd | 3rd | 1st | 2nd | 3rd | |||
|---|---|---|---|---|---|---|---|---|
| Atlanta | Xavier | Yolanda | Zeus | Xavier | Boston | Atlanta | Chicago | |
| Boston | Yolanda | Xavier | Zeus | Yolanda | Atlanta | Boston | Chicago | |
| Chicago | Xavier | Yolanda | Zeus | Zeus | Atlanta | Boston | Chicago |
A stable matching \(M = \{ A\-X, B\-Y, C\-Z \}\)
| 1st | 2nd | 3rd | 1st | 2nd | 3rd | |||
|---|---|---|---|---|---|---|---|---|
| Atlanta | Xavier | Yolanda | Zeus | Xavier | Boston | Atlanta | Chicago | |
| Boston | Yolanda | Xavier | Zeus | Yolanda | Atlanta | Boston | Chicago | |
| Chicago | Xavier | Yolanda | Zeus | Zeus | Atlanta | Boston | Chicago |
Q. Do stable matchings always exist?
Stable roommate problem:
| 1st | 2nd | 3rd | |
|---|---|---|---|
| A | B | C | D |
| B | C | A | D |
| C | A | B | D |
| D | A | B | C |
Q. Do stable matchings always exist?
|
no perfect matching is stable
|
Note
Observation
Stable matchings need not exist
An intuitive method that guarantees to find a stable matching between \(n\) hospitals and \(n\) students
// Gale-Shapley (preference lists for hospitals and students)
Initialize M to empty matching
While some hospital h is unmatched and hasn't proposed to every student:
s <- first student on h's list to whom h has not yet proposed
If s is unmatched
Add h-s to matching M
Else If s prefers h to current partner h'
Replace h'-s with h-s in matching M
Else
s rejects h
Return stable matching M
| 1st | 2nd | 3rd | 1st | 2nd | 3rd | |||
|---|---|---|---|---|---|---|---|---|
| Atlanta | Xavier | Yolanda | Zeus | Xavier | Boston | Atlanta | Chicago | |
| Boston | Yolanda | Xavier | Zeus | Yolanda | Atlanta | Boston | Chicago | |
| Chicago | Xavier | Yolanda | Zeus | Zeus | Atlanta | Boston | Chicago |
Note
Observation 1
Hospitals propose to students in decreasing order of preference.
Note
Observation 2
Once a student is matched, the student never becomes unmatched; only "trades up"
Claim: Gale-Shapley terminates after at most \(n^2\) iterations of while loop.
Pf: Each time through the while loop, a hospital proposes to a new student. There are only \(n^2\) possible proposals. ∎
| 1st | 2nd | 3rd | 4th | 5th | 1st | 2nd | 3rd | 4th | 5th | |||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Atlanta | V | W | X | Y | Z | Val | B | C | D | E | A | |
| Boston | W | X | Y | V | Z | Wayne | C | D | E | A | B | |
| Chicago | X | Y | V | W | Z | Xavier | D | E | A | B | C | |
| Dallas | Y | V | W | X | Z | Yolanda | E | A | B | C | D | |
| Eugene | V | W | X | Y | Z | Zeus | A | B | C | D | E |
Proposals required: \(n(n-1)+1\)
Claim: Gale-Shapley produces a matching.
Pf:
Q. Do all hospitals and all students get matched?
Claim: In Gale-Shapley matching, all hospitals get matched.
Pf: (by contradiction)
Note
Observation 2
Once a student is matched, the student never becomes unmatched; only "trades up"
Claim: In Gale-Shapley matching, all students get matched.
Pf:
Claim: In Gale-Shapley matching \(M'\), there are no unstable pairs.
Pf: Suppose that \(M'\) does not contain the pair \(h\)-\(s\).
Stable matching problem: Given \(n\) hospitals and \(n\) students and their preferences, find a stable matching if one exists.

Theorem [Gale-Shapley 1962]: The Gale-Shapley algorithm guarantees to find a stable matching for any problem instance.
Q. How to implement Gale-Shapley algorithm efficiently?
Q. If multiple stable matchings, which one does Gale-Shapley find?
Do all executions of Gale-Shapley lead to the same stable matching?
No, because the algorithm is nondeterministic
No, because an instance can have several stable matchings
Yes, because each instance has a unique stable matching
Yes, even though an instance can have several stable matchings and the algorithm is nondeterministic
For a given problem instance, there may be several stable matchings.
| 1st | 2nd | 3rd | 1st | 2nd | 3rd | |||
|---|---|---|---|---|---|---|---|---|
| Atlanta | Xavier | Yolanda | Zeus | Xavier | Boston | Atlanta | Chicago | |
| Boston | Yolanda | Xavier | Zeus | Yolanda | Atlanta | Boston | Chicago | |
| Chicago | Xavier | Yolanda | Zeus | Zeus | Atlanta | Boston | Chicago | |
| 1st | 2nd | 3rd | 1st | 2nd | 3rd | |||
| Atlanta | Xavier | Yolanda | Zeus | Xavier | Boston | Atlanta | Chicago | |
| Boston | Yolanda | Xavier | Zeus | Yolanda | Atlanta | Boston | Chicago | |
| Chicago | Xavier | Yolanda | Zeus | Zeus | Atlanta | Boston | Chicago |
An instance with two stable matchings:
\[M = \{ A\-X, B\-Y, C\-Z \}, \quad M' = \{ A\-Y, B\-X, C\-Z \}\]
Student \(s\) is a valid partner for hospital \(h\) if there exists any stable matching in which \(h\) and \(s\) are matched.
Ex:
| 1st | 2nd | 3rd | 1st | 2nd | 3rd | |||
|---|---|---|---|---|---|---|---|---|
| Atlanta | Xavier | Yolanda | Zeus | Xavier | Boston | Atlanta | Chicago | |
| Boston | Yolanda | Xavier | Zeus | Yolanda | Atlanta | Boston | Chicago | |
| Chicago | Xavier | Yolanda | Zeus | Zeus | Atlanta | Boston | Chicago |
An instance with two stable matchings:
\[M = \{ A\-X, B\-Y, C\-Z \}, \quad M' = \{ A\-Y, B\-X, C\-Z \}\]
|
Who is the best valid partner for W in the following instance with stable matchings?
|
\(M_0 = \{ A\-W, B\-X, C\-Y, D\-Z \}\) \(M_1 = \{ A\-X, B\-W, C\-Y, D\-Z \}\) \(M_2 = \{ A\-X, B\-Y, C\-W, D\-Z \}\) \(M_3 = \{ A\-Z, B\-W, C\-Y, D\-X \}\) \(M_4 = \{ A\-Z, B\-Y, C\-W, D\-X \}\) \(M_5 = \{ A\-Y, B\-Z, C\-W, D\-X \}\) |
| 1st | 2nd | 3rd | 4th | 1st | 2nd | 3rd | 4th | |||
|---|---|---|---|---|---|---|---|---|---|---|
| A | Y | Z | X | W | W | D | A | B | C | |
| B | Z | Y | W | X | X | C | B | A | D | |
| C | W | Y | X | Z | Y | C | B | A | D | |
| D | X | Z | W | Y | Z | D | A | B | C |
Student \(s\) is a valid partner for hospital \(h\) if there exists any stable matching in which \(h\) and \(s\) are matched.
Hospital-optimal assignment: Each hospital receives best valid partner.
Claim: All executions of Gale-Shapley yield hospital-optimal assignment.
Corollary: Hospital-optimal assigment is a stable matching!
Claim: Gale-Shapley matching \(M\) is hospital-optimal.
Proof: (by contradiction)
Q. Does hospital-optimality come at the expense of the students?
Student-pessimal assignment: Each student receives worst valid partner.
Claim: Gale-Shapley finds student-pessimal stable matching \(M\)
Proof: (by contradiction)
Suppose each agent knows the preference lists of every other agent before the hospital propose-and-reject algorithm is executed. Which of the following is true?
No hospital can improve by falsifying its preference list
No student can improve by falsifying their preference list
Both A and B
Neither A nor B
Q. Can there be an incentive to misrepresent your preference list?
Fact: No, for any hospital; yes, for some students.
Example: Xavier😈 lies by swapping A ↔ C
| 1 | 2 | 3 | 1 | 2 | 3 | |||
|---|---|---|---|---|---|---|---|---|
| Atlanta | X | Y | Z | Xavier | B | A | C | |
| Boston | Y | X | Z | Yolanda | A | B | C | |
| Chicago | X | Y | Z | Zeus | A | B | C | |
| Atlanta | X | Y | Z | Xavier😈 | B | C | A | |
| Boston | Y | X | Z | Yolanda | A | B | C | |
| Chicago | X | Y | Z | Zeus | A | B | C |
Extension 1: some agents declare others as unacceptable (med-school student unwilling to work in Cleveland)
Extension 2: some hospitals have more than one position.
Extension 3: Unequal number of positions and students (more than 43k med-school students; only 31k positions)
Matching \(M\) is unstable if there is a hospital \(h\) and student \(s\) such that:
Matching \(M\) is unstable if there is a hospital \(h\) and student \(s\) such that:
Theorem: There exists a stable matching.
Pf: Straightforward generalization of Gale-Shapley algorithm.
National Resident Matching Program (NRMP)

Lloyd Shapley: Stable matching theory and Gale-Shapley algorithm.

Alvin Roth: Applied Gale-Shapley to matching med-school students with hospitals, students with schools, and organ donors with patients.
![]() |
![]() |
New York City High School Matching
| 8th Grader | ranks top-5 high schools |
| High School | ranks students (and limit) |
| Goal | match 90k students to 500 high school programs |

QuestBridge National College Match for Low-Income Students
| Student | Ranks colleges |
| College | Ranks students willing to admit (and limit) |
| Goal | Match students to colleges |


Content delivery networks: Distribute much of world's content on web
| User | Prefers web server that provides fast response time |
| Server | Prefers to serve users with low cost |
| Goal | Assign billions of users to servers, every 10 seconds |

Input: Set of jobs with start times and finish times.
Goal: Find maximum cardinatily (max count) subset of mutually compatible jobs (jobs don't overlap)
: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
Input: Set of jobs with start times, finish times, and weights.
Goal: Find maximum weight subset of mutually compatible jobs (jobs don't overlap)
: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
Problem: Given a bipartite graph \(G = (L \cup R, E)\), find a max cardinality matching.
A subset of edges \(M \subseteq E\) is a matching if each node appears in at most one edge in \(M\)

Problem: Given a graph \(G = (V, E)\), find a max cardinality independent set.
A subset \(S \subseteq V\) is independent if for every \((u,v) \in E\), either \(u \notin S\) or \(v \notin S\), or both.

Input: Graph with weight on each node
Game: Two competing players alternate in selecting nodes. Not allowed to select a node if any of its neighbors have been selected.
Goal: Select a maximum weight subset of nodes.

Variations on a theme: independent set
Interval Scheduling
Weighted Interval Scheduling
Bipartite Matching
Independent Set
Competitive Facility Location
how to implement Gale-Shapley efficiently (\(O(n^2)\))
// Gale-Shapley (preference lists for hospitals and students)
Initialize M to empty matching
While some hospital h is unmatched and hasn't proposed to every student:
s <- first student on h's list to whom h has not yet proposed
If s is unmatched
Add h-s to matching M
Else If s prefers h to current partner h'
Replace h'-s with h-s in matching M
Else
s rejects h
Return stable matching M
Representing hospitals and students:
Representing the matching:
student[h] and hospital[s].student[h] = s and hospitals[s] = hHospitals proposing:
Students rejecting/accepting:
| 1st | 2nd | 3rd | 4th | 5th | 6th | 7th | 8th | |
|---|---|---|---|---|---|---|---|---|
pref[]: |
8 | 3 | 7 | 1 | 4 | 5 | 6 | 2 |
for i = 1 to n
inverse[pref[i]] = i
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
|---|---|---|---|---|---|---|---|---|
inverse[] |
4th | 8th | 2nd | 5th | 6th | 7th | 3rd | 1st |
Student prefers hospital 3 to 6 since inverse[3] < inverse[6]