Title: Finite State Machines
1Finite State Machines
- COMP 114
- Tuesday April 23
2Announcements
- TA of the year award
- See class web page for details
3Topics
- Last weeks grammar
- Finite State Machines
4What About 5 7 2 ?
- expression term plusminus term
- term factor multdiv factor
- factor number ( expression )
- FAILS!
5What About 5 7 2 ?
- expression term plusminus term
- term factor multdiv factor
- factor number ( expression )
- The problem here is that all expressions must end
with a plusminus term
6New Grammar
- expression term plusminus term
- term factor multdiv factor
- factor number ( expression )
7Finite State Machines
- Also called Finite Automata
- These are machines (real or theoretical) that
have a finite number of states - Imagine elevator
- current floor
- direction
- no history of where it has been
8Examples
- Text editors
- The Find in Microsoft Word
- Could think of computer as a very large finite
state machine - not very useful too many states
- Brains as finite state machines?
- Maybe not analog instead of digital state
9Sample Problem
- Man with wolf, goat, and cabbage is on left side
of river - Has a small rowboat, just big enough for him and
one other thing - Cant leave goat and wolf together
- Cant leave goat and cabbage together
- Can he get them to right bank intact?
10Model
- Current state is list of what things are on which
side of river - All on left
- Man and goat on right
- All on right (desired state)
MWGC -
WC - MG
- MWGC
11State Transitions
- Well indicate with arrows changes between states
- Letter indicates what happened
- g man took goat c man took cabbage
- w man took wolf m man went alone
g
MWGC -
WC - MG
12Transition Both Ways
g
MWGC -
WC - MG
g
13Some States Bad!
c
MWGC -
WG - MC
14Diagram of Solutions
- Can draw a diagram of solutions
- and transitions
- that dont lead to bad states
15Want To Try?
16Getting Started
g
MWGC -
WC - MG
g
Start
- MWGC
17State Diagram
m
g
MWGC -
MWC - G
WC - MG
m
g
c
c
w
w
Start
W - MGC
C - MWG
g
g
g
g
WGM - C
MGC - W
c
c
w
w
g
m
- MWGC
G - MWC
MG - WC
m
g
18Some Differences
- Not a typical state machine
- This problem has symmetric transitions
- Often not the case
- Only one ending state
- Often many accepting states
19Finite Automaton
- Finite set of states, Q
- Start state q0
- Set of transitions from one state to another
- Occur on input from alphabet ?
- Exactly one transition out of each state for each
symbol in input alphabet - A subset of final states, F ? Q
20Recognizing Integers
b
b
s
B
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
21Example
_ 1 2
b
b
s
B
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
22Example
_ 1 2
b
b
s
B
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
23Example
_ 1 2
b
b
s
B
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
24Example
_ 1 2
b
b
s
B
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
25Example
_ 1 2
b
b
s
B
Accept!
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
26Example 2
_ z34
b
b
s
B
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
27Example 2
_ z34
b
b
s
B
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
28Example 2
_ z34
b
b
s
B
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
29Example 2
_ z34
b
b
s
B
Start
d, b, s
b
b
S
d
NG
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
30Example 2
_ z34
b
b
s
B
FAIL!
Start
d, b, s
b
b
S
d
NG
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
31Example 3
1___
b
b
s
B
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
32Example 3
1___
b
b
s
B
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
33Example 3
1___
b
b
s
B
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
34Example 3
1___
b
b
s
B
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
35Example 3
1___
b
b
s
B
Accept!
Start
d, b, s
b
b
S
d
OK
OK
d, s
s
d
s
d
Key
d digit, b blank, s other symbol
36How To Implement
37Case Implementation
- final int START 0
- final int BLANK 1
- final int DIGIT 2
- final int AFTER 3
- final int NG 4
-
- int state START
- char c
38Start Case
- for(int i 0 i lt s.length() i)
- c s.charAt(i)
- switch(state)
- case START
- if(c ' ')
- state BLANK
- else
- if(Character.isDigit(c))
- state DIGIT
- else
- state NG
- break
39Digit Case
- case DIGIT
- if(c ' ')
- state AFTER
- else
- if(Character.isDigit(c))
- state DIGIT
- else
- state NG
- break
40NG Case
- case NG
- default
- state NG
- break
41Table for Example
Table is just a very small array
42Limits of FSM
- How about a string like
- ai bi
- Same number of as as bs
- FSM cant recognize because theres no notion of
past history. - More complex machines can recognize
- COMP 181
43Summary
- Ive told you
- How finite state machines work
- Sketched how to design one
- How to implement one
- Have not told you
- How to make one automatically, given a language
to recognize
44References
- Introduction to Automata Theory, Languages, and
Computation by Hopcroft and Ullman - Introduction to Algorithms by Cormen, Leiserson,
and Rivest
45Next Time
- 3D Computer Graphics
- Movies!