Stable Matching
Get to know the stable matching problem and its solution using greedy algorithms.
We'll cover the following
Every year, thousands of new doctors must obtain internships at hospitals around the United States. During the first half of the 20th century, competition among hospitals for the best doctors led to earlier and earlier offers of internships, sometimes as early as the second year of medical school, along with tighter deadlines for acceptance. In the 1940s, medical schools agreed not to release information until a common date during their students’ fourth year. In response, hospitals began demanding faster decisions. By 1950, hospitals would regularly call doctors, offer them internships, and demand immediate responses. Interns were forced to gamble if their third-choice hospital called first—accept and risk losing a better opportunity later, or reject and risk having no position at all.
Finally, a central clearing house for internship assignments, now called the National Resident Matching Program (NRMP), was established in the early 1950s. Each year, doctors submit a ranked list of all hospitals where they would accept an internship, and each hospital submits a ranked list of doctors they would accept as interns. The NRMP then computes a matching between doctors and hospitals that satisfies the following stability requirement. A matching is unstable if there is a doctor α and hospital B that would be both happier with each other than with their current match; that is,
- is matched with some other hospital , even though she prefers .
- B is matched with some other doctor , even though they prefer .
In this case, we call an unstable pair for the matching. The goal of the Resident Match is a stable matching, which matches with no unstable pairs.
For simplicity, let’s assume from now on that there is exactly the same number of doctors and hospitals; each hospital offers exactly one internship; each doctor ranks all hospitals and vice versa; and finally, there are no ties in the doctors’ or hospitals’ rankings.
Some bad ideas
At first glance, it is not even clear that stable matching always exists! Certainly, not every matching of doctors and hospitals is stable. Suppose there are three doctors (Dr. Quincy, Dr. Rotwang, Dr. Shephard, represented by lower-case letters) and three hospitals (Arkham Asylum, Bethlem Royal Hospital, and County General Hospital, represented by upper-case letters) who rank each other as follows:
The matching is unstable because Arkham would rather hire Dr. Rotwang than Dr. Quincy, and Dr. Rotwang would rather work at Arkham than at Bedlam. is an unstable pair for this matching.
One might imagine using an incremental algorithm that starts with an arbitrary matching and then greedily performs exchanges to resolve instabilities. Unfortunately, resolving one instability can create new ones; in fact, this incremental “improvement” can lead to an infinite loop. For example, if we start with our earlier unstable matching , each of the following exchanges resolves one unstable pair (indicated over the arrow), but the sequence of exchanges leads back to the original matching:
Alternatively, we might try the following multi-round greedy protocol. In each round, every unmatched hospital makes an offer to their favorite unmatched doctor, then every unmatched doctor with an offer accepts their favorite offer. It’s not hard to prove that at least one new doctor-hospital pair is matched in each round, so the algorithm always ends with a matching. For the previous example input, we already have a stable matching at the end of the first round! But consider the following input instead:
In the first round, Dr. Shephard accepts an offer from County, and Dr. Quincy accepts an offer from Bedlam (rejecting Arkham’s offer), leaving only Dr. Rotwang and Arkham unmatched. Thus, the protocol ends with the matching after two rounds. Unfortunately, this match is unstable; Arkham and Dr. Shephard prefer each other to their matches.
The Boston Pool and Gale-Shapley algorithms
In 1952, the NRMP adopted the “Boston Pool” algorithm to assign interns, so named because it had been previously used by a regional clearing house in the Boston area. Ten years later, David Gale and Lloyd Shapley described and formally analyzed a generalization of the Boston Pool algorithm and proved that it computes a stable matching. Gale and Shapley used the metaphor of college admissions. Essentially the same algorithm was independently developed by Elliott Peranson in 1972 for use in medical school admissions. Similar algorithms have since been adopted for many other matching markets, including faculty hiring in France, hiring of new economics PhDs in the United States, university admission in Germany, public school admission in New York and Boston, billet assignments for US Navy sailors, and kidney-matching programs.
Shapley was awarded the 2012 Nobel Prize in Economics for his research on stable matchings, together with Alvin Roth, who significantly extended Shapley’s work and used it to develop several real-world exchanges. (Gale did not share the prize because he died in 2008.)
Like our last failed greedy algorithm, the Gale-Shapley algorithm proceeds in rounds until every position has been accepted. Each round has two stages:
-
An arbitrarily unmatched hospital offers its position to the best doctor (according to ’s preference list) who has not already rejected it.
-
If is unmatched, she (tentatively) accepts ’s offer. If already has a match but prefers , she rejects her current match and (tentatively) accepts the new offer from . Otherwise, rejects the new offer.
All the doctors ultimately accepts the best offer that they receive, according to their preference list. In short, hospitals make offers greedily, and doctors accept offers greedily. The doctors’ ability to reject their current matches in favor of better offers is the key to making this mutually greedy strategy work.
For example, suppose that there are four doctors (Dr. Quincy, Dr. Rotwang, Dr. Shephard, and Dr. Tam) and four hospitals (Arkham Asylum, Bethlem Royal Hospital, County General Hospital, and The Dharma Initiative) who rank each other as follows:
Given these preference lists as input, the Gale-Shapley algorithm might proceed as follows:
-
Arkham makes an offer to Dr. Tam.
-
Bedlam makes an offer to Dr. Rotwang.
-
County makes an offer to Dr. Tam, who rejects her earlier offer from Arkham.
-
Dharma makes an offer to Dr. Shephard. (From this point on, there is only one unmatched hospital, so the algorithm has no more choices.)
-
Arkham makes an offer to Dr. Shephard, who rejects his earlier offer from Dharma.
-
Dharma makes an offer to Dr. Rotwang, who rejects her earlier offer from Bedlam.
-
Bedlam makes an offer to Dr. Tam, who rejects her earlier offer from County.
-
County makes an offer to Dr. Rotwang, who rejects it.
-
County makes an offer to Dr. Shephard, who rejects it.
-
County makes an offer to Dr. Quincy.
After the tenth round, all pending offers are accepted, and the algorithm returns the matching . We can (and should) verify by brute force that this matching is stable, even though no doctor was hired by their favorite hospital, and no hospital hired their favorite doctor; in fact, County ended up hiring their least favorite doctor. This is not the only stable matching for these preference lists; the matching is also stable.
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy