Toc Unit 1 Finite Automata
Toc Unit 1 Finite Automata
UNIT 1
Examples:
⯍ Adding two numbers in our brains, on a piece of paper or using
a calculator.
Computation
4
Examples:
⯍ Adding two numbers in our brains, on a piece of paper or using
a calculator.
Examples:
Adding two numbers in our brains, on a piece of paper or using
a calculator.
Converting a decimal number to its binary presentation or vise
versa.
Theory gives us
⚫ Concepts
⚫ Principles
⚫ Steps
⚫ Limitations
⚫ Advantages
⚫ etc
Theory of Computation
7
Computation seeks:
A more tangible definition for the intuitive notion of
algorithm which results in a more concrete definition for
computation.
Theory of Computation
8
Computation seeks:
A more tangible definition for the intuitive notion of
algorithm which results in a more concrete definition for
computation.
Language consist of
1. Symbols
2. Alphabets
3. Strings
Strings and Languages
12
Symbols: are Building blocks of language.
Ex. {a, b, c, d, e,…… 1, 2, 3, 4,……., A, B, C, D……..}
Alphabet: Finite, nonempty set of symbols
Examples:
= {0, 1}: binary alphabet
= {a, b, c, …, z}: the set of all lower case letters
The set of all ASCII characters
Reverse : wR
If w = abc, wR = cba
Strings and Languages
14
17
Example: = {0, 1}
0 =set of all string of length zero =1
1 = set of all string of length 1 = = {0, 1} =21
2 = set of all string of length 2
= {0, 1}. {0, 1} = {00, 01, 10, 11} =22
3 = set of all string of length 3 = 23
2 = {00, 01, 10, 11} . {0, 1} = {000, 010, 100, 110, 001,
011, 101, 111}
Kleene Closure/ Power of an (∑) Alphabet
18
⚫ The set of all possible strings over is called Kleene Closure denoted by
*
*= n= = 0 1 2 3 …
⚫ Positive Closure
+ = 1 2 3 …
* = + or + +
+ = * -
Strings and Languages
19
Language: set of strings chosen from some alphabet
⚫ A language is a subset of *
Example of languages:
⚫ Empty language: = { }
Note: {e}
⚫ Star-closure
L* = L0 L1 L2 ...
L+ = L* - L0
Strings and Languages
22
OPERATION DEFINITION
union of L and M L M = {s | s is in L or s is in M}
written L M
concatenation of L LM = {st | s is in L and t is in M}
and M written LM
Kleene closure of L
L* = L0 L1 L2 ...
written as L*
L* denotes “zero or more concatenations of “ L
positive closure of L
written as L+ L+ = L1 L2 ...
L+ denotes “one or more concatenations of “ L
L+ = LL*
Strings and Languages (Example)
23
aa, ab L
aaaa, aaab, aaba ,aabb ,abaa, abab, abba, abbb L
baa L
aL
Strings and Languages (Example)
24
immediately preceded by a
eL
ab L
abaabab L
bb L
bab L
abb L
Strings and Languages (Example)
25
⚫ Let X = {a,b,c} and Y = {abb, ba}. Then
X0 = {e}
X1 = X = {a,b,c}
X2 = XX = {aa,ab,ac,ba,bb,bc,ca,cb,cc}
X3 = XXX =
{aaa,aab,aac,aba,abb,abc,aca,acb,acc,baa,bab,bac,bba,bbb,
bbc,bca,bcb,bcc,caa,cab,cac,cba,cbb,cbc,cca,ccb,ccc}
Strings and Languages (Example)
26
⚫ The language L = {a,b} consists of the strings over {a,b} that contain the
substring bb
bb L
abb L
bbb L
aabb L
bbaaa L
bbabba L
abab L
bab L
bL
Strings and Languages (Example)
27
Let L be the language that consists of all strings that begin
with aa or end with bb
L1 = {aa}{a,b}*
L2 = {a,b}*{bb}
L = L1 L2 = {aa}{a,b}* {a,b}*{bb}
bb L
abb L
bbb L
aabb L
bbaaa L
bbabba L
abab L
bab L
ba L
Finite Representations of Languages
28
Math descriptions are very general, but it may be hard to know if a string
is in the language.
There are uncountably many languages over a nonempty set ∑ but only
Input file
Control Unit
Each cell can hold a symbol from an alphabet (which can be different from the input alphabet).
Finite Automata(FA) is the simplest machine to recognize patterns. The finite automata or finite state
machine is an abstract machine which have five elements or tuple. It has a set of states and rules for
moving from one state to another but it depends upon the applied input symbol. Basically it is an
abstract model of digital computer. Following figure shows some essential features of a general
automation.
To understand consider the example. Consider the two states q0, q1. q0 is a Initial state while q1 is final
state and if the input b is applied then its state is changes from q0 to q1 or q1 to q0. for input a it remains
in the same state.
Input file
a b
a
q a b b a a
0 q1
b
Control Unit
Q0bQ1
Q0aQ0
Q1bQ0
Q1aq1 Storage
Output
Accepted / Rejected
Basic Machine Vs Finite Automata
42
Machine is a system in which energy, material or information can be transformed without any
human interaction.
Ex. Photo printing machine, metal cutting machine etc.
But it is very difficult to incorporate decision making power in machine using mechanical control
system.
FA is mathematical model for actual physical process and it can be viewed as a several restricted
model of computer.
A finite automaton (FA) is used for solving some common types of algorithms such as.
1. Design of digital circuit
2. String Matching
3. Communication protocols for information exchange
4. Lexical analyzer of a compiler
Limitations of Finite Automata
43
In DFA, for each input symbol, one can determine the single state to
which the machine will move. Hence, it is called Deterministic
Automaton.
As it has a finite number of states, the machine is
called Deterministic Finite Machine or Deterministic Finite
Automaton.
a a b
b c a
c b c
Intermediate
c State
Final State
Example on Counting of Symbols (DFA)
48
Ex 1. Give DFA accepting the following language over the alphabet {0,1}
1. Number of 1’s is multiple of 3.
2. Number of 1’s not multiple of 3.
Solution:
1. Number of 1’s is multiple of 3: number of 1’s seen so far by machine can be
written as
i. 3n ii. 3(n+1) iii. 3(n+2)
Corresponding to three states mentioned above there will be 3 states.
⚫ State q0- no of 1’s so far is 3n
⚫ State q1- no of 1’s so far is 3n+1
⚫ State q2- no of 1’s so far is 3n+2
Possible transactions that can cause are
⚫ q0 to q1 if machine is in q0
⚫ q1 to q2 if machine is in q1
⚫ q2 to q0 if machine is in q2
⚫ An input of 0 will cause transaction to same state.
Example on Counting of Symbols (DFA)
Solution: 1. Number of 1’s is multiple of 3.
0 0 0
1 1
q0 q1 q2
1
Input 0 1
Symbols
q0* q0 q1
q1 q1 q2
q2 q2 q0
Input 0 1
Symbols
q0* q0 q1
q1 q1 q2
q2 q2 q0
q1, q2 are final states. Machine will be in final state if number of 1’s read not in Multiple
of 3n. Thus L= { w {0,1} | no of 1’s in multiple of 3. }
51
Example on Substring (DFA)
Ex 1. Draw DFA for the following language over the alphabet {a,b}
1. All strings starting with abb.
2. All strings with abb as substring.
3. All strings ending in abb.
Solution:
1. All strings starting with abb.
• First input b will take the machine to failure State.
• First two input as “aa” will take the machine to failure State.
• First three input as “aba” will take the machine to failure State.
• First three input as “abb” will take the machine to final State.
• So consider the states q0, q1, q2 and q3.
• Where q0 is initial state and q3 will be the final state.
• Failure state will be represented by qφ
• SO DFA can be constructed as
52
Example on Substring (DFA)
a, b
a b b
q0 q1 q2 q3
a
b
a
qφ Input a b
Symbols
a, b q0 q1 qφ
State Transition Diagram q1 qφ q2
q2 qφ q3
q3* q3 q3
qφ qφ qφ
• q0 is the initial state First input a will take the machine to q1 State.
• For state q1 preceding character is a and required ab to complete abb, so
let us take b input to reach next State q2.
• For state q2 preceding characters are “ab” and required b to complete
abb, so let us take b input to reach next State q3.
• A input b to state q0 will bring it to same state.
• A input a to state q1 will bring it to same state.
• A input a to state q2 will bring it to q1 state.
• Q3 is the final state.
• So let us draw State Transition Diagram and State Transition table.
54
Example on Substring (DFA)
b a a, b
a b b
q0 q1 q2 q3
a
Input a b
Symbols
q0 q1 q0
State Transition Diagram q1 q1 q2
q2 q1 q3
q3* q3 q3
• q0 is the initial state First input a will take the machine to q1 State.
• For state q1 preceding character is a and required ab to complete abb, so
let us take b input to reach next State q2.
• For state q2 preceding characters are “ab” and required b to complete
abb, so let us take b input to reach next State q3.
• A input b to state q0 will bring it to same state.
• A input a to state q1 will bring it to same state.
• A input a to state q2 will bring it to q1 state.
• A input a to state q3 will bring it to q1 state.
• A input b to state q3 will bring it to q0 state.
• Q3 is the final state.
• So let us draw State Transition Diagram and State Transition table.
56
Example on Substring (DFA)
a
b a
a b b
q0 q1 q2 q3
b a
Input a b
Symbols
q0 q1 q0
State Transition Diagram q1 q1 q2
q2 q1 q3
q3* q1 q0
• q0 is the initial state First input C will take the machine to q1 State.
• For state q1 preceding character is C and required AT to complete CAT, so
let us take A input to reach next State q2.
• For state q2 preceding characters are “CA” and required T to complete
CAT, so let us take T input to reach next State q3.
• Any input (A,H,I,O,T,R) other than C to state q0 will bring it to same state.
• An input C to state q1 will bring it to same state.
• An input C to state q2 will bring it to q1 state and Any input (H,I,O,T,R)
other than C & T will bring it to q0 state .
• Any input to state q3 will bring it to same state.
• Q3 is the final state.
• So let us draw State Transition Diagram and State Transition table.
58
Example on Substring (DFA)
A,C,H,I,O,T,R
C a
A,H,I,O,T,R
H,I,O,T,R
State Transition Diagram
A T
q0 q1 q2 q3
C
A,H,I,O,R C
Input C H A R I O T
Symbols
q0 q1 q0 q0 q0 q0 q0 q0
q1 q1 q0 q2 q0 q0 q0 q0
q2 q1 q0 q0 q0 q0 q0 q3
q3* q3 q3 q3 q3 q3 q3 q3
59
Example on Divisibility(DFA)
Ex. 1: Design DFA Which can accept a binary number divisible by 3.
Solution:
A binary number is divisible by 3 if the reminder is 0. So we must derive
the machine to determine the final reminder.
The binary number system having only two symbols {0, 1}.
1 Associated state q1
2 Associated state q2
60
Example on Divisibility(DFA)
Ex. 1: Design DFA Which can accept a binary number divisible by 3.
Solution:
61
Example on Divisibility(DFA)
Ex. 1: Design DFA Which can accept a binary number divisible by 3.
Solution:
So now let us draw the DFA for the given problem.
0 0 1
1 0
q0 q1 q2
Input 0 1
Symbols
q0* q0 q1
q1 q2 q0
q2 q1 q2
62
Example on Divisibility(DFA)
Ex. 2: Design DFA Which can accept a Decimal number divisible by 3.
Solution:
A number is divisible by 3 if the reminder is 0. So we must derive the
machine to determine the final reminder.
∑ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
1 Associated state q1
2 Associated state q2
63
Example on Divisibility(DFA)
Ex. 2: Design DFA Which can accept a Decimal number divisible by 3.
Solution:
• As there are only three possible reminders so consider the transitions for
only three inputs
– 0 for (0, 3, 6, 9)
– 1 for (1, 4, 7)
– 2 for (2, 5, 8)
• Now let us draw the table to derive the reminders for decimal number.
64
Example on Divisibility(DFA)
Ex. 1: Design DFA Which can accept a decimal number divisible by 3.
Solution:
Input Decimal Number Division of Binary Reminder Input ends in
Number By 3 state
0 0 0%3 0 q0
1 1 1%3 1 q1
2 2 2%3 2 q2
0 3 3%3 0 q0
1 4 4%3 1 q1
2 5 5%3 2 q2
0 6 6%3 0 q0
1 7 7%3 1 q1
2 8 8%3 2 q2
0 9 9%3 0 q0
1 10 10 % 3 1 q1
65
Example on Divisibility(DFA)
Ex. 1: Design DFA Which can accept a decimal number divisible by 3.
Solution:
So now let us draw the DFA for the given problem.
2 2
0 0
1 1
q0 q1 q2
0
2
1
Input 0 1 2
Symbols
q0* q0 q1 q2
q1 q1 q2 q0
q2 q2 q0 q1
66
Non Deterministic Finite Automata (NFA)
In NFA, for each input symbol, one can determine the multiple states to
which the machine will move. Hence, it is called Non Deterministic
Automaton.
In NFA, FA can reside in multiple states at same time for single input
symbol.
As it has a finite number of states, the machine is called Deterministic
Finite Machine or Deterministic Finite Automaton.
67
Example of NFA
0,1 0, 1
1 0
q0 q1 q2
Input 0 1
Symbols
q0 {q0} {q0,q1}
q1 {q2} φ
q2* {q2} {q2}
68
Processing of String by NFA
A string w ∈ ∑* is accepted by NFA M if δ* =(q0,w) containing a final state. Let us
See how the String 011010 is processed by given NFA.
0,1
1 0
q0 q1 q2
69
Processing of String by NFA 011010
{q0}
0 1 1 0 1 0 0
q0 q0 q0 q0 q0 q0 q0
{q0}
1 1
1 1
q1 q1 q1
1
{q0, q1}
0
0
1
φ q2 q2
{q0, q1, φ}
0 1
0
φ φ
1 0 {q0, q2, φ}
φ φ 1
0
{q0, q1, φ}
φ
0
70 {q0, q2, φ}
Non Deterministic Finite Automata (NFA)
On input 011010 the automata can take any of the following four Path.
0 1 1 0 1 0
1. q0 q0 q0 q0 q0 q0 q0
0 1 1 0 1 0
2. q0 q0 q0 q0 q0 q1 q2
0 1 1 0 1 0
3. q0 q0 q0 q1 q2 φ φ
0 1 1 0 1 0
4. q0 q0 q1 φ φ φ φ
So from above we can conclude that the given string is accepted as one of the solution (2)
Is ending to final state. Also every string ending with 10 is accepted by NFA.
71
Examples on NFA
EX.1 Draw NFA to accept strings containing the substring 00101.
0,1 0 0,1
0 1 0 1
q0 q1 q2 q3 q4
a b b
q0 q1 q2 q3
a
State Transition Diagram
Input a b
• To reach the final string from q0 to final Symbols
state q4 a substring aab is required. q0 {q0,q1} {q0}
•Any string containing sub string aab is q1 {q1} {q2}
accepted by above NFA.
q2 q1 {q3}
q3* φ φ
a a, b a, b
q3
a
q1 q2 q4 q5
3. δ*(1, abaab)= δ* (δ(1, a), baab) =δ* ((1, 2), baab)= δ* (δ(1,2),b),aab)
= δ* (1, 3), aab)
= δ* (1, 2, 4), ab)
= δ (1, 2, 3, 5), b)
= {1, 3, 4, 5}
75
NFA to DFA Conversion
The conversion of NFA to equivalent DFA is based on Subset construction. If a NFA consists
of three states, Q= {q0,q1,q2}
The Machine could be in any of the following states:
1. φ
2. (q0), (q1), (q2)
3. (q0, q1), (q1, q2), (q0, q2)
4. (q0, q1, q2)
The no of possible states are 2Q
The procedure of finding whether the given string is accepted by NFA includes following
steps.
Step 2: Add q0 of NFA to Q'. Then find the transitions from this start state.
Step 3: In Q', find the possible set of states for each input symbol. If this set of states is not in
Q', then add it to Q'.
Step 4: In DFA, the final state will be all the states which contain F(final states of NFA)
77
NFA to DFA Conversion Example-1
Ex. 1: Construct the Equivalent DFA from given NFA.
0
Input 0 1
Symbols
q0 q2
0, 1 q0 {q2} φ
q1 φ {q0, q2}
1 1 q2 {q0, q1} {q0}
0
q1
78
NFA to DFA Conversion Example-1
Step 1: Starting State {q0} is the first sub state.
O successor for q0 is q2 i.e δ: (q0, 0)→ q2
1 successor for q0 is qφ i.e δ: (q0, 1)→ qφ
0
q0 q2
Input 0 1
Symbols
{q0} {q2} φ
1
φ
79
NFA to DFA Conversion Example-1
Step 2: Consider sub State {q2} is the recently generated sub state.
O successor for q2 is q2 i.e δ: (q0, 0)→ (q0, q1)
1 successor for q2 is qφ i.e δ: (q0, 1)→ q0
1
0 Input 0 1
{q0} {q2} Symbols
{q0} {q2} φ
1
0 {q2} {q0, q1} {q0}
φ
{q0,q1}
So in this step new sub state {q0, q1} is generated so find the successor for {q0, q1}
80
NFA to DFA Conversion Example-1
Step 3: Consider sub State {q0, q1} is the recently generated sub state.
O successor for {q0, q1} is q2 i.e δ: ((q0,q1), 0)→ (q2)
1 successor for {q0, q1} are q0, q2 i.e δ: ((q0,q1), 1)→ (q0, q2)
1
0 Input 0 1
{q0} {q2} Symbols
{q0} {q2} φ
1 0
{q2} {q0, q1} {q0}
φ {q0, q1} {q2} {q0, q2}
0
{q0,q2} {q0,q1}
1
So in this step new sub state {q0, q2} is generated so find the successor for {q0, q2}
81
NFA to DFA Conversion Example-1
Step 4: Consider sub State {q0, q2} is the recently generated sub state.
O successor for {q0, q2} are q0, q1, q2 i.e δ: ((q0,q2), 0)→ (q0, q1, q2)
1 successor for {q0, q2} is q0 i.e δ: ((q0,q2), 1)→ (q0)
1
0 Input 0 1
{q0} {q2} Symbols
{q0} {q2} φ
1 0
{q2} {q0, q1} {q0}
φ {q0, q1} {q2} {q0, q2}
1 1
{q0, q2} {q0, q1, q2} {q0}
{q0,q2} {q0,q1}
1
0
So in this step new sub state {q0, q1, q2} is generated so find
the successor for {q0, q1, q2}
{q0, q1, q2}
Prepared by r. Vipin Wani 82
NFA to DFA Conversion Example-1
Step 5: Consider sub State {q0, q1, q2} is the recently generated sub state.
O successor for {q0, q1, q2} are q0, q1, q2 i.e δ: ((q0, q1, q2), 0)→ (q0, q1, q2)
1 successor for {q0, q1, q2} are q0, q2 i.e δ: ((q0, q1, q2), 1)→ (q0, q2)
1
0 Input 0 1
{q0} {q2} Symbols
{q0} {q2} φ
1 0
{q2} {q0, q1} {q0}
φ {q0, q1} {q2} {q0, q2}
1 1
{q0, q2} {q0, q1, q2} {q0}
{q0,q2} {q0,q1} {q0, q1, q2} {q0, q1, q2} {q0, q2}
1
0
1 As now no new sub state is generated so the process of sub set
generation stops.
{q0, q1, q2}
Prepared by r. Vipin Wani 0 83
NFA to DFA Conversion Example-1
Step 6: As the q2 is the final state in NFA so every sub state containing q2 will be
final state in DFA.
0 Input 0 1
{q0} {q2} Symbols
{q0} {q2} φ
1 0
{q2}* {q0, q1} {q0}
φ {q0, q1} {q2} {q0, q2}
1 1
{q0, q2}* {q0, q1, q2} {q0}
{q0,q2} {q0,q1} {q0, q1, q2}* {q0, q1, q2} {q0, q2}
1
0
1
This is the final DFA after representing Final states.
{q0, q1, q2}
Prepared by r. V pin Wani 0 84
NFA to DFA Conversion Example-2
Ex. 2: Construct the Equivalent DFA from given NFA.
Input 0 1
Symbols
p {p, q} {p}
q {r} {r}
r {s} φ
s* {s} {s}
85
NFA to DFA Conversion Example-2
Input 0 1
Symbols
{p, q} {p, q, r} {p, r} A new subset {p, q, r} & {p, r} are generated
0 0
1
{p, q, r}
1
0
0
1
{p, r, s}
{p, q, r, s}
1 0
0
{p, s}
1
87
NFA to DFA Conversion Example-3
Ex. 3: Construct the Equivalent DFA from given NFA.
Input 0 1
Symbols
p {p, q} {p}
q {r, s} {t}
r {p, r} {t}
s* φ φ
t* φ φ
88
NFA to DFA Conversion Example-3
Input 0 1
Symbols
{p, q} {p, q, r, s} {p, t} A new subset {p, q, r, s} & {p, t} are generated
89
NFA to DFA Conversion Example-3
1
0
{p}
{p, q}
1
1
0
1
{p, s}
{p, q, r, s}
90
Construction of NFA & DFA Example-1
Ex. 1: Construct the NFA and then Equivalent DFA accepting strings over {0,1}
For accepting all possible strings of Zero and ones not containing 101 as sub string.
Solution: This problem is fully deterministic. A DFA with φ transitions not mentioned
Is Called as NFA
0 1
1 0
q0 q1 q2
State q0 is initial state which will cause machine to move in state q1 on input1.
State q1 will cause machine to move in state q2 on input 0.
State q2 will cause machine to move in state q1 on input 0 and on input 1 it will move
to failure state φ.
91
Construction of NFA & DFA Example-1
NFA can be converted into DFA by introducing an explicate failure state.
So the equivalent DFA can be constructed as follow
0 1
0, 1
0 1
1
q0 q1 q2 φ
92
Construction of NFA & DFA Example-2
Ex. 1: Construct the NFA and then Equivalent DFA accepting strings over {0,1}
For accepting all possible strings of Zero and ones not containing 011 as sub string.
Solution: This problem is fully deterministic. A DFA with φ transitions not mentioned
Is Called as NFA
1 0
0 1
q0 q1 q2
State q0 is initial state which will cause machine to move in state q1 on input 0.
State q1 will cause machine to move in state q2 on input 1.
State q2 will cause machine to move in state q1 on input 0 and on input 1 it will move
to failure state φ.
93
Construction of NFA & DFA Example-1
NFA can be converted into DFA by introducing an explicate failure state.
So the equivalent DFA can be constructed as follow
1 0
0, 1
0 1 1
q0 q1 q2 φ
94
NFA with Transitions
stands for a null symbol. An transition allows transition on or no input the empty
string . This implies that a Machine can make a transition without any input.
When finding the string describe by path containing arc with label the symbols are
discarded.
The use of transition may specify the transition graph by reducing numbers of labeled arcs.
Steps to remove e transition from NFA.
S1 S2
Consider the transition from state S1 to s2 then follow the following steps to remove
transition .
1. Find all edges starting from S2.
2. Duplicate all edges to S1 without changing the edge label.
3. If s1 is initial state then mark s2 also as initial state.
4. If s2 is final state then mark s1 also as final state.
5. Remove the dead states. 95
NFA with Transitions Example
Ex. 1: Use direct methods to find an equivalent NFA without moves for the given NFA bellow.
0
q0 q1 q2
Solution:
Step 1: Transition from q2 duplicated on q1, and make a q1 as final state.
0
q0 q1 q2
0
q0 q1 q2
NFA with Transitions Example-2
Ex. 2: Use direct methods to find an equivalent NFA without moves for the given NFA bellow.
0 B C
A
E
D
Outgoing transitions from B & D are duplicated on A as δ (A, ) = B andδ (A, ) = D.
0 B C
A
E
D
97
NFA with Transitions Example-2
The state D can be deleted as it don’t have any incoming transitions.
0 B C
A
98
Closures
Give formal definition of closures with suitable example. for a null symbol.
Closures of state qi is the set of states including qi where qi can be reached by
any numbers of moves of given non deterministic finite automata.
Closure of qi
Closure of qi, includes qi
Set of states reachable from qi on moves.
Set of states reachable from existing states in closure using moves and so on .
q2 q3
0
q7
q0 q1 q6
q4 q5
Prepared by
Closures Example
Find Closure for given NFA.
Closures of state qi is the set of states including qi where qi can be reached by
any numbers of moves of given non deterministic finite automata.
so let us determine the Closures for each sub state.
Step 1: Closures of q0= {q0, q1, q2, q4, q7}
The moves from q0 are = {q0 to q1,
q0 to q7,
q1 to q2,
q1 to q4. }
0, 1 0, 1
{q0, q1, q2} { q1, q2} { q2}
103
Construction of NFA & DFA Example
Solution: Construction of DFA.
{q0, q1, q2} { q1, q2} { q2}
qφ
The State {q0, q1, q3 } and {q0, q2, q3 } are not repeatable, as
they are having common outgoing behavior.
{ q0, q1}
{q0} { q0, q3}
104 { q0, q2}
Deference Between NFA & DFA
DFA NFA
DFA is Deterministic Finite Automata. NFA is Nondeterministic Finite Automata.
For each symbolic representation of the For each symbolic representation of the
alphabet, there is only one state transition in alphabet, there are multiple state transition in
DFA. NFA.
DFA cannot use Empty String transition. NFA can use Empty String transition.
NFA can be understood as multiple little
DFA can be understood as one machine.
machines computing at the same time.
In NFA, each pair of state and input symbol
In DFA, the next possible state is distinctly set.
can have many possible next states.
DFA is more difficult to construct. NFA is easier to construct.
Time needed for executing an input string is Time needed for executing an input string is
less. more.
All DFA are NFA. Not all NFA are DFA.
DFA requires more space. NFA requires less space then DFA.
It is easy to determine weather w ∈ L as It is difficult to determine weather w ∈ L as
transitions are deterministic. transitions are non deterministic.
105
Finite automata as Output Device
(Moore / Mealy Machine)
Finite automata we have discussed so far had limited functionalities.
It was able to either accept or reject the string based on reachability of machine
from start to final state.
Finite automata can also be used as an output device.
• Such machines don’t have final states.
• Such machines can generate output on every input.
• The value of output is a function of current state and current input.
• This machine can be characterized by two behaviors.
1. State Transition Function (STF ): denoted by δ
δ: ∑ X Q Q
2. Output Function / Machine Function (MAF): denoted by
: ∑ X Q O / : Q O
106
Formal Definition of Mealy Machine
A Mealy machine is a finite-state machine whose output values are determined by
both its current state and the current inputs.
A Mealy machine can be represented by a 6-tuple M=(Q, ∑, O, δ, , q0) where −
1. Q is a finite set of states.
2. ∑ is a finite set of input symbols called the alphabet.
3. O is a finite set of output symbols.
4. δ is the transition function where δ: Q × ∑ → Q
5 is the output function where : Q × ∑ → O
6. q0 is the initial state from where any input is processed (q0 ∈ Q).
a/ q0 a/0
a/
b/
b/ q2 q3
a/ b/
b/
q1
Input a b States a b
Symbols q0 0 0
q0 q0 q1 q1 0 0
q1 q0 q2 q2 1 0
q2 q0 q3 q3 0 0
q3 q3 q3
output function for
State transition function Mealy machine
For Mealy machine
Input Symbols a b
Here in q1 / 0
q0 q0/0 q1/0 q1 is next state on transition.
q1 q0/0 q2/0 0 is a output on applying input.
q2 q0/1 q3/0
q3 q3/0 q3/0
SPtraetpearetdrabyn:sMitr.ioVinpinanWdanoi utput function combi1n0e8dFor Mealy machine
Formal Definition of Moore Machine
A Moore machine is a finite-state machine whose output values are determined
solely by its current state only.
A Mealy machine can be represented by a 5-tuple M=(Q, ∑, O, δ, , q0) where −
1. Q is a finite set of states.
2. ∑ is a finite set of input symbols called the alphabet.
3. O is a finite set of output symbols.
4. δ is the transition function where δ: Q × ∑ → Q
5 is the output function where : Q → O
6. q0 is the initial state from where any input is processed (q0 ∈ Q).
q1/
a
Input 0 1
Symbols
State output
q0/ q0 q1 q2 q0
q1 a
q1 q1 q2
q2/ q2 q1 q2 q2 b
b
State transition function output function for
For Moore machine Moore machine
SPtraetpearetdrabyn:sMitr.ioVinpinDWiaagniram 109
Moore Machine
State table for both transition function (δ) and output function () can be shown as
Input 0 1 output
Symbols
q0 q1 q2
q1 q1 q2 a
q2 q1 q2 b
Output in Moore machine is associated with a state and not with transition.
In more machine length of output string will be equivalent to length of input
string plus one.
i.e. if length of input string is 5 so length of output string will be 5+1=6.
110
Moore / Mealy Machine Examples
Construct moore & mealy machine for the input {0 ,1 ,2} print the residue module 5
Of the input treated as ternary (Base 3).
q0 q1 q2 q3 q4
Output in Moore machine is associated with a state and not with transition.
111
Moore / Mealy Machine Example-1
Ex 1: Construct Moore & mealy machine for the input {0 ,1 ,2} print the residue
module 5 Of the input treated as ternary (Base 3).
nstruct moore & mealy machine for the input {0 ,1 ,2} print the residue module 5
of the input treated as ternary (Base 3).
113
Moore / Mealy Machine Examples-2
Ex 2: Construct mealy machine that gives output of 1 if the input string ends in
“bab”.
a b/0 b/1
b/0 a
q0 q1 q2
a
114
Moore / Mealy Machine Examples-3
Ex 3: Construct Moore machine that gives output of 1 if the input strings ends in
“bab”.
a
a b
b a
q0/ q1/0 q2/0
0
a
b
b
q3/
1
Input a b Output
Meaning of various states. Symbols
• q0 Starting state.
q0 q0 q1 0
• q1 Make the preceding character a.
• q2 generate the preceding character ba q1 q2 q1 0
• q2 to q3 transition will generate the q2 q0 q3 0
preceding character bab so output will
be 1. q3 q2 q1 1
115
Moore / Mealy Machine Examples-4
Ex 4: Construct Moore machine that gives output of 1’s complement of binary
number.
1
1 0
0
q0/
q1/1
0
116
Moore / Mealy Machine Examples-5
Ex 5: Construct Mealy machine that gives output of 2’s complement of binary
number.
0/0 0/1
1/0
1/1
q0 q1
Complement No Change
Prepared by: Mr. Vipin Wani
Every bit 117
Moore / Mealy Machine Examples-6
Ex 6: Design Mealy machine that will read the sentence made up of vowels of
English language it will output in same sequences except in cases where i follows e
it will be converted to u.
a/a
i/i
e/e
o/o
u/u e/e
q0 q1
a/a
i/u
o/o
u/u
118
Moore / Mealy Machine Examples-6
State Transition & Output Function can be shown as bellow.
119
Moore / Mealy Machine Example-7
Ex 7: Design Mealy and Moore machine for the following processes. For input from
(0+1)* if input end in 101 output ‘X’ and if input end in 110 output ‘Y’ , otherwise
output ‘Z’.
1/z
0/z 1/z q2
1/z
q0 q1 0/y
0/z
0/z
1/x q3
Mealy machine
0
0 1
0
0 Q2/Z Q3/X
1
Q0/Z Q1/Z 1
1 0
0
Q4/Z Q5/Y
1
0,1
Moore machine
0/0 q1
q0
0/0 1/0
1/0
q2
1/1
Input Next State /Output
Symbols
0 1 Required Mealy Machine, State transitio n
q0 q1 /0 q2/ 0 and output function as shown here.
q1 q1 /1 q2/ 0
q2 q1 /0 q2/ 1
122
Conversion of Mealy Machine to Moore Machine Example-1
Meaning of various states.
q0- is a initial state.
q1- has preceding input 0 from q0 next input 0 will complete the sequence 00.
q2- has preceding input 1 from q0 next input 1 will complete the sequence 11.
123
Conversion of Mealy Machine to Moore Machine Example-1
Step 2: Conversion from Mealy to Moore Machine. 0
0
q1 /0 q1’ /1
0
0
q0 / 0
1
1
1
q2 / 0 q2’ / 1
1
1
Input Next State Output
Symbols
0 1 Required More Machine, State
q0 q1 q2 transition and output function as
shown here.
q1 q1’ q2 0 If mealy machine having m no of
q1’ q1’ q2 1 states and n output symbols then
possible no of states in resultant
q2 q1 q2’ 0
Moore machine will be mn.
q2’ q1 q2’ 1
124
Conversion of Mealy Machine to Moore Machine Example-2
Ex 2: Convert the given mealy machine to equivalent more machine.
Solution:
Step 1: Splitting of the states: Split the states if the incoming arcs are having different
output.
Split the state q1 into q1 with output 0 and q1’ with output 1.
Split the state q2 into q2 with output 0 and q2’ with output 1.
Split the state q3 into q3 with output 0 and q3’ with output 1.
As all incoming edge to q4 are producing same output so no need to split it will always
produce the output 1.
125
Conversion of Mealy Machine to Moore Machine Example-2
Step 2: Convert to equivalent Moore machine.
1
q1/ q2/0 q2’/1
0, 1
1 0
0
0
0, 1
q1’/1 q3/0 1
q3’/1
1
0 1
0
q4/1
126
Conversion of Mealy Machine to Moore Machine Example-2
Step 2: Construct state transition and output function for Moore machine.
127
Conversion of Moore Machine to Mealy Machine Example-1
Ex1: Convert the given Moore machine in to mealy machine
q0/ q1/0 b
a b
a
b
a
q2/0
q3/1
a
b
q0 b/0
b/0 q1
a/0 Solution: Construction of trivial
mealy machine by moving output
associated with a state to transitions
b/1 a/0
entering that state, we get.
a/1
q3 q2
a/0
b/0
128
Conversion of Moore Machine to Mealy Machine Example-1
Construct state transition and output function for mealy machine.
129
FSM for Vending Machine
A typical vending machine look like as following.
Reset
N
Vending
Coin Release
Machine
Sensor Mechanism
FSM
D
Clock
A vending machine can be designed using FSM model with auto billing. It reduces
the hardware. The typical no of steps included in FSM vending machines are:
1. Selection of product by User.
2. waiting for Insertion of Coin.
3. Product delivery and service.
Entire things can be modeled using mealy machine.
Machine can count the value of inserted coin and take the decision regarding
product release.
FSM can also be used for spelling checking.
130
Mealy Machine Vs Moore Machine.
The following table highlights the points that differentiate a Mealy Machine from a Moore
Machine.
Generally, it has fewer states than Moore Generally, it has more states than Mealy
Machine. Machine.
The value of the output function is a The value of the output function is a
function of the transitions and the changes, function of the current state and the
when the input logic on the present state is changes at the clock edges, whenever state
done. changes occur.
Mealy machines react faster to inputs. They In Moore machines, more logic is required
generally react in the same clock cycle. to decode the outputs resulting in more
circuit delays. They generally react one
clock cycle later.
131
132