You are on page 1of 22

Module #16 – Finite State Machines

Finite State Machines


Lecture 8
Ref: Wikipedia, Rosen Book

1
Module #16 – Finite State Machines

Finite State Machines (FSM)


Model machines (or components in a
computer) using a particular structure
Classification
 FSM with no output: determine whether the input
is accepted (recognized) or not
 FSM with output: generate output from the given
input
Representations
 State diagram
 State transition table

2
Module #16 – Finite State Machines

FSM with Output


Definition: M = (S, I, O, f, g, s0)
 S: finite set of states
 I, O: input/output alphabets
 f: transition function, f: S  I  S
 g: output function
 s0: initial state

3
Module #16 – Finite State Machines

Simple Example

4
Module #16 – Finite State Machines

Generating Output
Input string: x = x1x2…xk
s1 = f(s0, x1), s2 = f(s1, x2), …, sk = f(sk-1,
xThese
k) state transition produces output
y = y1y2…yk
y1 = g(s0, x1), y2 = g(s1, x2), …, yk = g(sk-
1, xk)

5
Module #16 – Finite State Machines

Another Example

start

Input: 101011
Output: 001000
6
Module #16 – Finite State Machines

Ex: vending machines

[description]
State table

7
Module #16 – Finite State Machines

Vending Machine: state diagram

8
Module #16 – Finite State Machines

Ex: Unit Delay Machine


Input: x1x2…xk; Output: 0x1x2…xk-1

f g
state
0 1 0 1
s0 s2 s1 0 0
s1 s2 s1 1 1
s2 s2 s1 0 0
Example & how the machine is
designed (p.755) 9
Module #16 – Finite State Machines

FSM with No Output


Language recognition; design and
implementation of compilers
 A string is recognized IFF it takes the
starting state to one of the final states
Def: Finite-state Automata: do not
produce output, but have a set of final
states

10
Ex: automaton to recognize
Module #16 – Finite State Machines

“nice”

11
Module #16 – Finite State Machines

Finite-State Automata
M = (S, I, f, s0, F)
 S: finite set of states
 I: input alphabets
 f: transition function, f: S  I  S
 s0: initial state
 F: final states, subset of S (indicated by
double circles)

12
Module #16 – Finite State Machines
Example: construct the state
diagram

13
Module #16 – Finite State Machines

Recognizing Language
A string x is said to be recognized by
the machine M = (S, I, f, s0, F) if f(s0, x)
is a state in F
The language recognized (accepted) by
the machine M, denoted by L(M), is the
set of all strings accepted by M.

14
Module #16 – Finite State Machines
Deterministic
Example finite automaton

The following example is of a DFA M,


with a binary alphabet, which
determines if the input contains an
even number of 0s

Start

15
Module #16 – Finite State Machines
Transformations from/to
State Diagram
It is possible to draw a state diagram from the table.
Draw the circles to represent the states given.
For each of the states, scan across the corresponding
row and draw an arrow to the destination state(s).
There can be multiple arrows for an input character if
the automaton is an NFA.
Designate a state as the start state. The start state is
given in the formal definition of the automaton.
Designate one or more states as accept state. This is
also given in the formal definition.

16
Module #16 – Finite State Machines

Example

17
Module #16 – Finite State Machines

Example (cont)

18
Module #16 – Finite State Machines

Example (cont)

19
Module #16 – Finite State Machines
Exercises

20
Module #16 – Finite State Machines
Exercises

21
Module #16 – Finite State Machines
Solutions
16. M16 can recognize any string starts with 1 and followed by any string (1x) where x is any
string. It also can recognize any string starts with 0 and followed by 1 n (1111) followed by 0.
so L(M16)={1x, 01n0, n=0,1,2,….}

17. M17 can recognize any string starts with 1 followed by 1 or 0 followed by any string, in
other words (1(1|0)x). It also can recognize any string starts with 0 and followed by any
string. So L(M17)={1(1|0)x, 0x, n=1,0 , x= 0,1,2,…..,| means OR}

18. M18 can recognize any string starts with 0 and followed by any number of ones, for
example 01111…., so L(M18)={01n, n=1,2,3,…..}

19. M19 can recognize any string starts with any number of zeros and ends with any number
of 1’s. for example 00000111111, 00011, etc, so L(M19)={0n1m, n=1,2,3….,m=1,2,3}

20. M20 can recognize any string with all zeros, and any string starts with as many zeros as
possible and ending with (1 followed by 1 or zeoro followed by any string), L(M20)={0n, 0n1(1|
0)x, x=0,1,2,3, …| means OR}

21. M21 can recognize any string starts with 0 followed by many 1’s followed by 0, such as
0111110, the previous pattern i.e 0111110, can be followed by (many zeros followed by 1)or
followed by single 1, for example 0111110 00001x or 0111110 1x, it also can recognize any
string starts with 1 followed by (1 or zero) followed by any string. So L(M21)={01n0,
01n00n1x, 01n01x,1(1|0)x, x=0,1,2,3…., n=1,2,3,…}
22

You might also like