Finite-State Machines

advertisement
King ABDUL AZIZ University
Faculty Of Computing and
Information Technology
CPCS 222
Discrete Structures I
Modeling Computation
Finite-State Machines
Dr. Eng. Farag Elnagahy
farahelnagahy@hotmail.com
Office Phone: 67967
Finite-State Machines (FSM)
finite-state machine is one type of structures that
used in models of computation.
We will study two types of finite-state machines
 finite-state machine with output
 finite-state machine with no output (has final state)
FSM is Used to model many kinds of machines
o Vending machines
o Delay machines
o Binary adders
o Language recognizer
o Spell checking
o Recognizing speech, etc.
2
Finite-State Machines with Outputs
A finite-state machine with output
M = ( S, I, O, f, g, s0 )
consists of:
 A finite set S of states
 A finite input alphabet I
 A finite output alphabet O
 A transition function f that assigns to each sate
and input pair a new state
 An output function g that assigns to each sate
and input pair an output
 An initial state s0
3
Finite-State Machines with Outputs
The following state table describes a finite-state
machine with S={s0,s1,s2,s3} , I={0,1} and O={0,1} the
values of the transition function f are displayed in the
first two columns, and the values of the output
function g are displayed in the last two columns.
State table
State diagram
4
Finite-State Machines with Outputs
Construct the state table for the finite-state machine
with the following state diagram.
5
Finite-State Machines with Outputs
Find the output string generated by the following
finite-state machine if the input string is 101011.
The obtained output is 001000 as shown in the table.
6
Finite-State Machines with Outputs
Unit-Delay Machine produces as output the input string
delayed by a specific amount of time.
Input: x1x2x3……..xn
output: 0x1x2x3……..xn-1
Previous
input
is 1
Previous
input
is 0
Unit-Delay Machine
7
Finite-State Machines with Outputs
Construct a finite-state machine that adds two
integers using their binary expansion.
Input1: (xn……..x1x0)2 Input2: (yn……..y1y0)2
For simplicity assumed that xn=yn=0
Previous
carry
is 0
A finite-state machine for addition
Previous
carry
is 1
8
Finite-State Machines with Outputs
Construct a finite-state machine that gives a 1 as its
current output bit if and only if the last three bits
received are all 1s.
9
Exercises
pp. 802-803
1-6
10
Finite-State Machines with no Outputs
 Finite-state machine with no output is called finitestate automaton (plural is automata)
 This machine does not produce output, but it has a
set for final states.
 The final states are indicated in state diagrams by
using double circles.
11
Finite-State Machines with no Outputs
A finite-state automaton M = ( S, I, f, s0 , F )
consists of:
 A finite set S of states
 A finite input alphabet I
 A transition function f that assigns a next state
to every pair of state and input (so that
f: SxIS)
 An initial state (start state) s0 and
 A subset F of S consisting of final states
(accepting states)
12
Finite-State Machines with no Outputs
Construct the state diagram for the finite-state
automaton M = ( S, I, f, s0 , F ) where S={s0,s1,s2,s3}
, I={0,1} ,F={s0,s3}, and the transition function f is
given in the following table.
13
Finite-State Machines with no Outputs
Set of strings
Suppose that A and B are subsets of V*, where V is a
vocabulary. The concatenation of A and B, denoted by
AB, is the set of all strings of the form xy, where x
is a string in A and y is a string in B.
V* is the set of all words over v.
Example A={0,11} and B={1,10,110} find AB and BA.
AB={01,010,0110,111,1110,11110}
BA={10,111,100,1011,1100,11011}
14
Finite-State Machines with no Outputs
The empty string or null string (), is the string
containing no symbol.
 is different from  (empty set)
An n=0,1,2,….. concatenation AAAAAAAAA
A0={}
An+1= An A
Example
A={1,00} find An for n=0,1,and 3.
A0={}
A1= A0 A= {}A= {1,00}
A2 =A1 A={11,100,001,0000}
A3 = A2 A={111,1100,1001,10000,0011,00100,00001,
000000}
15
Finite-State Machines with no Outputs
Language recognition by finite-state machines
 A string x is said to be recognized or accepted by
the machine M = ( S, I, f, s0 , F ) if it takes the
initial state s0 to a final state,that is, f(s0,x) is a
state in F.
 The Language recognized or accepted by the machine
M, denoted by L(M), is the set of all strings that are
recognized by M.
 Two finite-state automata are called equivalent if
they recognize the same language.
16
Finite-State Machines with no Outputs
Language recognition by finite-state machines
Determine the languages recognized by the finite-state
automata M1, and M2 in the following figure.
M1
L(M1)={1n |n=0,1,2,…}
Concatenations 111111
M2
L(M2)={1,01}
17
Finite-State Machines with no Outputs
Examp1: Construct a deterministic finite-state
automaton that recognizes the set of all bit strings
such that the first bit is 0 and all remaining bits are
1’s.
18
Finite-State Machines with no Outputs
Examp2: Construct a deterministic finite-state
automaton that recognizes the set of all bit strings
that contain exactly one 0.
19
Finite-State Machines with no Outputs
Examp3: Determine the set of bit strings recognized
by the following deterministic finite-state automaton.
If the bit string ends in 0, you end in state s2. If the
bit string ends in 1, you end in state s1. Therefore,
this automaton recognizes all bit strings that end in 0.
20
Finite-State Machines with no Outputs
Examp4: Determine the set of bit strings recognized
by the following deterministic finite-state automaton.
If the bit string has two consecutive 0’s or two
consecutive 1’s, you end in state s3.
If the bit string has no two consecutive 0’s or two
consecutive 1’s, you end in either state s1 or s2.
Therefore, this automaton recognizes all bit strings
that alternate 0’s and 1’s.
21
Finite-State Machines with no Outputs
Examp5: Determine the set of bit strings recognized
by the following deterministic finite-state automaton.
The string must end in 01 in order to be recognized
by this automaton. If the string ends in 11, the
string ends in state s0. If the string ends in 0, the
string ends in state s1.
22
Finite-State Machines with no Outputs
Types of finite-state automata
Deterministic
For each pair of state and input value there is a unique
next state given by transition function.
Nondeterministic
For each pair of state and input value there may be
several possible next states given by transition
function.
23
Finite-State Machines with no Outputs
Find the state diagram for the nondeterministic finitestate automaton with the following state table.
24
Finite-State Machines with no Outputs
Find the state diagram for the nondeterministic finitestate automaton with the following state table.
25
Exercises
pp. 814-817
1
11-12
16-22
23-24
30-31
26
Download