Computational Processes Mathematics for Computer Science MIT 6.042J/18.062J Computational Processes Copyright © Albert Meyer, 2002. Prof. Albert Meyer & Dr. Radhika Nagpal
Die Hard Picture source: http://movieweb.com/movie/diehard3/
Die Hard Supplies: 3 Gallon Jug 5 Gallon Jug Water
Die Hard Transferring water: 3 Gallon Jug 5 Gallon Jug
Die Hard Transferring water: 3 Gallon Jug 5 Gallon Jug
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?
Die Hard Work it out now!
How to do it Start with empty jugs: (0,0) 3 Gallon Jug 5 Gallon Jug
How to do it Fill the big jug: (0,5) 3 Gallon Jug 5 Gallon Jug
Pour from big to little: How to do it Pour from big to little: (3,2) 3 Gallon Jug 5 Gallon Jug
How to do it Empty the little: (0,2) 3 Gallon Jug 5 Gallon Jug
Pour from big to little: How to do it Pour from big to little: (2,0) 3 Gallon Jug 5 Gallon Jug
How to do it Fill the big jug: (2,5) 3 Gallon Jug 5 Gallon Jug
Done!! Pour from big to little: How to do it (3,4) 3 Gallon Jug
Die Hard once and for all What if you have a 9 gallon jug instead? 3 Gallon Jug 5 Gallon Jug 9 Gallon Jug
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?
Die Hard Work it out now!
State machine: Step by step procedure, possibly responding to input.
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
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
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)
State machines Die Hard Transitions: Fill the little jug: Fill the big jug: Empty the little jug: Empty the big jug:
(b,l) 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 b + l 5 otherwise (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) 6. Pour from big jug into little jug. Likewise.
Invariant: State Invariants Die hard once and for all P(state) = 3 divides number of gallons in each jug.
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
The Robot The robot is on a grid. y 2 1 x 0 1 2 3
The robot can move diagonally. y 2 1 x 0 1 2 3
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 0 1 2 3
NO! P((x, y)) ::= x + y is even P((0,0)) is true. Robot Invariant NO! P((x, y)) ::= x + y is even P((0,0)) is true. Transition adds ±1 to both x and y
So all positions (x, y) reachable by robot Robot Invariant So all positions (x, y) reachable by robot have x + y even, but 1 + 0 = 1 is odd. Therefore (1,0) is not reachable.
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.
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.
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.
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.
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).
Class Problems
Robert Floyd Picture source: http://www.stanford.edu/dept/news/report/news/november7/floydobit-117.html