Gale–Shapley algorithm: Difference between revisions

Content deleted Content added
Split off from stable marriage problem (very little new material)
Tag: Removed redirect
Line 1:
In [[mathematics]], [[economics]], and [[computer science]], the '''Gale–Shapley algorithm''' is an [[algorithm]] for finding a solution to the [[stable matching problem]].
#REDIRECT [[Stable marriage problem]]
It takes [[polynomial time]], and the time is [[linear time|linear]] in the size of the input to the algorithm. Depending on how it is used, it can find either the solution that is optimal for the participants on one side of the matching, or for the participants on the other side. It is a [[truthful mechanism]] from the point of view of the participants for whom it provides the optimal solution.
 
==Background==
{{main|stable matching problem}}
The stable matching problem, in its most basic form, takes as input equal numbers of two types of participants ({{mvar|n}} men and {{mvar|n}} women, or {{mvar|n}} medical students and {{mvar|n}} internships, for example), and an ordering for each participant giving their preference for who to be matched to among the participants of the other type. A matching is ''not'' stable if:
 
{{Ordered list|list-style-type=numeric
|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
|''B'' also prefers ''A'' over the element to which ''B'' is already matched.
}}
 
In other words, a matching is stable when there does not exist any match (''A'', ''B'') which both prefer each other to their current partner under the matching.
A stable matching always exists, and the algorithmic problem solved by the Gale–Shapley algorithm is to find one.
 
==Solution==
[[File:Gale-Shapley.gif|thumb|right|Animation showing an example of the Gale–Shapley algorithm]]
In 1962, [[David Gale]] and [[Lloyd Shapley]] proved that, for any equal number of men and women, it is always possible to solve the SMP and make all marriages stable. They presented an [[algorithm]] to do so.<ref>{{cite journal |first=D. |last=Gale |first2=L. S. |last2=Shapley |title=College Admissions and the Stability of Marriage |journal=[[American Mathematical Monthly]] |volume=69 |issue= 1|pages=9–14 |year=1962 |jstor=2312726 |doi=10.2307/2312726|url=http://www.dtic.mil/get-tr-doc/pdf?AD=AD0251958 }}</ref><ref>[[Harry Mairson]]: "The Stable Marriage Problem", ''The Brandeis Review'' 12, 1992 ([http://www1.cs.columbia.edu/~evs/intro/stable/writeup.html online]).</ref>
 
The '''Gale–Shapley algorithm''' (also known as the '''Deferred Acceptance algorithm''') involves a number of "rounds" (or "[[iteration]]s"):
* In the first round, first ''a'') each unengaged man proposes to the woman he prefers most, and then ''b'') each woman replies "maybe" to her suitor she most prefers and "no" to all other suitors. She is then provisionally "engaged" to the suitor she most prefers so far, and that suitor is likewise provisionally engaged to her.
* In each subsequent round, first ''a'') each unengaged man proposes to the most-preferred woman to whom he has not yet proposed (regardless of whether the woman is already engaged), and then ''b'') each woman replies "maybe" if she is currently not engaged or if she prefers this man over her current provisional partner (in this case, she rejects her current provisional partner who becomes unengaged). The provisional nature of engagements preserves the right of an already-engaged woman to "trade up" (and, in the process, to "jilt" her until-then partner).
* This process is repeated until everyone is engaged.
 
