# Gale–Shapley algorithm

In mathematics, economics, and computer science, the Gale–Shapley algorithm (also known as the deferred acceptance algorithm,[1] propose-and-reject algorithm,[2] or Boston Pool algorithm[1]) is an algorithm for finding a solution to the stable matching problem. It is named for David Gale and Lloyd Shapley, who published it in 1962, although it had been used for the National Resident Matching Program since the early 1950s. Shapley and Alvin E. Roth (who pointed out its prior application) won the 2012 Nobel Prize in Economics for work including this algorithm.

The stable matching problem seeks to pair up equal numbers of participants of two types, using preferences from each participant. The pairing must be stable: no pair of participants should prefer each other to their assigned match. In each round of the Gale–Shapley algorithm, unmatched participants of one type propose a match to the next participant on their preference list. Each proposal is accepted if its recipient prefers it to their current match. The resulting procedure is a truthful mechanism from the point of view of the proposing participants, who receive their most-preferred pairing consistent with stability. In contrast, the recipients of proposals receive their least-preferred pairing. The algorithm can be implemented to run in time quadratic in the number of participants, and linear in the size of the input to the algorithm.

The stable matching problem, and the Gale–Shapley algorithm solving it, have widespread real-world applications, including matching American medical students to residencies and French university applicants to schools. For more, see Stable marriage problem § Applications.

## Background

The stable matching problem, in its most basic form, takes as input equal numbers of two types of participants (n job applicants and n employers, for example), and an ordering for each participant giving their preference for whom to be matched to among the participants of the other type. A matching pairs each participant of one type with a participant of the other type. A matching is not stable if:

1. There is an element A of the first matched set which prefers some given element B of the second matched set over the element to which A is already matched, and
2. B also prefers A over the element to which B is already matched.

In other words, a matching is stable when there is no pair (A, B) where both participants prefer each other to their matched partners. If such a pair exists, the matching is not stable, in the sense that the members of this pair would prefer to leave the system and be matched to each other, possibly leaving other participants unmatched. A stable matching always exists, and the algorithmic problem solved by the Gale–Shapley algorithm is to find one.[3]

The stable matching problem has also been called the stable marriage problem, using a metaphor of marriage between men and women, and many sources describe the Gale–Shapley algorithm in terms of marriage proposals. However, this metaphor has been criticized as both sexist and unrealistic: the steps of the algorithm do not accurately reflect typical or even stereotypical human behavior.[4][5]

## Solution

In 1962, David Gale and Lloyd Shapley proved that, for any equal number of participants of each type, it is always possible to find a matching in which all pairs are stable. They presented an algorithm to do so.[6][7] In 1984, Alvin E. Roth observed that essentially the same algorithm had already been in practical use since the early 1950s, as the "Boston Pool algorithm" used by the National Resident Matching Program.[1][8]

The Gale–Shapley algorithm involves a number of "rounds" (or "iterations"). In terms of job applicants and employers, it can be expressed as follows:[9]

• In each round, one or more employers with open job positions each make a job offer to the applicant they prefer, among the ones they have not yet already made an offer to.
• Each applicant who has received an offer evaluates it against their current position (if they have one). If the applicant is not yet employed, or if they receive an offer from an employer they like better than their current employer, they accept the best new offer and become matched to the new employer (possibly leaving a previous employer with an open position). Otherwise, they reject the new offer.
• This process is repeated until all employers have either filled their positions or exhausted their lists of applicants.

### Implementation details and time analysis

To implement the algorithm efficiently, each employer needs to be able to find its next applicant quickly, and each applicant needs to be able to compare employers quickly. One way to do this is to number each applicant and each employer from 1 to ${\displaystyle n}$, where ${\displaystyle n}$ is the number of employers and applicants, and to store the following data structures:[10]

• A set of employers with unfilled positions
• A one-dimensional array indexed by employers, specifying the preference index of the next applicant to whom the employer would send an offer, initially 1 for each employer
• A two-dimensional array indexed by an employer and a number ${\displaystyle i}$ from 1 to ${\displaystyle n}$, naming the applicant who is each employer's ${\displaystyle i}$th preference
• A one-dimensional array indexed by applicants, specifying their current employer, initially a sentinel value such as 0 indicating they are unemployed
• A two-dimensional array indexed by an applicant and an employer, specifying the position of that employer in the applicant's preference list

Setting up these data structures takes ${\displaystyle O(n^{2})}$ time. With these structures it is possible to find an employer with an unfilled position, make an offer from that employer to their next applicant, determine whether the offer is accepted, and update all of the data structures to reflect the results of these steps, in constant time per offer. Once the algorithm terminates, the resulting matching can be read off from the array of employers for each applicant. There can be ${\displaystyle O(n^{2})}$ offers before each employer runs out of offers to make, so the total time is ${\displaystyle O(n^{2})}$.[10]

