State Machines step by step processes (may step in response

advertisement
3/1/10
State machines
Mathematics for Computer Science
MIT 6.042J/18.062J
step by step processes
(may step in response
to input not today)
State
Machines
Albert R Meyer, March 3, 2010
lec 5W.1
State machines
Albert R Meyer, March 3, 2010
lec 5W.2
Die Hard
The state graph of a 99-bounded counter:
start state
0
1
2
99
overflow
Image removed due to copyright restrictions.
States: {0,1,…,99, overflow}
Transitions:
i
99
overflow
0 i < 99
i+1
overflow
overflow
Albert R Meyer, March 3, 2010
Picture source: http://movieweb.com/movie/diehard3/
lec 5W.3
Die Hard
Simon says: On the fountain, there
should be 2 jugs, do you see them?
A 5-gallon and a 3-gallon. Fill one
of the jugs with exactly 4 gallons
of water and place it on the scale
and the timer will stop. You must
be precise; one ounce more or less
will result in detonation. If you're
still alive in 5 minutes, we'll speak.
Albert R Meyer, March 3, 2010
Albert R Meyer, March 3, 2010
lec 5W.4
Die Hard
Supplies:
3 Gallon Jug
Water
lec 5W.5
5 Gallon Jug
Image by MIT OpenCourseWare.
Albert R Meyer, March 3, 2010
lec 5W.6
1
3/1/10
Die Hard
Die Hard
Transferring water:
3 Gallon Jug
Transferring water:
5 Gallon Jug
Albert R Meyer, March 3, 2010
3 Gallon Jug
lec 5W.7
Die hard state machine
5 Gallon Jug
Albert R Meyer, March 3, 2010
lec 5W.8
State machines
State:
Die Hard Transitions:
amount of water in jugs: (b,l)
0 b 5, 0 l 3
Start State: (0,0)
Albert R Meyer, March 3, 2010
lec 5W.9
State machines
1. Fill little jug:
(b, l) (b, 3) for l < 3
2. Fill big jug:
(b, l) (5, l) for b < 5
3. Empty little jug:
(b, l) (b, 0) for l > 0
4. Empty big jug:
(b, l) (0, l) for b > 0
Albert R Meyer, March 3, 2010
lec 5W.10
Die Hard
5. Pour big jug into little jug
(i) If no overflow, then (b,l)(0,b+l)
b+l 3
(ii) otherwise (b,l) (b(3l),3)
6. Pour little jug into big jug.
Likewise
Simon’s challenge:
Disarm the bomb by putting
precisely 4 gallons of water on
the scale, or it will blow up.
(You can figure out how)
Albert R Meyer, March 3, 2010
lec 5W.11
Albert R Meyer, March 3, 2010
lec 5W.12
2
3/1/10
Die Hard once and for all
Preserved Invariants
What if have a 9 gallon jug instead?
Die hard once and for all
preserved invariant:
P(state) ::= “3 divides the
number of gallons in each jug.”
3 Gallon Jug 5 Gallon Jug 9 Gallon Jug
P((b,l)) ::= (3|b AND 3|l)
Can you do it? Can you prove it?
Albert R Meyer, March 3, 2010
lec 5W.20
Albert R Meyer, March 3, 2010
Preserved Invariants
lec 5W.22
Die Hard Once & For All
Floyd’s Invariant Method
Corollary: No state
(4,x) is reachable, so
Bruce Dies!
(just like induction)
Base case: Show P(start)
Preservation case: Show
if P(q) and q
r , then P(r)
Conclusion: P holds for all reachable
states, including final state (if any)
Albert R Meyer, March 3, 2010
Water
Image by MIT OpenCourseWare.
lec 5W.23
Albert R Meyer, March 3, 2010
The Diagonal Robot
y
The Diagonal Robot
the robot is on a grid
y
2
2
1
1
0
Image by MIT OpenCourseWare.
0
1
2
Albert R Meyer, March 3, 2010
lec 5W.24
3
it can move diagonally
Image by MIT OpenCourseWare.
0
x
0
lec 5W.25
1
2
Albert R Meyer, March 3, 2010
3
x
lec 5W.26
3
3/1/10
The Diagonal Robot
y
Robot Preserved Invariant
NO! preserved invariant:
can it get from (0,0) to (1,0)?
?
P((x, y)) ::= x + y is even
2
1
move adds ±1 to both x & y,
preserving parity of x+y.
Also, P((0, 0)) is true.
Image by MIT OpenCourseWare.
GOAL
0
0
1
2
3
x
Albert R Meyer, March 3, 2010
lec 5W.27
Albert R Meyer, March 3, 2010
lec 5W.28
Robert W Floyd (19342001)
Robot Preserved Invariant
So all positions (x,y)
reachable from (0,0)
have x + y even.
But 1 + 0 = 1 is odd, so
(1,0) is not reachable.
Photograph removed due to copyright restrictions.
Eulogy by Knuth: http://www.acm.org/pubs/membernet/stories/floyd.pdf
Picture source: http://www.stanford.edu/dept/news/report/news/november7/floydobit-117.html
Albert R Meyer, March 3, 2010
lec 5W.29
Albert R Meyer, March 3, 2010
lec 5W.38
Team Problems
Problems
1&2
Albert R Meyer, March 3, 2010
lec 5W.39
4
MIT OpenCourseWare
http://ocw.mit.edu
6.042J / 18.062J Mathematics for Computer Science
Spring 2010
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
Download