State Machines II: Derived Variables, If Values = v Stable ... · • Matching Hospitals &...

10
Mathematics for Computer Science MIT 6.042J/18.062J State Machines II: Derived Variables, Stable Marriage Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.1 Derived Variables Derived Variables Robot on the grid example: Another derived variable: States Q = ` 2 . π ::= σ (mod 2). Define the sum-value, σ, of a state: π is {0,1}-valued. σ(x,y) ::= x+y An `-valued derived variable. lec 8M.4 October 24, 2005 Copyright © Albert R. Meyer, 2005. October 24, 2005 Copyright © Albert R. Meyer, 2005. lec 8M.5 Derived Variables Called derivedto distinguish from actual variables that appear in a program. For robot Actual: x, y Derived: σ, π Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.6 Derived Variables A derived variable, v, is a function giving a “value” to each state: v: Q Values. If Values = `, we’d say v was natural-number-valued,” or `-valued.” Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.3 Derived Variables For GCD, have (actual) variables x, y. Proof of GCD termination: y is strictly decreasing and natural number-valued. Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.7 1

Transcript of State Machines II: Derived Variables, If Values = v Stable ... · • Matching Hospitals &...

Page 1: State Machines II: Derived Variables, If Values = v Stable ... · • Matching Hospitals & Residents. ... total number of names not crossed off boy’s lists ::= ... Stable Marriage:

Mathematics for Computer Science MIT 6.042J/18.062J

State Machines II: Derived Variables,

Stable Marriage

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.1

Derived Variables Derived Variables

Robot on the grid example: Another derived variable: States Q = `2. π ::= σ (mod 2). Define the sum-value, σ, of a state: π is {0,1}-valued.

σ(⟨x,y⟩) ::= x+y An `-valued derived variable.

lec 8M.4 October 24, 2005 Copyright © Albert R. Meyer, 2005. October 24, 2005 Copyright © Albert R. Meyer, 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. October 24, 2005 lec 8M.6

Derived Variables

A derived variable, v, is a function giving a “value” to each state:

v: Q → Values.If Values = `, we’d say v was

“natural-number-valued,” or “`-valued.”

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.3

Derived Variables

For GCD, have (actual) variables x, y.Proof of GCD termination:

y is strictly decreasing and natural number-valued.

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.7

1

Page 2: State Machines II: Derived Variables, If Values = v Stable ... · • Matching Hospitals & Residents. ... total number of names not crossed off boy’s lists ::= ... Stable Marriage:

Derived Variables

Termination followed by Well Ordering Principle:

y must take a least value – and then the algorithm is stuck.

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.8

Weakly Decreasing Variable

N 16

12

8

4

0 State876543210 QQQQQQQQQ

Down or constant after each step

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.10

Weakly Decreasing Variable

(We used to call weakly decreasing variables “nonincreasing” variables.)

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.12

Strictly Decreasing Variable

N 16

12

8

4

0 State876543210 QQQQQQQQQ

Goes down at every step

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.9

σ, π for the Diagonal Robot

σ: 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

Problem 1

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.14

2

Page 3: State Machines II: Derived Variables, If Values = v Stable ... · • Matching Hospitals & Residents. ... total number of names not crossed off boy’s lists ::= ... Stable Marriage:

3

lec 8M.15October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage

A Marriage Problem

1 2 3 4 5

A B C D E

Boys

Girls

lec 8M.16October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage

Boys Girls1: CBEAD A : 352142 : ABECD B : 521433 : DCBAE C : 435124 : ACDBE D : 123455 : ABDEC E : 23415

Preferences:

lec 8M.17October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage

1: CBEAD2 : ABECD3 : DCBAE4 : ACDBE5 : ABDEC

Try “greedy”strategy for boys

Preferences

lec 8M.18October 24, 2005Copyright © Albert R. Meyer, 2005.

1: CBEAD2 : ABECD3 : DCBAE4 : ACDBE5 : ABDEC

Stable Marriage

Marry Boy 1 with Girl C(his 1st choice)

C1

Preferences

lec 8M.19October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage

2 : ABE D3 : D BAE4 : A DBE5 : ABDE

Marry Boy 1 with Girl C(his 1st choice)

C1

Preferences

lec 8M.20October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage

2 : ABED3 : DBAE4 : ADBE5 : ABDE

Marry Boy 1 with Girl C(his 1st choice)

C1

Preferences

Page 4: State Machines II: Derived Variables, If Values = v Stable ... · • Matching Hospitals & Residents. ... total number of names not crossed off boy’s lists ::= ... Stable Marriage:

4

lec 8M.21October 24, 2005Copyright © Albert R. Meyer, 2005.

2 : ABED3 : DBAE4 : ADBE5 : ABDE

Stable Marriage

Next:Marry Boy 2 with Girl A:(best remaining choice)

A2

C1

Preferences

lec 8M.22October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage

Final “boy greedy” marriages

