Gale–Shapley algorithm


In mathematics, economics, and computer science, the Gale–Shapley algorithm is an algorithm for finding a solution to the stable matching problem, named for David Gale and Lloyd Shapley.
It takes polynomial time, and the time is 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

The stable matching problem, in its most basic form, takes as input equal numbers of two types of participants, and an ordering for each participant giving their preference for whom to be matched to among the participants of the other type. A matching is not stable if:
In other words, a matching is stable when there does not exist any match 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

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.
The Gale–Shapley algorithm involves a number of "rounds" :
The runtime complexity of this algorithm is where is the number of men or women.
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 and, being proposed to, she would necessarily be engaged 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

algorithm stable_matching is
Initialize all m ∈ M and w ∈ W to free
whilefree man m who still has a woman w to propose to do
w := first woman on m's list to whom m has not yet proposed
if w is free then
become engaged
else some pair already exists
if w prefers m to m' then
m' becomes free
become engaged
else
remain engaged
end if
end if
repeat

Optimality of the solution

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. These two matchings are the top and bottom elements of a larger structure on all stable matchings, the lattice of 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 m0. Suppose m0 is matched in B to a woman w1, which he prefers to his match in A. This means that in A, m0 has visited w1, but she rejected him. w1 rejected him in favor of some man that she prefers, say m2. So in B, w1 is matched to m0 but "yearns" to m2.
Since B is a stable matching, m2 must be matched in B to some woman he prefers to w1, say w3. This means that in A, m2 has visited w3 before arriving at w1, which means that w3 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. m0 - the first man rejected by his adjacent woman. By assumption, this rejection happens only after m2 comes to w1. But this can happen only after w3 rejects m2 - contradiction to m0 being the first.

Strategic considerations

The GS algorithm is a truthful mechanism from the point of view of men. 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. 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.
The GS algorithm is non-truthful for the women : each woman may be able to misrepresent her preferences and get a better match.

Implementation in software packages