Given a set of n number of males and a set of n number of females, have each group rate each member in ascending order of preference.
The algorithm terminates when a stable-perfect matching is found.
A unstable perfect matching is when women w is paired with man m, but there exists a situation where man m' prefers w, and w prefers m' back.
i.e. there exists a situation where the engagements should mutually change.
A stable perfect matching is when all men and women are paired in such a way that there exist no situation where man m and women w prefer each other and all are engaged.
A day is considered to be 1 full iteration in the algorithm. That is, every day every male proposes to his best preference. The first pseudo implementation does not have a clear concept of a 'day', but the second one does.
English-Code :: Gale-Shapely Algorithm:
Have each man propose the his first preference.
Have each women select her most preferred option from the from the current set of proposals, breaking a current engagement if necessary.
Note: If all man have unique women preferences (some permutation of the women), then everyone is engaged on day 1, although it may not necessarily be stable.
Continue until stable-perfect matching
Observations
Once matched a female does not become unmatched. She only trades up.
Once a male is matched, he can potentially become unmatched, and when he is unmatched, he can only trade down.
Pseudo-code
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
}
}
Gale Shapely Algorithm
while (male_n is unmatched) {
Have male_n prepose to his first preference female_m
If female_n is unmatched, or prefers male_n over her current partner, then match female_m with male_n.
if male_n is rejected (continue - and have male_n propose to female_m+1)
if male_n is accepted, move on to the next male_n+1 in list.
continue until every male_n is matched
}
Alternatively:
while (!every is matched) {
Have male_n propose to his best preference female_m
If female_m is unmatched, or prefers male_n over her current partner, then match female_m with male_n.
if male_n is rejected, continue to m_n+1 (do nothing)
continue to day_n+1 where every unmatched male proposes to his next best preference.
}
Males and Females
abe, bob, col, dan, ed, fred, gav, hal, ian, jon
abi, bea, cath, dee, eve, fay, gay, hope, ivy, jan
Rankings
abe: abi, eve, cath, ivy, jan, dee, fay, bea, hope, gay
bob: cath, hope, abi, dee, eve, fay, bea, jan, ivy, gay
col: hope, eve, abi, dee, bea, fay, ivy, gay, cath, jan
dan: ivy, fay, dee, gay, hope, eve, jan, bea, cath, abi
ed: jan, dee, bea, cath, fay, eve, abi, ivy, hope, gay
fred: bea, abi, dee, gay, eve, ivy, cath, jan, hope, fay
gav: gay, eve, ivy, bea, cath, abi, dee, hope, jan, fay
hal: abi, eve, hope, fay, ivy, cath, jan, bea, gay, dee
ian: hope, cath, dee, gay, bea, abi, fay, ivy, jan, eve
jon: abi, fay, jan, gay, eve, bea, dee, cath, ivy, hope
abi: bob, fred, jon, gav, ian, abe, dan, ed, col, hal
bea: bob, abe, col, fred, gav, dan, ian, ed, jon, hal
cath: fred, bob, ed, gav, hal, col, ian, abe, dan, jon
dee: fred, jon, col, abe, ian, hal, gav, dan, bob, ed
eve: jon, hal, fred, dan, abe, gav, col, ed, ian, bob
fay: bob, abe, ed, ian, jon, dan, fred, gav, col, hal
gay: jon, gav, hal, fred, bob, abe, col, ed, dan, ian
hope: gav, jon, bob, abe, ian, dan, hal, ed, col, fred
ivy: ian, col, hal, gav, fred, bob, abe, ed, jon, dan
jan: ed, hal, gav, abe, bob, jon, col, ian, fred, dan