1 C 2 A 3 D

4 B 5 E

lec 8M.23October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage

Trouble!

C1

B4lec 8M.24October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage

Boy 4 likes Girl C better than his wife.

C1

B4

lec 8M.25October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriageand vice-versa

C1

B4lec 8M.26October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable MarriageRogue Couple

C1

B4

Page 5: State Machines II: Derived Variables, If Values = v Stable ... · • Matching Hospitals & Residents. ... total number of names not crossed off boy’s lists ::= ... Stable Marriage:

5

lec 8M.27October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage

Stable Marriage Problem: Marry everyone without

any rogue couples!

lec 8M.28October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage

Let’s Try it!?Volunteers:

5 Boys & 5 Girls

lec 8M.29October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage I.

5 A 2 B 4 C

3 D 1 E

Boy Optimal

lec 8M.30October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage II.

3 A 5 B 4 C

1 D 2 E

All Girls get 1st Choice

lec 8M.31October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage

More than a puzzle:• College Admissions

(original Gale & Shapley paper, 1962)

• Matching Hospitals & Residents.• Matching Dance Partners.

lec 8M.32October 24, 2005Copyright © Albert R. Meyer, 2005.

Stable Marriage

Page 6: State Machines II: Derived Variables, If Values = v Stable ... · • Matching Hospitals & Residents. ... total number of names not crossed off boy’s lists ::= ... Stable Marriage:

Stable Marriage

The Mating Algorithm: day by day

Copyright © Albert R. Meyer, 2005. All rights reserved. October 24, 2005 lec 8M.33

Mating Algorithm

Morning: boy serenades favorite girl Afternoon: girl rejects all but favorite

Ted Alice Bob

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.35

Mating Algorithm

Stop when no girl rejects.

Girl marries her favorite suitor.

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.37

Mating Algorithm

Morning: boy serenades favorite girl

Ted Alice Bob

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.34

Mating Algorithm

Morning: boy serenades favorite girl Afternoon: girl rejects all but favorite Evening: rejected boy writes off girl

Ted … Alice

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.36

Team Problem

Problem 2

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.38

6

Page 7: State Machines II: Derived Variables, If Values = v Stable ... · • Matching Hospitals & Residents. ... total number of names not crossed off boy’s lists ::= ... Stable Marriage:

Mating Algorithm

Partial Correctness: • Everyone is married. • Marriages are stable. Termination:

there exists a Wedding Day.

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.39

Mating Algorithm: variables

Derived Variable

serenading(Bob):Bob’s favorite eligible girl.(“Top” on Bob’s list.)::= max{q(Bob)} using Bobs’ preference order.

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.41

Stable Marriage: Termination

Derived Variable

boy’s-list-length: total number of names not crossed off boy’s lists

::= Σb ∈ boys |q(b)|

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.43

Mating Algorithm

Model as State Machine

State q: Each boy’s set of “eligible” girls142 43

not crossed off

q(Bob) = {Carole, Alice, …}

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.40

Mating Algorithm: variables

Derived Variable

suitors(Alice):all boys serenading Alice.

::= serenading−1(Alice)

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.42

Stable Marriage: Termination

boy’s-list-length: strictly decreasing & N-valued.

So ∃ Wedding Day.

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.44

7

Page 8: State Machines II: Derived Variables, If Values = v Stable ... · • Matching Hospitals & Residents. ... total number of names not crossed off boy’s lists ::= ... Stable Marriage:

Mating Algorithm: variables

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

Lemma: A girl’s favorite tomorrow will be at least as desirable as today’s.

That is, favorite(G) is weakly increasing for each G.

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.47

Mating Algorithm: Boys Get Worse

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.

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.49

Mating Algorithm

Different girls have differentfavorites,because boys serenadeone girl at a time.(favorite: Girls→Boys

is an injection) Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.46

Mating Algorithm: Girls improve

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.

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.

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.50

8

Page 9: State Machines II: Derived Variables, If Values = v Stable ... · • Matching Hospitals & Residents. ... total number of names not crossed off boy’s lists ::= ... Stable Marriage:

Mating Algorithm: Invariant

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

Everyone is Married by Wedding Day Proof: by contradiction.If B is not married, his list is empty.By Invariant, all girls have favoritesbetter than B -- so they do have a favorite.That is, all girls are married. So all boys are married.

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.53

Mating Algorithm

Who does better, boys or girls?

Girls’ suitors get better, and boy’s sweethearts get worse, so girls do better? No!

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.57

Stable Marriage: Termination

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

Problem 3

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.54

Mating Algorithm

Mating Algorithm is Optimal for all Boys at once. Pessimal for all Girls.

Copyright © Albert R. Meyer, 2005. October 24, 2005 lec 8M.58

9

Page 10: State Machines II: Derived Variables, If Values = v Stable ... · • Matching Hospitals & Residents. ... total number of names not crossed off boy’s lists ::= ... Stable Marriage:

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