Download presentation
Presentation is loading. Please wait.
1
Computational Processes
Mathematics for Computer Science MIT 6.042J/18.062J Computational Processes Copyright © Albert Meyer, 2002. Prof. Albert Meyer & Dr. Radhika Nagpal
2
Die Hard Picture source:
3
Die Hard Supplies: 3 Gallon Jug 5 Gallon Jug Water
4
Die Hard Transferring water: 3 Gallon Jug 5 Gallon Jug
5
Die Hard Transferring water: 3 Gallon Jug 5 Gallon Jug
6
Psychopath’s challenge: Disarm bomb by putting 4 gallons
Die Hard Psychopath’s challenge: Disarm bomb by putting 4 gallons of water on top, or it will blow up. Question: How to do it?
7
Die Hard Work it out now!
8
How to do it Start with empty jugs: (0,0) 3 Gallon Jug 5 Gallon Jug
9
How to do it Fill the big jug: (0,5) 3 Gallon Jug 5 Gallon Jug
10
Pour from big to little:
How to do it Pour from big to little: (3,2) 3 Gallon Jug 5 Gallon Jug
11
How to do it Empty the little: (0,2) 3 Gallon Jug 5 Gallon Jug
12
Pour from big to little:
How to do it Pour from big to little: (2,0) 3 Gallon Jug 5 Gallon Jug
13
How to do it Fill the big jug: (2,5) 3 Gallon Jug 5 Gallon Jug
14
Done!! Pour from big to little: How to do it (3,4) 3 Gallon Jug
15
Die Hard once and for all
What if you have a 9 gallon jug instead? 3 Gallon Jug 5 Gallon Jug 9 Gallon Jug
16
Die Hard once and for all
What if you have a 9 gallon jug instead? 3 Gallon Jug 9 Gallon Jug Can you do it? Can you prove it?
17
Die Hard Work it out now!
18
State machine: Step by step procedure, possibly responding to input.
19
The state graph of a 99-bounded counter:
State machines The state graph of a 99-bounded counter: start state overflow 1 2 99
20
States: {0,1,…,99, overflow} State machines
The state graph of a 99-bounded counter: start state 1 2 99 overflow States: {0,1,…,99, overflow}
21
States: {0,1,…,99, overflow} Transitions: State machines
The state graph of a 99-bounded counter: start state 1 2 99 overflow States: {0,1,…,99, overflow} Transitions: 0 i < 99 i i+1 99 overflow
22
States: {0,1,…,99, overflow} Transitions: State machines
The state graph of a 99-bounded counter: start state 1 2 99 overflow States: {0,1,…,99, overflow} Transitions: overflow overflow
23
Die hard state machine State machines
State = amount of water in the jug: (b,l) where 0 b 3 and 0 l 5. Start state = (0,0)
24
State machines Die Hard Transitions: Fill the little jug: Fill the big jug: Empty the little jug: Empty the big jug:
25
5. Pour from little jug into big jug: If no overflow, then
State machines 5. Pour from little jug into big jug: If no overflow, then
26
5. Pour from little jug into big jug: If no overflow, then b + l 5
State machines 5. Pour from little jug into big jug: If no overflow, then b + l 5
27
(b,l) 5. Pour from little jug into big jug: (for l >0)
State machines 5. Pour from little jug into big jug: (for l >0) If no overflow, then …, (b,l) otherwise.
28
5. Pour from little jug into big jug: (for l >0)
State machines 5. Pour from little jug into big jug: (for l >0) 6. Pour from big jug into little jug. Likewise.
29
Invariant: State Invariants Die hard once and for all
P(state) = 3 divides number of gallons in each jug.
30
Invariant: State Invariants Die hard once and for all
P(state) = 3 divides number of gallons in each jug.
31
State Invariants Proof method (just like induction): Show base case: P(start). Invariant case: if P(q) and , then P(r). Conclusion: P holds for all reachable states, including final state (if any). q r
32
The Robot The robot is on a grid. y 2 1 x
33
The robot can move diagonally. y
2 1 x
34
Can it reach from (0,0) to (1,0)? y
The Robot Can it reach from (0,0) to (1,0)? y ? 2 1 GOAL x
35
Robot Invariant NO! P((x,y)) ::= x+y is even
36
Robot Invariant NO! P((x,y)) ::= x+y is even P((0,0)) is true.
37
NO! P((x,y)) ::= x+y is even P((0,0)) is true. Transition adds ±1 to
Robot Invariant NO! P((x,y)) ::= x+y is even P((0,0)) is true. Transition adds ±1 to both x and y
38
So all positions (x,y) reachable by robot
Robot Invariant So all positions (x,y) reachable by robot have x + y even, but = 1 is odd. Therefore (1,0) is not reachable.
39
GCD correctness Computing GCD(a,b) using the Euclidean Algorithm: Let x=a, y=b. If y=0, return x & terminate. Else set (x:=y, y:=x mod y) simultaneously; Repeat process.
40
GCD correctness Example: GCD(414,662)
41
GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 (mod 662) = 414
42
GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 mod 662 = 414 = GCD(414,248) since 662 mod 414 = 248
43
GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 mod 662 = 414 = GCD(414,248) since 662 mod 414 = 248 = GCD(248,166) since 414 mod 248 = 166
44
GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 mod 662 = 414 = GCD(414,248) since 662 mod 414 = 248 = GCD(248,166) since 414 mod 248 = 166 = GCD(166,82) since 248 mod 166 = 82
45
GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 mod 662 = 414 = GCD(414,248) since 662 mod 414 = 248 = GCD(248,166) since 414 mod 248 = 166 = GCD(166,82) since 248 mod 166 = 82 = GCD(82,2) since 166 mod 82 = 2
46
GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 mod 662 = 414 = GCD(414,248) since 662 mod 414 = 248 = GCD(248,166) since 414 mod 248 = 166 = GCD(166,82) since 248 mod 166 = 82 = GCD(82,2) since 166 mod 82 = 2 = GCD(2,0) since 82 mod 2 = 0
47
GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 mod 662 = 414 = GCD(414,248) since 662 mod 414 = 248 = GCD(248,166) since 414 mod 248 = 166 = GCD(166,82) since 248 mod 166 = 82 = GCD(82,2) since 166 mod 82 = 2 = GCD(2,0) since 82 mod 2 = 0 Return 2.
48
P((x,y)) ::= [gcd(a,b) = gcd(x,y)].
GCD correctness The Invariant is P((x,y)) ::= [gcd(a,b) = gcd(x,y)].
49
P((x,y)) ::= [gcd(a,b) = gcd(x,y)].
GCD correctness The Invariant is P((x,y)) ::= [gcd(a,b) = gcd(x,y)]. P(start start): at start x = a , y =b.
50
Transitions: (x,y) (y, rem(x,y))
GCD correctness Transitions: (x,y) (y, rem(x,y))
51
Transitions: (x,y) (y, rem(x,y))
GCD correctness Transitions: (x,y) (y, rem(x,y)) Invariant holds by Lemma: gcd(x,y) = gcd(y,rem(x,y)), for y 0.
52
Conclusion: on termination
GCD correctness Conclusion: on termination x equals gcd(a,b).
53
Conclusion: on termination
GCD correctness Conclusion: on termination x equals gcd(a,b). Because terminate when y = 0, so x = gcd(x,0) = gcd(x,y) = gcd(a,b).
54
Class Problems
55
Robert Floyd Picture source:
56
A derived variable v is simply a function
Derived variables A derived variable v is simply a function that associate value with state. Die Hard Example: total amount of water in the two jugs
57
For GCD, already have varables
Derived variables For GCD, already have varables x, y.
58
Proof of GCD termination:
Derived variables Proof of GCD termination: Show that variable y is strictly decreasing and natural number valued.
59
Termination follows by least number principle:
Derived variables Termination follows by least number principle: y must take a least value, and thus the algorithm is stuck.
60
Strictly Decreasing Variable
16 12 8 4 State
61
Weakly Decreasing Variable
16 12 8 4 State
62
Weakly Decreasing Variable
We used to call weakly decreasing variables “nonincreasing” variables. OK terminology but remember: nondecreasing is NOT EQUAL to “not decreasing.”
63
There are 5 boys and 5 girls
Stable Marriage There are 5 boys and 5 girls A B D E
64
Preferences: Boys Girls 1: CBEAD A: 35214 2 : ABECD B : 52143
Stable marriage Preferences: Boys Girls 1: CBEAD A: 35214 2 : ABECD B : 52143 3 : DCBAE C : 43512 4 : ACDBE D : 12345 5 : ABDEC E : 23415
65
Stable marriage Boys 1: CBEAD 2 : ABECD 3 : DCBAE 4 : ACDBE 5 : ABDEC
66
Boys 1: CBEAD 2 : ABECD 3 : DCBAE 4 : ACDBE 5 : ABDEC Marry 1 with C:
Stable marriage Boys 1: CBEAD 2 : ABECD 3 : DCBAE 4 : ACDBE 5 : ABDEC Marry 1 with C: C 1
67
Only 4 boys and 4 girls left. Boys 2 : ABED 3 : DBAE 4 : ADBE 5 : ABDE
Stable marriage Only 4 boys and 4 girls left. Boys 2 : ABED 3 : DBAE 4 : ADBE 5 : ABDE A B D E
68
A problem: Rogue couples
Stable marriage A problem: Rogue couples 1 C 4 B Girl C likes like boy 4 better Boy 4 like girl C better
69
The mating algorithm – day by day
Stable marriage The mating algorithm – day by day
70
The mating algorithm – day by day
Stable marriage The mating algorithm – day by day Morning: boys serenade favorite girls
71
The mating algorithm – day by day
Stable marriage The mating algorithm – day by day Morning: boys serenade favorite girls Afternoon: girl rejects all but best Serenader
72
The mating algorithm – day by day
Stable marriage The mating algorithm – day by day Morning: boys serenade favorite girls Afternoon: girl rejects all but best Serenader Evening: rejected boys cross off girls
73
Termination condition: stop when no rejection is possible.
Stable marriage Termination condition: stop when no rejection is possible. The algorithm terminates. Everybody ends up married. The resulting marriages are stable.
74
Infinitely Wide Trees … …
75
Infinitely Wide Trees … …
76
Infinitely Wide Trees
77
Infinitely Wide Trees
78
Tutorial Problem 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.