The Stable Marriage Problem Algorithms and Networks 20142015
The Stable Marriage Problem Algorithms and Networks 2014/2015 Hans L. Bodlaender Johan M. M. van Rooij
A prize winning algorithm g Lloyd Shapley, Nobel Prize Winner 2012 in economics. g Obtained the prize for a number of contributions, one being the Gale-Shapley algorithm, discussed today. Photo Bengt Nyman http: //en. wikipedia. org/wiki/File: Lloyd_Shapley_2_2012. jpg 2
The stable marriage problem g There are n men and n women all unmarried. Each has a preference list on the persons of the opposite sex. g Does there exist and can we find a stable marriage (stable matching): a matching between men and women, such that there is no man and woman who both prefer each other above their partner in the matching? g Origin: assignment of medical students to hospitals (for internships). g Students list hospitals in order of preference. g Hospitals list students in order of preference. 3
Example g Arie: Ann Cindy Betty g Bert: Betty Ann Cindy g Carl: Betty Ann Cindy g Ann: Bert Carl Arie g Betty: Carl Bert Arie g Cindy: Bert Carl Arie 4 g Stable matching: (Arie, Cindy), (Bert, Ann), (Carl, Betty). g Matching (Arie, Cindy), (Bert, Betty), (Carl, Ann) is not stable. g Carl and Betty prefer each other above given partner: they form a Blocking pair.
Contents of this lecture g Stable marriage problem. g Gale-Shapley algorithm. g Man-optimal and woman-optimal stable marriages. g Stable roommates problem. 5 The Stable Marriage Problem
Algorithms and Networks STABLE MARRIAGE PROBLEM 6
Problem: greedy algorithm does not need to terminate g Greedy or ‘Soap-Series’ Algorithm: While there is a blocking pair Do Switch the blocking pair g Greedy “local search” approach does not need to terminate: g Can go on for ever! g So, we need something else… 7
Result g Gale-Shapley algorithm: always finds a stable matching. g Input: list of men, women, and their preference list. g Output: stable matching. 8
The algorithm g Fix some ordering on the men. g Repeat until everyone is matched. g Let Y be the first unmatched man in the ordering. g Now Y will ‘propose’ to the women X highest on Y’s preference list to whom he has not proposed before. g If X has no partner, or feels that she is better off with Y than with her current partner, she accepts and X and Y become matched. This possibly turns a man Z to be unmatched. g Otherwise X turns down Y and Y will continue proposing to next woman on his list. Questions: Does this terminate? How fast? Does this give a stable matching? 9
A demonstration on the blackboard. Example: g Arie: Ann Cindy Betty g Bert: Betty Ann Cindy g Carl: Betty Ann Cindy g Ann: Bert Carl Arie g Betty: Carl Bert Arie g Cindy: Bert Carl Arie 10 Gale-Shapley Algorithm: Repeat until everyone is matched: g Let Y be the first unmatched man. g Y will ‘propose’ to the women X highest on Y’s preference list to whom he has not proposed before. g If X has no partner, or feels that she is better off with Y than with her current partner, she accepts and X and Y become matched (possibly leaving man Z). g Otherwise X turns down Y and Y will continue proposing to next woman on his list.
Termination and number of steps Observations: g Once a woman is matched, she stays matched. Her partner can change though. g If her partner changes, she will always get a more preferable partner. g The women to whom a man proposes will get worse (in his opinion) during the execution of algorithm. g If a man becomes free again, then there is a woman to whom he has not yet proposed. g There must be a free woman due to a counting argument, and he cannot have proposed to her by the first observation. g The computed set is a matching. g The algorithm uses at most n 2 steps/proposals. g Each man proposes to each women at most once. 11
The matching is stable g Suppose the computed matching is not stable. g There exist (X, Y), (X’, Y’) where • Y prefers X’ over X • X’ prefers Y over Y’. g Ys last proposal was to X (by definition of the algorithm). g Did Y propose to X’ during the execution of the algorithm? g No? Then Y does not prefer X’ over X – contradiction. g Yes? Then X’ rejected Y in favour of man Y’’. • Y’’ Y’! Otherwise, contradiction with that X’ prefers Y over Y’. g Since X’ ends up with Y’ we know that she prefers Y’ over Y’’. g But wait: X’ prefers Y over Y’ and Y’ over Y’’, thus Y over Y’’. • Contradiction: she rejected Y in favour of Y’’. 12
More than one stable matching? Conclusion: g A stable matching always exists and can be found in polynomial time. g How well are the men/women off in the stable matching produced by the Gale-Shapley algorithm? g What if the woman would propose? g Are there alternative stable matchings? Example: (preference lists) g Arie: Ann Betty g Bert: Betty Ann 13 g Ann: Bert Arie g Betty: Arie Bert
Man-optimal stable matchings Theorem: g All executions of the Gale-Shapley algorithm result in the set S = { (Y, best partner of Y in any stable matching) }. Corollaries: g S is a stable matching (not clear from the definition). g The algorithm produces a Man-optimal stable matching. g The algorithm produces the worst stable matching from the perspective of the women. g Suppose that in S woman X is matched to Y while Y is not her worst possible stable partner. g Then there is a stable matching S’ in which she is matched to Y’ that is worse than Y (let Y have partner X’ in S’). g Since X is the best stable partner of Y (in S) and X’ is a stable partner of Y (in S’), we know that Y prefers X to X’. g Contradiction: S’ is not stable, blocking pair (X, Y). 14
Proof of Theorem: g All executions of the Gale-Shapley algorithm result in the set S = { (Y, best partner of Y in any stable matching) }. g Suppose it is not true, then some man Y is rejected by a stable partner X during the execution of the algorithm. g Consider the first time this happens: X rejects Y in favour of Y’. g Because men propose in order of preference, and this is the first g g 15 such rejection, X is Ys best stable partner. Since X is a stable partner for Y, there is a stable matching S’ where X and Y are matched. Here Y’ is matched to X’ X. Since we consider the first rejection of a stable partner, Y’ has not been rejected yet by a stable partner: thus, Y’ prefers X over X’. Then S’ is not stable: X prefers Y’ over Y, Y’ prefers X over X’. Contradiction.
Stable marriage: conclusions g A stable matching always exists and can be found in O(n 2) time using the Gale-Shapley algorithm. g The Gale-Shapley algorithm produces a stable matching where: g All men have there best possible stable partner. g All woman have there worst possible stable partner. 16
Algorithms and Networks STABLE ROOMMATES PROBLEM 17
Stable roommates Given: g A set of people that must share two-person rooms. g Each person has a preference list with all possible roommates. Question: g Does there exist (and can we find) a stable matching: an assignment of roommates, such that there are no two people who both prefer each other above their current roommate. 18
Not always a stable matching for the stable roommates g Consider the following instance: g Arie: Carl Bert Dirk g Bert: Arie Carl Dirk g Carl: Bert Arie Dirk g Dirk: can be anything g Each matching is unstable e. g. , (Arie, Bert)(Carl, Dirk) has {Carl, Arie} as blocking pair. 19
Testing stable roommates g In the next slides we give an algorithm to find a stable roommates assignment or decide that no such assignment exists. g The algorithm will filter preference list, removing options that will never lead to stable assignments. g Some form of constraint propagation. Algorithm outline: g Phase 1: proposing and filtering. g Phase 2: removing all-or-nothing cycles. g Phase 3: find empty preference lists, or output solution. 20
Phase 1: proposing g In the proposing phase people propose to others to be roommates. g During the algorithm each person: g Has at most one outstanding proposal. g Keeps the best proposal he has received so far and rejects others. Algorithm: g Apply the following for each person once: 1. proposer : = current person. 2. proposer proposes to his best choice by whom he is not (yet) rejected. 3. if rejected, goto 2. 4. if accepted by someone without a proposal: done for this person. 5. else proposer : = person who just got rejected, goto 2. 21
Phase 1: example on the blackboard Example: 1: 4, 6, 2, 2: 6, 3, 5, 3: 4, 5, 1, 4: 2, 6, 5, 5: 4, 2, 3, 6: 5, 1, 4, 5, 1, 6, 2, 3. 4. 2. 3. 1. 3. Algorithm: g Apply the following for each person once: 1. proposer : = current 2. 3. 4. 5. 22 person. proposer proposes to his best choice by whom he is not (yet) rejected. if rejected, goto 2. if accepted by someone without a proposal: done for this person. else proposer : = person who just got rejected, goto 2.
A lemma on rejections Lemma: g If y rejects x in phase 1 of the algorithm, then x and y cannot be partners in a stable matching. g Suppose that x is rejected by y in favour of z while x and y are roommates in some stable matching S. g W. l. o. g. assume that this is the first such rejection while running the algorithm. g Let z be matched to w in S. Since S is stable, and y prefers z to x, z must prefer w over y. g But then, z has proposed to w earlier, and hence has been rejected by w earlier, contradicting that this is the first rejection by a stable roommate. 23 The Stable Marriage Problem
Best or worst stable roommates Lemma: g If y rejects x in phase 1 of the algorithm, then x and y cannot be partners in a stable matching. Corollaries: g If, in phase 1 of the algorithm, x proposes to y: 1. x cannot have a better stable roommate than y. 2. y cannot have a worse stable roommate than x. g (1. ) follows directly by the lemma. For (2. ): g Let S be a stable assignment, where y is matched to z while y prefers x over z, and where x is matched to any person w. g y prefers x to z, and by (1. ) x prefers y to w: contradiction to S being stable. 24
Filtering for phase 1 g Phase 1 finishes with either: 1. Some person being rejected by all others. g There is no stable matching (by the previous lemma). 2. Everyone holds a proposal from someone and has a made a proposal to someone that has not been rejected. g In the second case we can delete entries from the preference lists that never lead to a stable assignment. g If y holds a proposal from x then we remove from y’s preference list: g All those who rejected y. g All those to whom y prefers x (x was worst possible matching). g All those who hold a proposal from a person whom they prefer to y. 25
Filtering for phase 1 g If y holds a proposal from x then we remove from y’s preference list: g All those who rejected y. g All those to whom y prefers x (x was worst possible matching). g All those who hold a proposal from a person whom they prefer to y. The example 1: 4, 6, 2, 5, 3. 2: 6, 3, 5, 1, 4. 3: 4, 5, 1, 6, 2. 4: 2, 6, 5, 1, 3. 5: 4, 2, 3, 6, 1. 6: 5, 1, 4, 2, 3. 26 Proposals held in bold. Rejected in phase 1 in red. Worse than proposal in purple. Eliminated because the other holds a better proposal in green.
The result of phase 1 filtering g If y holds a proposal from x then we remove from y’s preference list: g All those who rejected y. g All those to whom y prefers x (x was worst possible matching). g All those who hold a proposal from a person whom they prefer to y. The example 1: 4, 6, 2, 5, 3. 2: 6, 3, 5, 1, 4. 3: 4, 5, 1, 6, 2. 4: 2, 6, 5, 1, 3. 5: 4, 2, 3, 6, 1. 6: 5, 1, 4, 2, 3. 27 The filtered example 1: 6. 2: 3, 5, 4. 3: 5, 2. 4: 2, 5. 5: 4, 2, 3. 6: 1.
Properties after filtering g Proposals after filtering: g Everyone holds a proposal, and the proposal is from the last person on his list. g Everyone is first on someone’s list, namely the person where he holds a proposal from. g Other properties after filtering: g x appears on y’s list, if and only if, y appears on x’s list. g x is first on y’s list, if and only if, y appears last on x’s list. g if x has only y on his list, then y has only x on his list and they must be together in a stable roommates assignment if such an assignment exists. g Now we are ready for phase 2. 28
Definition: all-or-nothing cycle Definition: g An all-or-nothing cycle is a sequence of persons a 1, a 2, . . . , ar such that: g The second person in ai’s current preference list is the first person in ai+1’s preference list, and we will call this person bi+1, the best match for ai+1. (all indices modulo r) a 1: b 1, b 2, . . . a 2: b 2, b 3, . . . a 3: b 3, b 4, . . . a 4: b 4, b 5, . . . . (modulo r) 29 ai is the i-the person on the cycle. bi is ai’s best remaining roommate. bi+1 is second on ai’s list.
Definition: all-or-nothing cycle Definition: g An all-or-nothing cycle is a sequence of persons a 1, a 2, . . . , ar such that: g The second person in ai’s current preference list is the first person in ai+1’s preference list, and we will call this person bi+1, the best match for ai+1. (all indices modulo r) The example 1: 6. 2: 3, 5, 4. 3: 5, 2. 4: 2, 5. 5: 4, 2, 3. 6: 1. 30 after phase 1 All-or-nothing cycle: 3, 4. a 1 = 3. b 2 = 2. a 1: b 1, b 2, . . . a 2 = 4. a 2: b 2, b 1, . . . b 1 = 5.
First lemma on all-or-nothing cycles Lemma: (this is why we call it an all-or-nothing cycle) g Let a 1, a 2, . . . , ar be an all-or-nothing cycle relative to a set of filtered preference lists, and let bi be the first person on ai’s preference list. In any stable assignment: g either ai and bi are roommates for all i, g or ai and bi are roommates for no value of i. g Suppose that for some i, ai and bi are partners in a stable matching S that is part of the filtered preference lists. g Since bi is first on ai’s filtered list, ai is last on bi’s filtered list. g Since bi is second on ai-1’s list, ai-1 is present on bi’s filtered list. g So, for S to be stable, ai-1 must be matched to the only person he prefers to bi, namely bi-1. g By induction and taking i modulo r, the result follows. 31
Second lemma on all-or-nothing cycles Lemma: g Let a 1, a 2, . . . , ar be an all-or-nothing cycle relative to a set of filtered preference lists, and let bi be the first person on ai’s preference list. g If there is a stable assignment S in which (all) a i and bi are partners, then there is another one in which they are not. g We may assume all ai to be different from all bi: no aj = bk. g If so, then not all ai can have their best partner bi, as aj = bk has its worst remaining partner as ak has his best (and aj has more than one option because it is on the all-or-nothing cycle). g Claim: Let S’ be the matching where ai is partnered to bi+1 (his second best choice), and any person that is not any ai or bi is partnered as in S. We claim that S’ is stable. g Lemma follows if we prove this claim. 32
Proof of the claim Claim: g Let S’ be the matching where ai is partnered to bi+1 (his second best choice), and any person that is not any a i or bi is partnered as in S. We claim that S’ is stable. g Each bi gets a better in S’ partner than in S, where he gets his worst possible remaining partner, so any instability must involve some ai (otherwise S not stable). Let ai prefer x to bi+1. Then: g Either x = bi (ai’s partner in S), then clearly bi prefers ai-1 to ai. g Or, x is no longer on ai’s reduced preference list. In this case, x currently holds a better proposal than ai, otherwise x would still be on ai’s list. And because x’s current proposal is his worst remaining option, he clearly prefers his partner in S’ to ai. 33
Filtering for phase 2 g Given an all-or-nothing cycle a 1, a 2, . . . , ar relative to a set of filtered preference lists, we filter the preference lists by: g Letting each bi reject the current proposal from ai. g Let each ai consequently propose to bi+1. g Removing all items further back on bi+1’s list than ai (these are now worse than bi+1’s current proposal. The example 1: 6. 2: 3, 5, 4. 3: 5, 2. 4: 2, 5. 5: 4, 2, 3. 6: 1. 34 with one phase-2 filtering step 1: 6. a 1 = 3. 2: 3. b 2 = 2. 3: 2. Stable assignment: a 2 = 4. 4: 5. (1, 6), (2, 3), (4, 5). b 1 = 5. 5: 4. 6: 1.
Phase 2 filtering maintains the properties g Proposals after filtering: g Everyone holds a proposal, and the proposal is from the last person on his list. g Everyone is first on someone’s list, namely the person where he holds a proposal from. g Other properties after filtering: g x appears on y’s list, if and only if, y appears on x’s list. g x is first on y’s list, if and only if, y appears last on x’s list. g if x has only y on his list, then y has only x on his list and they must be together in a stable roommates assignment if such an assignment exists. 35
Finding an all-or-nothing cycle g An all-or-nothing cycle is a sequence of persons a 1, a 2, . . . , ar such that: g The second person in ai’s current preference list is the first person in ai+1’s preference list, and we will call this person bi+1, the best match for ai+1. (all indices modulo r) g Property of filtered instances: g Everyone is first on someone’s list: the person where he holds a proposal from. a 1: b 1, b 2, . . . a 2: b 2, b 1, . . . g Let p 1 be any person who’s current preference list contains more than one person. g Let qi : = the second person on pi’s filtered list. g Let pi+1 : = the last person on qi’s filtered list (qi first on pi+1). g Repeat until this cycles, then ai = ps+i-1, where ps is the first pi to be repeated in the sequence. 36
Phase 3: find empty lists or output solution g After phase 1, apply phase 2 filtering exhaustively. g If at some point a reduced preference list is empty, then there is no stable roommates assignment. g Filtering keeps at least one stable roommates assignment. g If a reduced set of preference lists has exactly one person on each list, then the lists specify a stable assignment. g Identical to ‘proof of the claim’. If some y prefers person x over his current partner, then by filtering x must prefer the last person on his list over y. 37
Example where phase 2 results in an empty list Example 1: 2, 6, 4, 2: 3, 5, 1, 3: 1, 6, 2, 4: 5, 2, 3, 5: 6, 1, 3, 6: 4, 2, 5, 3, 6, 5, 6, 4, 1, 5. 4. 4. 1. 2. 3. Example after phase 1 filtering 1: 2, 3. 2: 3, 1. 3: 1, 2. 4: 5, 6. 5: 6, 4. 6: 4, 5. g All-or-nothing cycle: 1, 2, 3 g a 1 = 1, b 2 = 3. g a 2 = 2, b 3 = 1. g a 3 = 3, b 1 = 2. g If the bi reject their current proposal, lists become empty. 38
Stable Roommates Problem g We have shown a three phase algorithm for the stable roommates problem. g The algorithm filters preference list, removing options that will never lead to stable assignments. g Some form of constraint propagation. Three phases: g Phase 1: proposing and filtering. g Phase 2: removing all-or-nothing cycles. g Phase 3: find empty preference lists, or output solution. 39
Algorithms and Networks SOME FINAL COMMENTS 40
Final comments g Stable marriage problem g Stable matching always exists. g Man-optimal (or woman-optimal) matchings can be found using the Gale-Shapley algorithm. g Stable roommates problem. g Stable matchings do not always exists. g Testing whether one exists, and finding one can be done using the given three-phase algorithm. g Much further work has been done, e. g. : g Random / Fair stable matchings. g Many variants of stable matching are also solvable (indifferences, groups, forbidden pairs, …). g What happens if some participants lie about their preferences? g Stable roommates with indifferences: NP-complete. 41
- Slides: 41