CS 401: Computer Algorithms I
Stable Matching / Representative Problems
Xiaorui Sun
1
CS 401: Computer Algorithms I Stable Matching / Representative - - PowerPoint PPT Presentation
CS 401: Computer Algorithms I Stable Matching / Representative Problems Xiaorui Sun 1 Last Lecture (summary) Stable matching problem: Given n men and n women, and their preferences, find a stable matching. For a perfect matching M , a pair
Xiaorui Sun
1
2
For a perfect matching M, a pair m-w is unstable if they prefer each other to their match in M.
Zoran Amy Claire Brenda Yuri Brenda Claire Amy Xavier Amy Claire Brenda 1st 2nd 3rd
Men’s Preference Profile
Claire Xavier Zoran Yuri Brenda Xavier Zoran Yuri Amy Yuri Zoran Xavier 1st 2nd 3rd
Women’s Preference Profile
favorite least favorite favorite least favorite
3
For a perfect matching M, a pair m-w is unstable if they prefer each other to their match in M.
Initialize each person to be free. while (some man is free and hasn't proposed to every woman) { Choose such a man m w = 1st woman on m's list to whom m has not yet proposed if (w is free) assign m and w to be engaged else if (w prefers m to her fiancé m') assign m and w to be engaged, and m' to be free else w rejects m }
4
5
Initialize each person to be free. while (some man is free and hasn't proposed to every woman) { Choose such a man m w = 1st woman on m's list to whom m has not yet proposed if (w is free) assign m and w to be engaged else if (w prefers m to her fiancé m') assign m and w to be engaged, and m' to be free else w rejects m }
O(n2) time O(n) time
preference, denoted as pref[m/w, i] Overall: O(n4) time O(n) time Can be improved to O(n) using the fact that if a man is free, then he hasn’t proposed to every woman yet (any free man is fine) Can be improved to O(n3)
We describe O(n2) time implementation. Representing men and women:
Assume men are named 1, …, n. Assume women are named n+1, …, 2n.
Free men:
Maintain two arrays wife[m], and husband[w].
Maintain a list of free men, e.g., in a queue.
Men proposing:
For each man, maintain a list of women, ordered by preference pref[m/w, i]. Maintain an array count[m] that counts the number of proposals made by man m.
6
7
Initialize each person to be free. while (some man is free and hasn't proposed to every woman) { Choose such a man m w = 1st woman on m's list to whom m has not yet proposed if (w is free) assign m and w to be engaged else if (w prefers m to her fiancé m') assign m and w to be engaged, and m' to be free else w rejects m }
O(1) time O(1) time
Women rejecting/accepting.
Does woman w prefer man m to man m'? For each woman, create inverse of preference list of men. Constant time access for each query after O(n) preprocessing per woman. O(n2) total preprocessing cost.
8
for i = 1 to n inverse[amy, pref[amy, i]] = i
Pref 1st 8 2nd 7 3rd 3 4th 4 5th 1 5 2 6 6th 7th 8th Inverse 4th 2nd 8th 6th 5th 7th 1st 3rd 1 2 3 4 5 6 7 8 Amy Amy
Amy prefers man 3 to 6, since !"#$%&$[()*, ,] = / < 1 = !"#$%&$[()*, 2]
9
Initialize each person to be free. while (some man is free and hasn't proposed to every woman) { Choose such a man m w = 1st woman on m's list to whom m has not yet proposed if (w is free) assign m and w to be engaged else if (w prefers m to her fiancé m') assign m and w to be engaged, and m' to be free else w rejects m }
O(1) time O(1) time Overall: O(n2) time O(1) time
10
11
12 Yuri Xavier B A 1st A B 2nd X Brenda Amy Y 1st Y X 2nd
13
Suppose some man is paired with someone other than his best valid partner. Men propose in decreasing order of preference Þ some man is rejected by a valid partner. Let m be the man who is the first such rejection, and let w be the women who is first valid partner that rejects him. Let S be a stable matching where w and m are matched. In building S*, when m is rejected, w forms (or reaffirms) engagement with a man, say m’, whom she prefers to m. Let w’ be the partner of m’ in S. In building S*, m’ is not rejected by any valid partner at the point when m is rejected by w. Thus, m’ prefers w to w’. But w prefers m’ to m. Thus w-m’ is unstable in S.
14
m’-w’ m-w
S
. . . since this is the first rejection by a valid partner
15
w is a valid partner of m if there exist some stable matching where m and w are paired
16
Different implementations may have different running time
Proof by contradiction
17
18
Alvin Roth modified the Gale-Shapley algorithm and apply it to
doctors to hospitals around the country. (90s)
(Saved >1,000 people every year!)
Reference: https://medium.com/@UofCalifornia/how-a-matchmaking-algorithm-saved-lives-2a65ac448698
19
20
Shapley and Roth got the Nobel Prize (Economic) in 2012. (David Gale passed away in 2008.)
Some of the problems in this course may seem obscure or even pointless when it was discovered. But their abstraction allows for variety of applications.
21
22
Jobs don’t overlap
23
24
25
Subset of nodes such that no two joined by an edge
26
Second player can guarantee 20, but not 25
27