Mathematics for Computer Science MIT 6 042 J18

  • Slides: 38
Download presentation
Mathematics for Computer Science MIT 6. 042 J/18. 062 J Computational Processes Copyright ©

Mathematics for Computer Science MIT 6. 042 J/18. 062 J Computational Processes Copyright © Albert Meyer, 2002. Prof. Albert Meyer & Dr. Radhika Nagpal March 3, 2002 1

Die Hard Picture source: http: //movieweb. com/movie/diehard 3/ March 3, 2002 2

Die Hard Picture source: http: //movieweb. com/movie/diehard 3/ March 3, 2002 2

Die Hard Supplies: 3 Gallon Jug 5 Gallon Jug Water March 3, 2002 3

Die Hard Supplies: 3 Gallon Jug 5 Gallon Jug Water March 3, 2002 3

Die Hard Transferring water: 3 Gallon Jug 5 Gallon Jug March 3, 2002 4

Die Hard Transferring water: 3 Gallon Jug 5 Gallon Jug March 3, 2002 4

Die Hard Transferring water: 3 Gallon Jug 5 Gallon Jug March 3, 2002 5

Die Hard Transferring water: 3 Gallon Jug 5 Gallon Jug March 3, 2002 5

Die Hard Psychopath’s challenge: Disarm bomb by putting 4 gallons of water on top,

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? March 3, 2002 6

Die Hard Work it out now! March 3, 2002 7

Die Hard Work it out now! March 3, 2002 7

How to do it Start with empty jugs: (0, 0) 3 Gallon Jug 5

How to do it Start with empty jugs: (0, 0) 3 Gallon Jug 5 Gallon Jug March 3, 2002 8

How to do it Fill the big jug: (0, 5) 3 Gallon Jug 5

How to do it Fill the big jug: (0, 5) 3 Gallon Jug 5 Gallon Jug March 3, 2002 9

How to do it Pour from big to little: (3, 2) 3 Gallon Jug

How to do it Pour from big to little: (3, 2) 3 Gallon Jug 5 Gallon Jug March 3, 2002 10

How to do it Empty the little: (0, 2) 3 Gallon Jug 5 Gallon

How to do it Empty the little: (0, 2) 3 Gallon Jug 5 Gallon Jug March 3, 2002 11

How to do it Pour from big to little: (2, 0) 3 Gallon Jug

How to do it Pour from big to little: (2, 0) 3 Gallon Jug 5 Gallon Jug March 3, 2002 12

How to do it Fill the big jug: (2, 5) 3 Gallon Jug 5

How to do it Fill the big jug: (2, 5) 3 Gallon Jug 5 Gallon Jug March 3, 2002 13

How to do it Pour from big to little: (3, 4) 3 Gallon Jug

How to do it Pour from big to little: (3, 4) 3 Gallon Jug 5 Gallon Jug Done!! March 3, 2002 14

Die Hard once and for all What if you have a 9 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 March 3, 2002 15

Die Hard once and for all What if you have a 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? March 3, 2002 16

Die Hard Work it out now! March 3, 2002 17

Die Hard Work it out now! March 3, 2002 17

State machines State machine: Step by step procedure, possibly responding to input. March 3,

State machines State machine: Step by step procedure, possibly responding to input. March 3, 2002 18

State machines The state graph of a 99 -bounded counter: start state 0 1

State machines The state graph of a 99 -bounded counter: start state 0 1 2 99 overflow States: {0, 1, …, 99, overflow} Transitions: i 99 i+1 0 i < 99 overflow March 3, 2002 19

State machines The state graph of a 99 -bounded counter: start state 0 1

State machines The state graph of a 99 -bounded counter: start state 0 1 2 99 overflow States: {0, 1, …, 99, overflow} Transitions: overflow March 3, 2002 20

State machines Die hard state machine State = amount of water in the jug:

State machines Die hard state machine State = amount of water in the jug: (b, l) where 0 b 3 and 0 l 5. Start state = (0, 0) March 3, 2002 21

State machines Die Hard Transitions: 1. Fill the little jug: 2. Fill the big

State machines Die Hard Transitions: 1. Fill the little jug: 2. Fill the big jug: 3. Empty the little jug: 4. Empty the big jug: March 3, 2002 22

State machines 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) March 3, 2002 23

State machines 5. Pour from little jug into big jug: (for l >0) 6.

State machines 5. Pour from little jug into big jug: (for l >0) 6. Pour from big jug into little jug. Likewise. March 3, 2002 24

State Invariants Die hard once and for all Invariant: P(state) = 3 divides number

State Invariants Die hard once and for all Invariant: P(state) = 3 divides number of gallons in each jug. March 3, 2002 25

State Invariants Proof method (just like induction): 1) Show base case: P(start). 2) Invariant

State Invariants Proof method (just like induction): 1) Show base case: P(start). 2) Invariant case: if P(q) and q then P(r). r , Conclusion: P holds for all reachable states, including final state (if any). March 3, 2002 26

The Robot y The robot is on a grid. 2 1 0 0 1

The Robot y The robot is on a grid. 2 1 0 0 1 2 3 x March 3, 2002 27

The Robot y The robot can move diagonally. 2 1 0 0 1 2

The Robot y The robot can move diagonally. 2 1 0 0 1 2 3 x March 3, 2002 28

The Robot y Can it reach from (0, 0) to (1, 0)? ? 2

The Robot y Can it reach from (0, 0) to (1, 0)? ? 2 1 GOAL 0 0 1 2 3 x March 3, 2002 29

Robot Invariant NO! P((x, y)) : : = x + y is even P((0,

Robot Invariant NO! P((x, y)) : : = x + y is even P((0, 0)) is true. Transition adds ± 1 to both x and y March 3, 2002 30

Robot Invariant So all positions (x, y) reachable by robot have x + y

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. March 3, 2002 31

GCD correctness Computing GCD(a, b) using the Euclidean Algorithm: 1. Let x = a,

GCD correctness Computing GCD(a, b) using the Euclidean Algorithm: 1. Let x = a, y = b. 2. If y = 0, return x & terminate. 3. Else set (x: =y, y: =x mod y) simultaneously; 4. Repeat process. March 3, 2002 32

GCD correctness Example: GCD(414, 662) = GCD(662, 414) since 414 mod 662 = 414

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 = GCD(2, 0) since 82 mod 2 = 0 Return 2. March 3, 2002 33

GCD correctness The Invariant is P((x, y)) : : = [gcd(a, b) = gcd(x,

GCD correctness The Invariant is P((x, y)) : : = [gcd(a, b) = gcd(x, y)]. P(start, start): at start x = a , y =b. March 3, 2002 34

GCD correctness Transitions: (x, y) (y, rem(x, y)) Invariant holds by Lemma: gcd(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. March 3, 2002 35

GCD correctness Conclusion: on termination x equals gcd(a, b). Because terminate when y =

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). March 3, 2002 36

Class Problems March 3, 2002 37

Class Problems March 3, 2002 37

Robert Floyd Picture source: http: //www. stanford. edu/dept/news/report/news/november 7/floydobit-117. html March 3, 2002 38

Robert Floyd Picture source: http: //www. stanford. edu/dept/news/report/news/november 7/floydobit-117. html March 3, 2002 38