Although this time bound is quadratic in the number of participants, it may be considered as linear time when measured in terms of the size of the input, two matrices of preferences of size ${\displaystyle O(n^{2})}$.[11]

### Correctness guarantees

This algorithm guarantees that:

Everyone gets matched
At the end, there cannot be an applicant and employer both unmatched. An employer left unmatched at the end of the process must have made an offer to all applicants. But an applicant who receives an offer remains employed for the rest of the process, so there can be no unemployed applicants. Since the numbers of applicants and job openings are equal, there can also be no open positions remaining.[9]
The matches are stable
No applicant X and employer Y can prefer each other over their final match. If Y makes an offer to X, then X would only reject Y after receiving an even better offer, so X cannot prefer Y to their final match. And if Y stops making offers before reaching X in their preference list, Y cannot prefer X to their final match. In either case, X and Y do not form an unstable pair.[9]

## Optimality of the solution

There may be many stable matchings for the same system of preferences. This raises the question: which matching is returned by the Gale–Shapley algorithm? Is it the matching better for applicants, for employers, or the intermediate one? As it turns out, the Gale–Shapley algorithm in which employers make offers to applicants always yields the same stable matching (regardless of the order in which job offers are made), and its choice is the stable matching that is the best for all employers and worst for all applicants among all stable matchings.[9]

In a reversed form of the algorithm, each round consists of unemployed applicants writing a single job application to their preferred employer, and the employer either accepting the application (possibly firing an existing employee to do so) or rejecting it. This produces a matching that is best for all applicants and worst for all employers among all stable matchings. These two matchings are the top and bottom elements of the lattice of stable matchings.[12]

In both forms of the algorithm, one group of participants proposes matches, and the other group decides whether to accept or reject each proposal. The matching is always best for the group that makes the propositions, and worst for the group that decides how to handle each proposal.[12]

## Strategic considerations

The Gale–Shapley algorithm is a truthful mechanism from the point of view of the proposing side. This means that no proposer can get a better matching by misrepresenting their preferences. Moreover, the Gale–Shapley algorithm is even group-strategy proof for proposers, i.e., no coalition of proposers can coordinate a misrepresentation of their preferences such that all proposers in the coalition are strictly better-off.[13] However, it is possible for some coalition to misrepresent their preferences such that some proposers are better-off, and the others retain the same partner.[14]

The Gale–Shapley algorithm is non-truthful for the non-proposing participants. Each may be able to misrepresent their preferences and get a better match.[15] A particular form of manipulation is truncation: presenting only the topmost alternatives, implying that the bottom alternatives are not acceptable at all. Under complete information, it is sufficient to consider misrepresentations of the form of truncation strategies. However, successful misrepresentation requires knowledge of the other agents' preferences; without such knowledge, misrepresentation can give an agent a worse assignment. Moreover, even after an agent sees the final matching, they cannot deduce a strategy that would guarantee a better outcome in hindsight. This makes the Gale–Shapley algorithm a regret-free truth-telling mechanism. Moreover, in the Gale–Shapley algorithm, truth-telling is the only strategy that guarantees no regret. The Gale–Shapley algorithm is the only regret-free mechanism in the class of quantile-stable matching mechanisms.[16]

## Generalizations

In their original work on the problem, Gale and Shapley considered a more general form of the stable matching problem, suitable for university and college admission. In this problem, each university or college may have its own quota, a target number of students to admit, and the number of students applying for admission may differ from the sum of the quotas, necessarily causing either some students to remain unmatched or some quotas to remain unfilled. Additionally, preference lists may be incomplete: if a university omits a student from their list, it means they would prefer to leave their quota unfilled than to admit that student, and if a student omits a university from their list, it means they would prefer to remain unadmitted than to go to that university. Nevertheless, it is possible to define stable matchings for this more general problem, to prove that stable matchings always exist, and to apply the same algorithm to find one.[6]

A form of the Gale–Shapley algorithm, performed through a real-world protocol rather than calculated on computers, has been used for coordinating higher education admissions in France since 2018, through the Parcoursup system. In this process, over the course of the summer before the start of school, applicants receive offers of admission, and must choose in each round of the process whether to accept any new offer (and if so turn down any previous offer that they accepted). The method is complicated by additional constraints that make the problem it solves not exactly the stable matching problem. It has the advantage that the students do not need to commit to their preferences at the start of the process, but rather can determine their own preferences as the algorithm progresses, on the basis of head-to-head comparisons between offers that they have received. It is important that this process performs a small number of rounds of proposals, so that it terminates before the start date of the schools, but although high numbers of rounds can occur in theory, they tend not to occur in practice.[17] It has been shown theoretically that, if the Gale–Shapley algorithm needs to be terminated early, after a small number of rounds in which every vacant position makes a new offer, it nevertheless produces matchings that have a high ratio of matched participants to unstable pairs.[18]