The runtime complexity of this algorithm is <math>O(n^2)</math> where <math>n</math> is the number of men or women.<ref name="IwamaMiyazaki2008">{{cite journal|last1=Iwama|first1=Kazuo|author1-link=Kazuo Iwama (computer scientist)|last2=Miyazaki|first2=Shuichi|title=A Survey of the Stable Marriage Problem and Its Variants|year=2008|pages=131–136|doi=10.1109/ICKS.2008.7|journal=International Conference on Informatics Education and Research for Knowledge-Circulating Society (icks 2008)|isbn=978-0-7695-3128-1|hdl=2433/226940|url=https://repository.kulib.kyoto-u.ac.jp/dspace/bitstream/2433/226940/1/ICKS.2008.7.pdf}}</ref>
 
This algorithm guarantees that:
 
; Everyone gets married : At the end, there cannot be a man and a woman both unengaged, as he must have proposed to her at some point (since a man will eventually propose to everyone, if necessary) and, being proposed to, she would necessarily be engaged (to someone) thereafter.
; The marriages are stable : Let Alice and Bob both be engaged, but not to each other. Upon completion of the algorithm, it is not possible for both Alice and Bob to prefer each other over their current partners. If Bob prefers Alice to his current partner, he must have proposed to Alice before he proposed to his current partner. If Alice accepted his proposal, yet is not married to him at the end, she must have dumped him for someone she likes more, and therefore doesn't like Bob more than her current partner. If Alice rejected his proposal, she was already with someone she liked more than Bob.
 
==Algorithm==
 
'''function''' stableMatching {
Initialize all ''m'' ∈ M and ''w'' ∈ W to ''free''
'''while''' ∃ ''free'' man ''m'' who still has a woman w to propose to {
w = first woman on m's list to whom m has not yet proposed
'''if''' w is ''free''
(m, w) become ''engaged''
'''else''' some pair (m', w) already exists
'''if''' w prefers m to m'
m' becomes ''free''
(m, w) become ''engaged''
'''else'''
(m', w) remain ''engaged''
}
}
 
==Optimality of the solution==
[[File:DA-men-optimality.png|thumb|Proof that deferred acceptance is optimal for men]]
The existence of different stable matchings raises the question: which matching is returned by the Gale-Shapley algorithm? Is it the matching better for men, for women, or the intermediate one? In the above example, the algorithm converges in a single round on the men-optimal solution because each woman receives exactly one proposal, and therefore selects that proposal as her best choice, ensuring that each man has an accepted offer, ending the match.
 
This is a general fact: the Gale-Shapley algorithm in which men propose to women ''always'' yields a stable matching that is the ''best for all men'' among all stable matchings. Similarly, if the women propose then the resulting matching is the ''best for all women'' among all stable matchings.
 
To see this, consider the illustration at the right. Let A be the matching generated by the men-proposing algorithm, and B an alternative stable matching that is better for at least one man, say ''m''<sub>0</sub>. Suppose ''m''<sub>0</sub> is matched in B to a woman ''w''<sub>1</sub>, which he prefers to his match in A. This means that in A, ''m''<sub>0</sub> has visited ''w''<sub>1</sub>, but she rejected him (this is denoted by the green arrow from ''m''<sub>0</sub> to ''w''<sub>1</sub>). ''w''<sub>1</sub> rejected him in favor of some man that she prefers, say ''m''<sub>2</sub>. So in B, ''w''<sub>1</sub> is matched to ''m''<sub>0</sub> but "yearns" (wants but unmatched) to ''m''<sub>2</sub> (this is denoted by the red arrow from ''w''<sub>1</sub> to ''m''<sub>2</sub>).
 
Since B is a stable matching, ''m''<sub>2</sub> must be matched in B to some woman he prefers to ''w''<sub>1</sub>, say ''w''<sub>3</sub>. This means that in A, ''m''<sub>2</sub> has visited ''w''<sub>3</sub> before arriving at ''w''<sub>1</sub>, which means that ''w''<sub>3</sub> has rejected him. By similar considerations, and since the graph is finite, we must eventually have a directed cycle in which each man was rejected in A by the next woman in the cycle, who rejected him in favor of the next man in the cycle. But this is impossible since such "cycle of rejections" cannot start anywhere: suppose by contradiction that it starts at e.g. ''m''<sub>0</sub> - the first man rejected by his adjacent woman (''w''<sub>1</sub>). By assumption, this rejection happens only after ''m''<sub>2</sub> comes to ''w''<sub>1</sub>. But this can happen only after ''w''<sub>3</sub> rejects ''m''<sub>2</sub> - contradiction to ''m''<sub>0</sub> being the first.
 
== Strategic considerations ==
The GS algorithm is a [[truthful mechanism]] from the point of view of men (the proposing side). I.e, no man can get a better matching for himself by misrepresenting his preferences. Moreover, the GS algorithm is even ''group-strategy proof'' for men, i.e., no coalition of men can coordinate a misrepresentation of their preferences such that all men in the coalition are strictly better-off.<ref>{{Cite journal|last=Dubins|first=L. E.|last2=Freedman|first2=D. A.|date=1981-08-01|title=Machiavelli and the Gale-Shapley Algorithm|journal=The American Mathematical Monthly|volume=88|issue=7|pages=485–494|doi=10.1080/00029890.1981.11995301|issn=0002-9890}}</ref> However, it is possible for some coalition to misrepresent their preferences such that some men are better-off and the other men retain the same partner.<ref>{{Cite journal|last=Huang|first=Chien-Chung|date=2006|editor-last=Azar|editor-first=Yossi|editor2-last=Erlebach|editor2-first=Thomas|title=Cheating by Men in the Gale-Shapley Stable Matching Algorithm|journal=Algorithms – ESA 2006|volume=4168|series=Lecture Notes in Computer Science|language=en|publisher=Springer Berlin Heidelberg|pages=418–431|doi=10.1007/11841036_39|isbn=9783540388760}}</ref>
 
The GS algorithm is non-truthful for the women (the reviewing side): each woman may be able to misrepresent her preferences and get a better match.
 
==Implementation in software packages==
 
*[[R (programming language)|R]]: The Gale–Shapley algorithm (also referred to as deferred-acceptance algorithm) for the stable marriage and the [[Hospital resident|hospitals/residents problem]] is available as part of the <code>matchingMarkets</code><ref>{{cite journal|last=Klein|first=T.|year=2015|title=Analysis of Stable Matchings in R: Package matchingMarkets|url=http://cran.at.r-project.org/web/packages/matchingMarkets/vignettes/matching.pdf|journal=Vignette to R Package MatchingMarkets}}</ref><ref>{{cite web|url=http://cran.at.r-project.org/web/packages/matchingMarkets/|title=matchingMarkets: Analysis of Stable Matchings|date=|work=R Project}}</ref> and <code>matchingR</code><ref>{{cite web|url=https://cran.r-project.org/web/packages/matchingR/|title=matchingR: Matching Algorithms in R and C++|date=|work=R Project}}</ref> packages.
*[[Application programming interface|API]]: The MatchingTools API provides a free application programming interface for the Gale–Shapley algorithm.<ref>{{cite web|url=https://matchingtools.com|title=MatchingTools API}}</ref>
*[[Python (programming language)|Python]]: The Gale–Shapley algorithm is included along with several others for generalized matching problems in the <code>QuantEcon/MatchingMarkets.py</code> package<ref>{{cite web|url=https://github.com/QuantEcon/MatchingMarkets.py|title=matchingMarkets.py|date=|work=Python package}}</ref>
*[[Matlab (programming language)|Matlab]]: The Gale–Shapley algorithm is implemented in the <code>assign2DStable</code> function that is part of the [[United States Naval Research Laboratory |United States Naval Research Laboratory's]] free Tracker Component Library.<ref>{{cite web|url=https://github.com/USNavalResearchLaboratory/TrackerComponentLibrary|title=Tracker Component Library|work=Matlab Repository|access-date=January 5, 2019}}</ref>
 
==References==
{{reflist}}
 
==External links==
*[http://www.sephlietz.com/gale-shapley/ Gale–Shapley JavaScript Demonstration]
 
[[Category:Stable matching]]
[[Category:Lloyd Shapley]]
[[Category:Algorithms]]