Finite State Machines - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Finite State Machines

Description:

Man with wolf, goat, and cabbage is on left side of river ... Current state is list of what things are on which side of river. All on left ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 46
Provided by: anselmo9
Category:
Tags: finite | goat | list | machines | state

less

Transcript and Presenter's Notes

Title: Finite State Machines


1
Finite State Machines
  • COMP 114
  • Tuesday April 23

2
Announcements
  • TA of the year award
  • See class web page for details

3
Topics
  • Last weeks grammar
  • Finite State Machines

4
What About 5 7 2 ?
  • expression term plusminus term
  • term factor multdiv factor
  • factor number ( expression )
  • FAILS!

5
What 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

6
New Grammar
  • expression term plusminus term
  • term factor multdiv factor
  • factor number ( expression )

7
Finite 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

8
Examples
  • 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

9
Sample 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?

10
Model
  • 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
11
State 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
12
Transition Both Ways
g
MWGC -
WC - MG
g
13
Some States Bad!
c
MWGC -
WG - MC
  • We wont draw those

14
Diagram of Solutions
  • Can draw a diagram of solutions
  • and transitions
  • that dont lead to bad states

15
Want To Try?
16
Getting Started
g
MWGC -
WC - MG
g
Start
- MWGC
17
State 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
18
Some Differences
  • Not a typical state machine
  • This problem has symmetric transitions
  • Often not the case
  • Only one ending state
  • Often many accepting states

19
Finite 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

20
Recognizing 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
21
Example
_ 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
22
Example
_ 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
23
Example
_ 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
24
Example
_ 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
25
Example
_ 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
26
Example 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
27
Example 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
28
Example 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
29
Example 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
30
Example 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
31
Example 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
32
Example 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
33
Example 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
34
Example 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
35
Example 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
36
How To Implement
  • Case statements
  • Tables

37
Case 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

38
Start 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

39
Digit Case
  • case DIGIT
  • if(c ' ')
  • state AFTER
  • else
  • if(Character.isDigit(c))
  • state DIGIT
  • else
  • state NG
  • break

40
NG Case
  • case NG
  • default
  • state NG
  • break

41
Table for Example
Table is just a very small array
42
Limits 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

43
Summary
  • 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

44
References
  • Introduction to Automata Theory, Languages, and
    Computation by Hopcroft and Ullman
  • Introduction to Algorithms by Cormen, Leiserson,
    and Rivest

45
Next Time
  • 3D Computer Graphics
  • Movies!
Write a Comment
User Comments (0)
About PowerShow.com