Derived Variables Mathematics for Computer Science MIT 6.042J/18.062J A derived variable, v, is a function giving a “value” to each state: v: Q → Values. If Values = `, we’d say v was State Machines II: Derived Variables, Stable Marriage Copyright © Albert R. Meyer, 2005. October 24, 2005 “natural-number-valued,” or “`-valued.” lec 8M.1 Derived Variables Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.3 Derived Variables Robot on the grid example: States Q = `2. Another derived variable: π ::= σ (mod 2). π is {0,1}-valued. Define the sum-value, σ, of a state: σ(〈x,y〉) ::= x+y An `-valued derived variable. Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.4 Derived Variables October 24, 2005 October 24, 2005 lec 8M.5 Derived Variables Called “derived” to distinguish from actual variables that appear in a program. For robot Actual: x, y Derived: σ, π Copyright © Albert R. Meyer, 2005. Copyright © Albert R. Meyer, 2005. For GCD, have (actual) variables x, y. Proof of GCD termination: y is strictly decreasing and natural number-valued. lec 8M.6 Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.7 1 Derived Variables Strictly Decreasing Variable Termination followed by Well Ordering Principle: y must take a least value – and then the algorithm is stuck. N 16 8 4 0 Copyright © Albert R. Meyer, 2005. lec 8M.8 October 24, 2005 Down or constant after each step 12 8 4 0 Q0 Q1 Q2 Copyright © Albert R. Meyer, 2005. Q3 Q4 Q5 Q6 Q7 Q8 State October 24, 2005 lec 8M.10 Weakly Decreasing Variable October 24, 2005 Q1 Q2 Copyright © Albert R. Meyer, 2005. Q3 Q4 Q5 Q6 Q7 Q8 State lec 8M.9 October 24, 2005 σ: up & down all over the place – neither increasing nor decreasing. π: is constant – both increasing & decreasing (weakly) Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.11 Team Problem (We used to call weakly decreasing variables “nonincreasing” variables.) Copyright © Albert R. Meyer, 2005. Q0 σ, π for the Diagonal Robot Weakly Decreasing Variable N 16 Goes down at every step 12 Problem 1 lec 8M.12 Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.14 2 Stable Marriage Stable Marriage Preferences: A Marriage Problem 1 2 3 4 5 Boys 1: CBEAD 2 : ABECD 3 : DCBAE 4 : ACDBE 5 : ABDEC Boys Girls A Copyright © Albert R. Meyer, 2005. B C D E lec 8M.15 October 24, 2005 Preferences Copyright © Albert R. Meyer, 2005. Preferences lec 8M.17 October 24, 2005 Copyright © Albert R. Meyer, 2005. 1 October 24, 2005 Copyright © Albert R. Meyer, 2005. 1 C lec 8M.18 October 24, 2005 Stable Marriage Marry Boy 1 with Girl C (his 1st choice) 2 : ABE D 3 : D BAE 4 : A DBE 5 : ABDE Marry Boy 1 with Girl C (his 1st choice) 1: CBEAD 2 : ABECD 3 : DCBAE 4 : ACDBE 5 : ABDEC Try “greedy” strategy for boys Stable Marriage Preferences lec 8M.16 October 24, 2005 Stable Marriage Stable Marriage 1: CBEAD 2 : ABECD 3 : DCBAE 4 : ACDBE 5 : ABDEC Copyright © Albert R. Meyer, 2005. Girls A : 35214 B : 52143 C : 43512 D : 12345 E : 23415 Preferences Marry Boy 1 with Girl C (his 1st choice) 2 : ABED 3 : DBAE 4 : ADBE 5 : ABDE C lec 8M.19 Copyright © Albert R. Meyer, 2005. 1 October 24, 2005 C lec 8M.20 3 Stable Marriage Preferences 1 C Next: Marry Boy 2 with Girl A: (best remaining choice) 2 : ABED 3 : DBAE 4 : ADBE 5 : ABDE Copyright © Albert R. Meyer, 2005. Stable Marriage 1 C lec 8M.21 October 24, 2005 1 lec 8M.23 Stable Marriage Copyright © Albert R. Meyer, 2005. 1 C 4 B October 24, 2005 lec 8M.24 Rogue Couple C 4 B October 24, 2005 Stable Marriage and vice-versa Copyright © Albert R. Meyer, 2005. C 4 B October 24, 2005 1 lec 8M.22 October 24, 2005 Boy 4 likes Girl C better than his wife. C 4 5 E Stable Marriage Trouble! 1 Copyright © Albert R. Meyer, 2005. 3 D 2 A 4 B A 2 Stable Marriage Copyright © Albert R. Meyer, 2005. Final “boy greedy” marriages lec 8M.25 Copyright © Albert R. Meyer, 2005. B October 24, 2005 lec 8M.26 4 Stable Marriage Stable Marriage Let’s Try it! ?Volunteers: 5 Boys & 5 Girls Stable Marriage Problem: Marry everyone without any rogue couples! Copyright © Albert R. Meyer, 2005. lec 8M.27 October 24, 2005 Stable Marriage I. 5A 4C 3A 1E 4C 5B 2E 1D Boy Optimal October 24, 2005 lec 8M.28 October 24, 2005 Stable Marriage II. 2B 3D Copyright © Albert R. Meyer, 2005. Copyright © Albert R. Meyer, 2005. All Girls get 1st Choice lec 8M.29 Stable Marriage Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.30 Stable Marriage More than a puzzle: • College Admissions (original Gale & Shapley paper, 1962) • Matching Hospitals & Residents. • Matching Dance Partners. Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.31 Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.32 5 Stable Marriage Mating Algorithm Morning: boy serenades favorite girl The Mating Algorithm: day by day Ted Alice Bob Copyright © Albert R. Meyer, 2005. All rights reserved. lec 8M.33 October 24, 2005 Mating Algorithm Copyright © Albert R. Meyer, 2005. lec 8M.34 October 24, 2005 Mating Algorithm Morning: boy serenades favorite girl Afternoon: girl rejects all but favorite Evening: rejected boy writes off girl Morning: boy serenades favorite girl Afternoon: girl rejects all but favorite … Alice Ted … Ted Alice Bob Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.35 Mating Algorithm October 24, 2005 October 24, 2005 lec 8M.36 Team Problem Stop when no girl rejects. Girl marries her favorite suitor. Copyright © Albert R. Meyer, 2005. Copyright © Albert R. Meyer, 2005. Problem 2 lec 8M.37 Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.38 6 Mating Algorithm Mating Algorithm Partial Correctness: Model as State Machine State q: Each boy’s set of “eligible” 1424 3 girls • Everyone is married. • Marriages are stable. not crossed off Termination: q(Bob) = {Carole, Alice, …} there exists a Wedding Day. Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.39 Mating Algorithm: variables Copyright © Albert R. Meyer, 2005. October 24, 2005 Mating Algorithm: variables Derived Variable Derived Variable serenading(Bob): suitors(Alice): Bob’s favorite eligible girl. (“Top” on Bob’s list.) ::= max{q(Bob)} using Bobs’ preference order. all boys serenading Alice. Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.40 lec 8M.41 Stable Marriage: Termination ::= serenading−1(Alice) Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.42 Stable Marriage: Termination Derived Variable boy’s-list-length: boy’s-list-length: strictly decreasing & N-valued. total number of names not crossed off boy’s lists So ∃ Wedding Day. ::= Σb ∈ boys |q(b)| Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.43 Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.44 7 Mating Algorithm: variables Mating Algorithm Derived Variable favorite(Carole): Carole’s preferred suitor. ::= max{suitors(Carole)} using Carole’s preference order. Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.45 Mating Algorithm: Girls improve That is, favorite(G) is weakly increasing for each G. October 24, 2005 lec 8M.47 Lemma: A boy’s 1st love tomorrow will be no more desirable than today’s. That is, serenading(B) is weakly decreasing for each B. October 24, 2005 October 24, 2005 lec 8M.46 Lemma: A girl’s favorite tomorrow will be at least as desirable as today’s. …because today’s favorite will stay until she rejects him for someone better. Mating Algorithm: Boys Get Worse Copyright © Albert R. Meyer, 2005. Copyright © Albert R. Meyer, 2005. Mating Algorithm: Girls improve Lemma: A girl’s favorite tomorrow will be at least as desirable as today’s. Copyright © Albert R. Meyer, 2005. Different girls have different favorites, because boys serenade one girl at a time. (favorite: Girls→Boys is an injection) Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.48 Mating Algorithm: Boys Get Worse Lemma: A boy’s 1st love tomorrow will be no more desirable than today’s. …because boys work straight down their lists. lec 8M.49 Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.50 8 Mating Algorithm: Invariant Stable Marriage: Termination If G has rejected B, then she has a better current favorite. Proof: favorite(G) is weakly increasing. Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.51 Mating Algorithm: Everyone Marries On Wedding Day: • Each girl has 1 suitors • Each boy is married, or has no girls on his list Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.52 Team Problem Everyone is Married by Wedding Day Proof: by contradiction. If B is not married, his list is empty. By Invariant, all girls have favorites better than B -- so they do have a favorite. Problem 3 That is, all girls are married. So all boys are married. Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.53 Mating Algorithm lec 8M.54 Mating Algorithm is Optimal for all Boys at once. Pessimal for all Girls. Girls’ suitors get better, and boy’s sweethearts get worse, so girls do better? No! October 24, 2005 October 24, 2005 Mating Algorithm Who does better, boys or girls? Copyright © Albert R. Meyer, 2005. Copyright © Albert R. Meyer, 2005. lec 8M.57 Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.58 9 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! Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.59 10