Mathematics for Computer Science MIT 6 042 J18




















































- Slides: 52
Mathematics for Computer Science MIT 6. 042 J/18. 062 J Computational Processes II Copyright © Albert Meyer, 2002. Prof. Albert Meyer & Dr. Radhika Nagpal March 6, 2002 1
Computational Processes Last lecture: Invariants & Partial Correctness. March 6, 2002 2
Computational Processes Today: Termination, Derived Variables, Getting Married. March 6, 2002 3
Derived variables A derived variable, v, is a function giving a value to each state. Robot example: • Sum of coordinates, v : : = x + y. • Parity of x + y, w : : = v mod 2. March 6, 2002 4
Derived variables For GCD, already have variables x, y. March 6, 2002 5
Derived variables Proof of GCD termination: variable y is strictly decreasing and natural number valued. March 6, 2002 6
Derived variables Termination follows by least number principle: y must take a least value, and thus the algorithm is stuck. March 6, 2002 7
Strictly Decreasing Variable 16 12 N Goes down at every step 8 4 0 State March 6, 2002 8
Weakly Decreasing Variable 16 12 N Down or constant after each step 8 4 0 State March 6, 2002 9
Weakly Decreasing Variable (We used to call weakly decreasing variables “nonincreasing” variables. ) March 6, 2002 10
Weakly Decreasing Variable OK terminology but remember: nonincreasing is NOT SAME as “not increasing. ” March 6, 2002 11
In-class Problem 1 March 6, 2002 12
Stable Marriage A Marriage Problem 1 2 3 4 5 Boys Girls A B C D E March 6, 2002 13
Stable Marriage Preferences: Boys 1: CBEAD 2 : ABECD 3 : DCBAE 4 : ACDBE 5 : ABDEC Girls A : 35214 B : 52143 C : 43512 D : 12345 E : 23415 March 6, 2002 14
Stable Marriage Preferences 1: CBEAD 2 : ABECD 3 : DCBAE 4 : ACDBE 5 : ABDEC Try “greedy” strategy for boys March 6, 2002 15
Stable Marriage Preferences 1: CBEAD 2 : ABECD 3 : DCBAE 4 : ACDBE 5 : ABDEC Marry Boy 1 with Girl C (his 1 st choice) 1 C March 6, 2002 16
Stable Marriage Preferences 2 : ABE D 3 : D BAE 4 : A DBE 5 : ABDE Marry Boy 1 with Girl C (his 1 st choice) 1 C March 6, 2002 17
Stable Marriage Preferences 2 : ABED 3 : DBAE 4 : ADBE 5 : ABDE Marry Boy 1 with Girl C (his 1 st choice) 1 C March 6, 2002 18
Stable Marriage Preferences 1 C 2 : ABED 3 : DBAE 4 : ADBE 5 : ABDE Next: Marry Boy 2 with Girl A: (best remaining choice) 2 A March 6, 2002 19
Stable Marriage Final “boy greedy” marriages 1 C 3 D 2 A 4 B 5 E March 6, 2002 20
Stable Marriage Trouble! 1 C 4 B March 6, 2002 21
Stable Marriage Boy 4 likes Girl C better than his wife. 1 C 4 B March 6, 2002 22
Stable Marriage and vice-versa 1 C 4 B March 6, 2002 23
Stable Marriage Rogue Couple 1 C 4 B March 6, 2002 24
Stable Marriage Problem: Marry everyone without any rogue couples! March 6, 2002 25
Stable Marriage Do it! PROBLEM 2 March 6, 2002 26
Stable Marriage More than a puzzle: • College Admissions (original Gale & Shapley paper, 1962) • Matching Hospitals & Residents. • Matching Dance Partners. March 6, 2002 27
Stable Marriage March 6, 2002 28
Stable Marriage The Mating Algorithm March 6, 2002 29
Stable Marriage The Mating Algorithm: day by day March 6, 2002 30
Stable Marriage Morning: boy serenades favorite girl Ted Alice Bob March 6, 2002 31
Stable Marriage Morning: boy serenades favorite girl Afternoon: girl rejects all but favorite Ted Alice Bob March 6, 2002 32
Stable Marriage Morning: boy serenades favorite girl Afternoon: girl rejects all but favorite Evening: rejected boy writes off girl … Alice Ted … March 6, 2002 33
Stable Marriage Stop when no rejects. Girl marries her favorite suitor. March 6, 2002 34
In-class Problem 3 March 6, 2002 35
Stable Marriage Partial Correctness: • Everyone is married. • Marriages are stable. Termination: there exists a Wedding Day. March 6, 2002 36
Stable Marriage Model as State Machine State q: Each boy’s set of eligible girls q(Bob)={Carole, Alice, …} March 6, 2002 37
Stable Marriage Derived Variable: serenading Bob’s favorite eligible girl. March 6, 2002 38
Stable Marriage Derived Variable: suitors all boys serenading Alice. March 6, 2002 39
Stable Marriage Derived Variable: favorite Carole’s preferred suitor. March 6, 2002 40
Stable Marriage Different girls have different favorites, because boys serenade one girl at a time. March 6, 2002 41
Stable Marriage Lemma: A girl’s favorite tomorrow will be at least as desirable as today’s. favorite(G) is weakly increasing for each G. March 6, 2002 42
Stable Marriage Lemma: A boy’s love tomorrow will be no more desirable than today’s. serenading(B) is weakly decreasing for each B. March 6, 2002 43
Stable Marriage Derived Variable: total-girls-names : : = total number of names not crossed off boy’s lists Strictly decreasing & N-valued. So Wedding Day. March 6, 2002 44
Stable Marriage Mating Invariant If G has rejected B, then she has a better current favorite. March 6, 2002 45
Stable Marriage Mating Invariant If G has rejected B, then she has a better current favorite. Proof: favorite(G) is weakly increasing. March 6, 2002 46
Stable Marriage Everyone is Married on the Wedding Day Proof by contradiction: If B is not married, can’t be serenading, so list is empty. By Invariant all girls have favorites better than B. So all girls are married. So all boys are married. March 6, 2002 47
Stable Marriage Stability: Similar easy argument using the Invariant. March 6, 2002 48
In-class Problem 4 March 6, 2002 49
Stable Marriage Who does better, boys or girls? Girls’ suitors get better, and boy’s sweethearts get worse, so girls do better? No! March 6, 2002 50
Stable Marriage Mating Algorithm is Optimal for all Boys at once. Pessimal for all Girls. March 6, 2002 51
Stable Marriage More questions, rich theory: Other stable marriages possible? - Can be many. Can a boy do better by lying? – No! Can a girl do better by lying – Yes! March 6, 2002 52