## Recognition

Shapley and Roth were awarded the 2012 Nobel Memorial Prize in Economic Sciences "for the theory of stable allocations and the practice of market design". Gale had died in 2008, making him ineligible for the prize.[19]

## References

1. ^ a b c Roth, Alvin E. (February 2003). "The origins, history, and design of the resident match". JAMA. 289 (7): 909–912. doi:10.1001/jama.289.7.909.
2. ^ Carter, Michael W.; Price, Camille C. (2000). Operations Research: A Practical Introduction. CRC Press. p. 102. ISBN 9780849322563.
3. ^ Gusfield, Dan; Irving, Robert W. (1989). The Stable Marriage Problem: Structure and Algorithms. MIT Press. p. 6. ISBN 9780262515528.
4. ^ Wagner, Roy (April 2009). "Mathematical marriages: Intercourse between mathematics and semiotic choice". Social Studies of Science. 39 (2): 289–308. doi:10.1177/0306312708099443. hdl:20.500.11850/121579.
5. ^ Giagkousi, Kyriaki (March 2021). Gender and Computing Algorithms: The case of Stable Matching (PDF) (Master's thesis). National and Kapodistrian University of Athens, Department of History and Philosophy of Science and Department of Informatics and Telecommunications. Retrieved 2023-12-20.
6. ^ a b Gale, D.; Shapley, L. S. (1962). "College admissions and the stability of marriage". The American Mathematical Monthly. 69 (1): 9–14. doi:10.2307/2312726. JSTOR 2312726. Archived from the original on 2017-09-25. Retrieved 2019-11-20.
7. ^ Mairson, Harry (1992). "The stable marriage problem". The Brandeis Review. 12.
8. ^ Bergstrom, Theodore C. (June 1992). "Review of Two-Sided Matching: A Study in Game-Theoretic Modeling and Analysis by A. E. Roth and M. A. O. Sotomayor". Journal of Economic Literature. 30 (2): 896–898. JSTOR 2727713.
9. ^ a b c d Erickson, Jeff (June 2019). "4.5 Stable matching" (PDF). Algorithms. University of Illinois. pp. 170–176. Retrieved 2023-12-19.
10. ^ a b Kleinberg, Jon; Tardos, Éva (2006). "2.3 Implementing the stable matching algorithm using lists and arrays". Algorithm Design. Addison-Wesley. pp. 42–47.
11. ^ Gusfield & Irving (1989), p. 182.
12. ^ a b Knuth, Donald E. (1976). Mariages stables et leurs relations avec d'autres problèmes combinatoires (PDF) (in French). Montréal, Quebec: Les Presses de l'Université de Montréal. ISBN 0-8405-0342-3. MR 0488980. See in particular Problem 6, pp. 87–94.
13. ^ Dubins, L. E.; Freedman, D. A. (1981). "Machiavelli and the Gale–Shapley algorithm". The American Mathematical Monthly. 88 (7): 485–494. doi:10.2307/2321753. JSTOR 2321753. MR 0628016.
14. ^ Huang, Chien-Chung (2006). "Cheating by men in the Gale-Shapley stable matching algorithm". In Azar, Yossi; Erlebach, Thomas (eds.). Algorithms – ESA 2006, 14th Annual European Symposium, Zurich, Switzerland, September 11–13, 2006, Proceedings. Lecture Notes in Computer Science. Vol. 4168. Springer. pp. 418–431. doi:10.1007/11841036_39. MR 2347162.
15. ^ Gonczarowski, Yannai A.; Friedgut, Ehud (April 2013). "Sisterhood in the Gale–Shapley matching algorithm". Electronic Journal of Combinatorics. 20 (2): P12:1–P12:18. arXiv:1104.2217. doi:10.37236/3267.
16. ^ Fernandez, Marcelo Ariel (July 31, 2020). "Deferred acceptance and regret-free truth-telling" (Working paper). Johns Hopkins University Department of Economics.
17. ^ Mathieu, Claire (2018). "College admission algorithms in the real world" (Invited lecture at the European Symposium of Algorithms). Aalto University.
18. ^ Floréen, Patrik; Kaski, Petteri; Polishchuk, Valentin; Suomela, Jukka (August 2009). "Almost stable matchings by truncating the Gale–Shapley algorithm". Algorithmica. 58 (1): 102–118. arXiv:0812.4893. doi:10.1007/s00453-009-9353-9.
19. ^ Bhattacharjee, Yudhijit (October 15, 2012). "Economics Nobel honors matchmaking finesse". Science. 338 (6105): 314. doi:10.1126/science.338.6105.314. PMID 23087221.