Content deleted Content added
→Solution: Clarify for editors like the previous IP who miss the point |
condense |
||
Line 1:
In [[mathematics]], [[economics]], and [[computer science]], the '''Gale–Shapley algorithm''' (also known as the '''deferred acceptance algorithm''' or '''propose-and-reject algorithm''') is an [[algorithm]] for finding a solution to the [[stable matching problem]], named for [[David Gale]] and [[Lloyd Shapley]] who had described it as solving both the ''college admission problem'' and the ''stable marriage problem''.
It takes [[polynomial time]], and the time is [[linear time|linear]] in the size of the input to the algorithm
==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 whom to be matched to among the participants of the other type. A stable matching always exists, and the algorithmic problem solved by the Gale–Shapley algorithm is to find one. A matching is ''not'' stable if:
{{Ordered list|list-style-type=numeric
Line 11:
}}
In other words, a matching is stable when there
==Solution==
[[File:Gale-Shapley.gif|thumb|right|Animation showing an example of the Gale–Shapley algorithm]]
In 1962,
The '''Gale–Shapley 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).
* This process is repeated until everyone is engaged.
Line 31 ⟶ 30:
==Algorithm==
'''algorithm''' stable_matching '''is'''
Initialize
'''while''' ∃ ''free'' man ''m'' who
w := first woman on m's list to whom m has not yet proposed
'''if''' w is ''free'' '''then'''
(m, w) become ''engaged''
'''else''' '''if''' ∃ some pair (m', w)
'''if''' w prefers m to m' '''then'''
m' becomes ''free''
Line 51 ⟶ 49:
{{main|Lattice of stable matchings}}
[[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
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. These two matchings are the top and bottom elements of
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>.
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,
== Strategic considerations ==
The GS algorithm is a
| last1 = Dubins | first1 = L. E. | author1-link = Lester Dubins
| last2 = Freedman | first2 = D. A. | author2-link = David A. Freedman
Line 87 ⟶ 85:
==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>
|