Flat Unit I
Flat Unit I
1. Input tape:
The input tape is divided into squares; each square contained a single symbol from input alphabet
∑.
The end squares of each tape contain end markers Ø at left end and $ right end.
Absence of end markers indicates that tape is infinite length.
The left to right sequence of symbols between end markers is the input string to be processed.
1
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
2. Reading head:
The Reading head examines only one square at a time and can move one square towards left till
end of the string.
3. Finite Control:
The FSC is responsible for controlling total functioning of finite automate machine.
It will decide which input symbol is read next and where to move either to the left or right.
The input to the finite control will be usually
Input symbol from input tape
Present state of machine.
The output may be
Movement of Reading head along the tape to the next square or to null move
The next state/new state of FSM
2
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Finite Automata can be represented in two ways:
1: Transition Diagram
2: Transition Table
1: Transition Diagram: Transition diagram contains set of vertices and edges. Here vertices are
represented as no of states and edges are represented as transitions.
Here,
Initial state is represented by circle with arrow.
2: Transition Table: Representation of the transition function in Tabular form is called transition table.
Where rows correspond to states and columns corresponds to input/ next state.
The initial state is given by ->
3
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Acceptance of a String by Finite Automata:
There are two conditions for declaring a string to be accepted by finite automata. The Conditions are
1: The String must be totally traversed.
2: The machine must come to a final state.
It can be said that if C(q0, W) = qn , where W is the string given as input to the finite automata, q 0 is
the Initial state and qn is the final states, then the string W can be said to be accepted by the finite
automata.
If these two conditions are satisfied, then we can declare a string to be accepted by the finite
automata. If any of the conditions are not satisfied, then we can declare a string to be not accepted by a
finite automaton.
Example 1: Test whether the following strings are accepted by the following finite automata or
not.
1: 01010
2: 1010100
3: 1110011
Input
Present State
0 1
->q0 q1 q2
q1 q3 q2
q2 q2 q3
* q3 q3 q0
Solution: i ) 01010
δ(q0,01010) -> δ(q1,1010)
-> δ(q2,010)
-> δ(q2,10)
-> δ(q3,0)
-> q3
Therefore q3 is the final state. So the string 01010 is accepted by the given finite automata.
ii ) 1010100
δ(q0,1010100) -> δ(q2,010100)
-> δ(q2,10100)
-> δ(q3,0100)
-> δ(q3,100)
4
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
-> δ(q0,00)
-> δ(q1,0)
-> q3
Therefore q3 is the final state. So the string 1010100 is accepted by the given finite automata.
iii ) 1110011
δ(q0, 1110011) -> δ(q2, 110011)
-> δ(q3, 10011)
-> δ(q0, 0011)
-> δ(q1, 011)
-> δ(q3, 11)
-> δ(q0, 1)
-> q2
Therefore q2 is not final state. So the string 1110011 is not accepted by the given finite automata.
Example 2 : Test whether the following strings are accepted by the following finite automata or
not.
(i) 100010
(ii) 0001101
(iii) 1000
Input
Present State
0 1
->q0 q3 q1
q1 q0 q2
*q2 q2 q2
* q3 q1 q1
Solution: A) 100010
δ (q0, 100010) -> δ(q1, 00010)
-> δ(q0, 0010)
-> δ(q3, 010)
-> δ(q1, 10)
-> δ(q2, 0)
-> q2
Therefore q2 is the final state. So the string 100010 is accepted by the given finite automata.
5
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
B ) 0001101
δ (q0, 0001101)->δ(q3, 001101)
-> δ (q1, 01101)
-> δ (q0, 1101)
-> δ (q1, 101)
-> δ (q2, 01)
-> δ (q2, 1)
-> q2
Therefore q2 is the final state. So the string 0001101 is accepted by the given finite automata.
C) 1000
δ (q0, 1000) -> δ (q1, 000)
-> δ (q0, 00)
-> δ (q3, 0)
-> q1
Therefore q1 is not final state. So the string 1000 is not accepted by the given finite automata.
Example 3: Consider below transition and verify whether the following string will be accepted or
not.
a) 010101
b) 0011
c) 111100
6
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
The Central Concepts of Automata:
1. Alphabets
2. Strings
3. Languages
Alphabets: In formal language alphabets are denoted by ∑(Sigma). Any finite non-empty set of symbols
is called alphabet.
Example:
∑ = { 0, 1 } --- Binary alphabet.
∑ = { a, b, c … z } -- Lower case alphabet.
∑ = { A, B, C … Z } – Upper case alphabet.
∑ = {0, 1, 2…9} --- Decimal Alphabet.
∑ = { } is not an alphabet, because it is empty
∑ = { 0,1,2…9,A, B,C, D, E, F }—Hexa decimal alphabet.
Powers of an Alphabet: If ∑ is an alphabet, we can express the set of all strings of a certain length from
that alphabet by using an exponential notation.
Power set is denoted by ∑k
Where, k is the length of string.
Example: ∑ = {0, 1}
∑0 = {€} -- the set of string length 0
∑1 = {0, 1} -- the set of string length 1
∑2 = {00, 01, 10, 11} - the set of string length 2
∑3 = {000,001,010,011,100,110,101,111} -- the set of string length 3
Strings: Any finite sequence of symbols over the given alphabet is called a string.
Length of a string: Length of a string is the no of symbols in a given string. If ∑ is a string then length is
denoted by | w |.
Example: ∑ = a, b, c, d, Then length is | w | = 4
∑ = 0, 1, 0 is a binary string, Then length is | w | = 3
∑ = £ is the empty string, Then length is | w | = 0
Empty String: The Empty string is the string with zero occurrences of symbols.This string is denoted by
£.
7
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Prefix of a String: Any sequence of leading (starting) symbols of given string is called as prefix.
Example: w = abc
Proper Prefix: The Prefix without string itself is called as Proper Prefix.
Example: w = abc
Suffix of a String: Any sequence of tailing (ending) symbols of given string is called as suffix.
Example: w = abc
Proper Suffix: The Suffix without string itself is called as Proper Suffix.
Example: w = abc
Substrings of String: A substring of a string is defined as a string formed by taking any number of
symbols of the string.
Example1: ∑ = 012
The possible substrings are “0”, “1”, “2”, “01”, “12”, “012”.
Example2: str = “abcd”
Every substring of the given string : “a”, “b”, “c”, “d”, “ab”, “bc”, “cd”, “abc”, “bcd” and “abcd”
Reverse of the string: if ∑ is a word in some language ‘L’. Then reverse of ∑ is the same string of letters
spelled backward.
Example: ∑ = abc
Then ∑R = cba
8
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Languages: Language is a set of strings oven an alphabet. A language is denoted by ‘ L ‘.
Example: ∑ = {0, 1}
1: Union
2: Concatenation
3: Kleene Closure (or) Kleene Star
4: Kleene Pluse (or) Positive Closure
1: Union: Let L1 and l2 are two languages. By union of these two languages are represented by LI U L2.
Example: L1 = {0, 01, 10, 11, 001}
L2 = {0, 01, 001} Then
L1 U L2 = {0, 01, 10, 11,001}
L2 U L1 = {0, 01, 001, 10, 11}
Therefore L1 U L2 = L2 U L1
2: Concatenation: Concatenation of two languages L1 and L2 are represented by L1L2.
Example: ∑ = {0, 1}
L1 = {00, 01} L2 = {0, 1}
L1.L2 = {00, 01}. {0, 1}
= {000, 001, 010, and 011}
L2.L1 = {0, 1}. {00, 01}
= {000, 001, 100, 101}
Therefore L1. L2 # L2. L1
9
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
3: Kleene Closure (or) Kleene Star: Kleene star is denoted by ∑* . The set ∑* is the infinite set of all
possible strings of all possible lengths over ∑ including £ .
Kleene Star Representation:
∑* = ∑0 U ∑1 U ∑2 U ………..∑α
Example: ∑ = {a, b}
∑* = {£, a, b, aa, ab, ba, bb……………………………………}
4: Kleene Pluse (or) Positive Closure: Kleene Plus is denoted by ∑+ . The set ∑+ is the infinite set of all
possible strings of all possible lengths over ∑ not including £ .
Kleene Pluse Representation:
∑+ = ∑1 U ∑2 U ∑3 ………..∑α
Example: ∑ = {a, b}
∑* = {a, b, aa, ab, ba, bb……………………………………}
10
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
1: DFA (Deterministic Finite Automata):
In Deterministic Finite Automata (DFA) there are unique transition from one state to another
state that automata is called Deterministic Finite Automata (DFA).
In Deterministic Finite Automata (DFA) each and every states should contain exactly one out
degree for each input alphabet symbol.
DFA may have multiple final states.
DFA does not allow any null transitions, meaning every state must have a transition defined for
every input symbol.
Formal Definition of a DFA
DFA contains 5 tuples {Q, Σ, Q, F, δ}.
Where,
Q: Finite set of all states.
Σ: Finite set of input Alphabet.
Q: Initial state.
F: set of final state.
δ: Transition Function,
Defined as δ: Q X Σ –> Q.
A simple example of DFA is given below.
Here
Q = {q0, q1, q2}
Σ= {0, 1 }
q = {q0}
F = {q2}
δ = Q X Σ –> Q.
δ (q0,0) = q1
δ (q0,1) = q0
δ (q1,0) = q1
δ (q1,1) = q2
δ (q2,0) = q1
δ (q2,1) = q1
11
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
DFA PROBLEMS
12
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example1: Deign a DFA which accept a Binary Number divisible by 2 over an alphabet {0, 1}.
Solution: Binary Number, i.e Σ: {0, 1}
Divisible by 2 (0, 1)
2 div 2 = 0
3 div 2 = 1
4 div 2 = 0
5 div 2 = 1
6 div 2 = 0
7 div 2 = 1
L = {0, 01, 10, 11, 100, 101, 110........}
Transition Table Transition Diagram
Present Input
State 0 1
->*q0 q0 q1
q1 q0 q1
Example2: Deign a DFA which accept a Binary Number divisible by 3 over an alphabet {0, 1}.
Solution: Binary Number, i.e Σ: {0, 1}
Divisible by 3 (0, 1, 2)
Solution: L = {0, 01, 10, 11, 100, 101, 110, 1100........}
Transition Tale Transition Diagram
Input
Present State
0 1
->*q0 q0 q1
q1 q2 q0
q2 q1 q2
Example3: Deign a DFA which accept a Binary Number divisible by 4 over an alphabet {0, 1}.
Solution: L = {0, 01, 10, 11, 100, 101, 110........}
Transition Table Transition Diagram
Input
Present State
0 1
->*q0 q0 q1
q1 q2 q3
q2 q0 q1
q3 q2 q3
13
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example4: Deign a DFA which accept a Decimal Number divisible by 3.
Solution: Decimal Number, i.e. Σ: {0, 1,2 ,3,4,5,6,7,8,9}
Decimal Number Divisible by 3 (0, 1, 2)
3 div 3 = 0
4 div 3 = 1
5 div 3 = 2
6 div 3 = 0
7 div 3 = 1
8 div 3 = 2
Transition Table
Present Input / Next State
State 0 1 2 3 4 5 6 7 8 9
->*q0 q0 q1 q2 q0 q1 q2 q0 q1 q2 q0
q1 q1 q2 q0 q1 q2 q0 q1 q2 q0 q1
q2 q2 q0 q1 q2 q0 q1 q2 q0 q1 q2
Transition Diagram
14
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
RULE 1: Calculate the length of substring.
All strings ending with ‘n’ length substring will always require minimum (n+1) states in the DFA.
Example 1. Draw a DFA for the language accepting strings ending with ’01’ over input alphabets
∑ = {0, 1}.
Example 2 Draw a DFA for the language accepting strings ending with ‘abb’ over input alphabets
∑ = {a, b}
Solution: L = { abb, aabb, babb, aaabb, ababb, baabb, bbabb, aaaabb, abbabb……. }
So DFA can be
Q = { q0, q1, q2, q3 }, Σ = { a , b }, q= { q0 }, F = { q3 } and δ : Q X Σ –> Q
The required DFA is-
Example 3. Draw a DFA for the language accepting strings ending with ‘abba’ over input alphabets
∑ ={a, b}.
Solution: L = { abba, aabba, babba, aaabba, ababba, baabba, bbabba, aaaabba, abbabba, abbaabba…. }
So DFA can be
Q = { q0, q1, q2, q3, q4 }, Σ = { a , b }, q= { q0 }, F = { q4 } and δ : Q X Σ –> Q
The required DFA is-
15
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example 4. Draw a DFA for the language accepting strings ending with ‘0011’ over input alphabets
∑ = {0, 1}
Solution: L = { 0011, 00011, 10011, 100011, 010011, 110011, 000011, 0000011, 0100011,…. }
So DFA can be
Q = { q0, q1, q2, q3, q4 }, Σ = { a , b }, q= { q0 }, F = { q4 } and δ : Q X Σ –> Q
16
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example2 Draw a DFA for the language accepting strings starting with ‘ab’ over input alphabets
∑ = {a, b}
Solution: L = { ab, aba, abb, abab, abba, abaa, abbb, abaaa, abbbb, ababa,…… }
So DFA can be
Q = { q0, q1, qf,D }, Σ = { a , b }, q= { q0 }, F = { qf } and δ : Q X Σ –> Q
The required DFA is
Example 3: Draw a DFA for the language accepting strings starting with ‘0’ over input alphabets
∑={0, 1} ?
Solution: L = { 0, 01, 00, 010, 011, 000, 010, 001……}
So DFA can be
Q = { q0, q1, Dead State }, Σ = { a , b }, q= { q0 }, F = { q1 } and δ : Q X Σ –> Q
The required DFA is-
17
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example 4. Draw a DFA that accepts a language L over input alphabets ∑ = {0, 1} such that L is the
set of all strings starting with ’00’.
Solution: L = { 00, 001, 000, 0010, 0011, 0000, 0010, 0001……}
So DFA can be
Q = { q0, q1, qf, D }, Σ = { a , b }, q= { q0 }, F = { qf } and δ : Q X Σ –> Q
Example 5.Draw a DFA for the language accepting strings starting with ‘101’ over input alphabets
∑={0, 1}
Solution: L = { 101,1010, 1011, 10111, 10100, 10110, 10101,…… }
So DFA can be
Q = { q0, q1, q2, qf, D }, Σ = { 0 , 1 }, q= { q0 }, F = { qf } and δ : Q X Σ –> Q
18
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example 6. Construct a DFA that accepts a language L over input alphabets ∑ = {a, b} such that L is
the set of all strings starting with ‘aba’.
Solution: L = {aba,abaa, abab, abaaa, abaab, ababa, ababb,…… }
So DFA can be
Q = { q0, q1, q2, qf, D }, Σ = { a , b }, q= { q0 }, F = { qf } and δ : Q X Σ –> Q
The required DFA is
Example 7. Construct a DFA that accepts a language L over input alphabets ∑ = {a, b} such that L is
the set of all strings starting with ‘aa’ or ‘bb’.
19
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
String Containing
Example 1. Design DFA for a string which accepts a String Containing 10 as its substring over an
Input Symbol ∑ = {0, 1}.
Solution:
L = { 10, 010, 110, 101, 100, 0010, 0110, 1010, 1110, 00010,00110,01110, 11110, ……}
So DFA can be
Q = { 0,1,2,3,4 }, Σ = { 0 , 1 }, q= { 0 }, F = { 4 } and δ : Q X Σ –> Q
The required DFA is
Example 2. Design DFA for a string which accepts a String Containing 101 as its substring over an
Input Symbol ∑ = {0, 1}.
Solution:
L = { 101, 0101, 1101, 1010, 1011, 00101, 01101, 10101, 11101, 111101,000101,……}
So DFA can be
Q = { q0,q1,q2,q3 }, Σ = { 0 , 1 }, q= { q0 }, F = { q3 } and δ : Q X Σ –> Q
20
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example 3. Design DFA for a string which accepts a String Containing 1001 as its substring over an
Input Symbol ∑ = {0, 1}.
Solution:
L = { 1001,01001,11001,10010,10011,001001,011001,101001,111001,100100,100101….}
So DFA can be
Q = { 0,1,2,3,4 }, Σ = { 0 , 1 }, q= { 0 }, F = { 4 } and δ : Q X Σ –> Q
The required DFA is
Example 4. Construct DFA for the language over { a,b} that contain substring ‘abb’.
Solution:
L = { abb, aabb, babb, abba, abbb, ababb, baabb, aaabb, bbabb………..}
So DFA can be
Q = { q0,q1,q2,q3 }, Σ = { a , b }, q= { q0 }, F = { q3 } and δ : Q X Σ –> Q
The required DFA is
21
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example 5. Design DFA for a string which accepts a String not Containing 00 as its substring over
an Input Symbol ∑ = {0, 1}.
Solution:
L = { abb, aabb, babb, abba, abbb, ababb, baabb, aaabb, bbabb………..}
The required DFA is
Example 6. Design DFA for a string which accepts a String not Containing 101 as its substring over
an Input Symbol ∑ = {0, 1}.
Solution: L = { 0, 1, 00, 01, 10, 11, 000, 001, 011, 010, 110, 111, 110…………. }
Contains the string 101
22
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example 7. Construct DFA for the language over { a,b} that does not contain substring ‘abb’.
Solution:
L = { a, b, aa, ab, ba, bb, aaa, aab, aba, baa, bba, bbb,………….}
So DFA can be
Q = { q0,q1,q2,q3 }, Σ = { a , b }, q= { q0 }, F = { q0,q1,q2 } and δ : Q X Σ –> Q
The required DFA is
Example 8. Design DFA for a string which accepts a String not Containing aabb as its substring
over an Input Symbol ∑ = {a, b}.
23
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example: Draw a DFA for the language accepting strings containing at most two ‘0’ over input
alphabets ∑ = {0, 1} , i.e | W |<=2 ?
Solution:
Example : Draw a DFA for the language accepting strings containing at least two ‘0’ over input
alphabets ∑ = {0, 1} , i.e | W |>=2 ?
Solution:
Example : Draw a DFA for the language accepting strings containing exactly two ‘0’ over input
alphabets ∑ = {0, 1} ?
Solution:
24
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example : Draw a DFA for the language accepting strings containing three consecutive ‘0’ always
over input alphabets ∑ = {0, 1} ?
Solution:
Example . Construct a DFA that accepts set of all strings over ∑ = {a, b} of length 2 i.e, | w | = 2.
Solution: L = { All the strings of length 2 }
L = {aa, ab, ba, bb}
So DFA can be
Q = { q0, q1, q2, D }, Σ = { a , b }, q= { q0 }, F = { q2 } and δ : Q X Σ –> Q
The required DFA is
Example . Construct a DFA that accepts set of all strings over ∑ = {a, b} of length at most 2
i.e, | w | <= 2.
Solution: L = { All the strings of length at most 2 }
L = { £, a, b, aa, ab, ba, bb }
So DFA can be
Q = { q0, q1, q2, D }, Σ = { a , b }, q= { q0 }, F = { q2 } and δ : Q X Σ –> Q
The required DFA is
25
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example. Construct a DFA that accepts set of all strings over ∑ = {a, b} of length at least 2
i.e, | w | >= 2.
Solution: L = {All the strings of length at most 2 }
L = { aa, ab, ba, bb, aaa, aab, aba, bba,……….. }
So DFA can be
Q = { q0, q1, q2 }, Σ = { a , b }, q= { q0 }, F = { q2 } and δ : Q X Σ –> Q
The required DFA is
Example: Design a DFA with sigma ∑ = {0, 1}, accepts those strings that have an even number of
“0’s” and an even number of “1’s”.
Solution:
The given question provides the following language
L= { ε, 00, 11, 0101, 1001, 0011, 00001111, 11110000,…., }
Note: Epsilon (ε) represents zero, which is even.
Example: Design a DFA with sigma ∑ = {0, 1}, accepts those strings that have an even number of
“0’s” and an odd number of “1’s”.
Solution:
The given question provides the following language
L= { ε, 00, 1, 01101, 111001, 00111, 000011111, 111110000,…., }
26
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example: Design a DFA with sigma ∑ = {0, 1}, accepts those strings that have an odd number of
“0’s” and an even number of “1’s”.
Solution:
The given question provides the following language
L= { 011, 00101, 10001, 00011, 000001111, 111100000,…., }
Example: Design a DFA with sigma ∑ = {0, 1}, accepts those strings that have an odd number of
“0’s” and an odd number of “1’s”.
Solution:
The given question provides the following language
L= { 01, 001101, 110001, 000111, 0000011111, 1111100000,…., }
27
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example: For ∑= {a, b}. Construct DFA that Accept the set consisting of
a) All string with exactly 2 a’ s.
b) All string with exactly 3 a’ s.
c) All string with exactly one b.
d) All string with exactly 3 symbols.
a) All string with exactly 2 a’ s.
Solution: L = {aa, aab, baa, aba, bbaa, bbbaa…}
b) All string with exactly 3 a’ s.
Solution: L = {aaa, aaab, abaa, aaba, abbaa, baaa…}
c) All string with exactly one b.
Solution: L = {b, ab, ba, baa, aab………}
d) All string with exactly 3 symbols.
Solution: L = {aaa, bbb, baa, aba, bba, abb, aab……..}
Example: For ∑= {a, b}. Construct DFA that Accept the set consisting of
a) All string with at least 2 a’s. [At least 2 means-> 2 or above].
b) All string with at least 3 a’s.
c) All string with at least 4 b’s.
d) All string with at least 3 symbols.
a) All string with at least 2 a’ s.
Solution: L = {aa, aab, baa, aba, bbaa, bbbaa,aaa,abaa,aaaab……}
b) All string with at least 3 a’ s.
Solution: L = {aaa, aaab, baaa, aaba, aabbaa, abaaa…}
c) All string with at least 4 b’s.
Solution: L = {bbbb, abbbb, bbbba, bbbbbaa, aabbbb………}
d) All string with at least 3 symbols.
Solution: L = {aaa, bbb, baa, aba, bba, abb, aab,aaaa, abab, aaabb……..}
Example: For ∑= {a, b}. Construct DFA that Accept the set consisting of
a) All string with at most 2 a’s. [At most 2 means-> 2 or below].
b) All string with at most 3 a’s.
c) All string with at most 4 b’s.
d) All string with at most 3 symbols.
a) All string with at most 2 a’ s.
28
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Solution: at most 2 a’ s ( 0,1,2)
L = { £, a, aa, aab,baa,bbaa,aabb……}
b) All string with at most 3 a’ s.
Solution: at most 3 a’ s ( 0,1,2,3)
L = {£, a, aa, aab,baa,bbaa,aabb,aaa,aaab,baaa……}
c) All string with at most 4 b’s.
Solution: at most 4 b’ s ( 0,1,2,3,4)
L = {£, b, bb, bba,abb,bbba,aabbb,bbbb……}
d) All string with at most 3 symbols.
Solution: at most 3 symbols ( 0,1,2,3 )
L = {£, a, b, aa,ab,ba,bb,aaa,aba,bbb,bab,……….}
2 : NFA ( Non-Deterministic Finite Automata ) : NFA stands for non-deterministic finite automata. For
a given input symbol, there can be more than one transition from a state. Such automaton is called Non-
deterministic Finite Automaton.
A NFA can be represented as 5-Tuples.
MNFA = { Q, ∑, δ , q0 , F }
Where,
Q - is a finite set of states which is non-empty
∑ - is input alphabet
δ - is a transition function or mapping function Q X ∑ → 2 Q
q0 – is Initial state
F – is a set of final states.
Note: Every DFA is NFA but Every NFA need not be DFA.
Example: NFA Transition Diagram
29
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
NFA Transition Table
Transition table for the given Non-Deterministic Finite Automata (NFA) is-
Example 1: Construct a NFA that accepts all binary strings that end with 101.
Example 2: Design an NFA with ∑ = {0, 1} accepts all string ending with 01.
Example 3 : Design an NFA in which all the string contain a substring 1110.
30
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Some important points about DFA and NFA
Every DFA is NFA, but NFA is not DFA.
In NFA and DFA, there can be multiple final states.
DFA is used in Lexical Analysis in Compiler.
NFA mostly used as theoretical concept.
Difference between DFA and NFA
DFA NFA
1 : DFA is a finite automata. Where , for all 1 : NFA is a finite automata. Where , for some
cases, when a single input is given to a single cases, when a single input is given to a single
state, the machine goes to a single state, i.e, all state, the machine goes to more than one
the moves of the machine can be uniquely state, i.e, some of the moves of the machine
determined by the present state and the can not be uniquely determined by the
present input symbol. present state and the present input symbol.
2: A NFA can be represented as 5-Tuples.
2 : A DFA can be represented as 5-Tuples.
MNFA = { Q, ∑, δ , q0 , F }
MDFA = { Q, ∑, δ , q0 , F }
Where,
Where,
Q - is a finite set of states which is non-
Q - is a finite set of states which is non-empty
empty
∑ - is input alphabet
∑ - is input alphabet
δ - is a transition function or mapping
δ - is a transition function or mapping
function Q X ∑ → Q
function Q X ∑ → 2Q
q0 – is Initial state
q0 – is Initial state.
F – is a set of final states.
F – is a set of final states.
3 : Difficult to construct 3 : Easy to construct
4 : No £-transition exist i.e, there should not be 4 : £-transition can exist i.e, without any input
any transition or a a transition if exist it should there can be transition from one state to
be on an input symbol. another state.
5 : DFA takes more space but have speed of 5: NFA’s are more compact but takes more
computation. time.
31
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
CONVERSION OF NFA TO DFA
Solution:
Step1: Draw NFA Graph (or) Diagram.
Step2: Transition table for the given Non-Deterministic Finite Automata (NFA) is-
Step 03: Now convert NFA transition Table To its corresponding DFA transition Table
First consider the first row of NFA transition table which will becomes the first row of DFA transition
table. It’s given below
32
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Input / Next State
State / Alphabet
a b
→q0 q0 q0, q1
We can see in above row, transition for q0 is already found. New state present in state Q’ is {q0, q1}.
δ'([q0,q1], a) = δ(q0, a) ∪ δ(q1, a)
= {q0} ∪ {-}
= [q0]
δ'([q0,q1], b) = δ(q0, b) ∪ δ(q1, b)
= {q0,q1} ∪ {q2}
= {q0,q1,q2} // new state generated
Add transitions for set of states {q0, q1} to the transition table T’.
Input / Next State
State / Alphabet
a b
→q0 q0 q0, q1
Since no new states are left to be added in the transition table T’, so we stop.
The state [q0, q1,q2] is the final state as well because it contains a final state q2.
33
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Step 4: Finally, Transition table for Deterministic Finite Automata (DFA) is-
Input / Next State
State / Alphabet
a b
Solution:
Step1: Draw NFA Graph (or) Diagram.
34
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Step2: Transition table for the given Non-Deterministic Finite Automata (NFA) is-
q1 {q1,*q2} *q2
Step 03: Now convert NFA transition Table To its corresponding DFA transition Table
First consider the first row of NFA transition table which will becomes the first row of DFA transition
table. It’s given below
Input / Next State
State / Alphabet
0 1
→q0 q0 q1, q2
We can see in above row, transition for q0 is already found. New state present in state Q’ is {q1, q2}.
δ'([q1,q2], a) = δ(q1, a) ∪ δ(q2, a)
= {q1,q2} ∪ {q0,q1}
= [q0,q1,q2] // new state generated
δ'([q1,q2], b) = δ(q1, b) ∪ δ(q2, b)
= {q2} ∪ {q1}
= {q1,q2} // new state generated
Add transitions for set of states {q1, q2} to the transition table T’.
Input / Next State
State / Alphabet
0 1
→q0 q0 {q1, q2}
We can see in above row, New state present in state Q’ is {q0,q1, q2}and q1,q2 is already found.
δ'([q0,q1,q2], a) = δ(q0, a) ∪ δ(q1, a) ∪ δ(q2, a)
= {q0} ∪ {q1,q2} ∪ {q0,q1}
= [q0,q1,q2]
δ'([q0,q1,q2], b) = δ(q0, b) ∪ δ(q1, b) ∪ δ(q2, b)
= {q1,q2} ∪ {q2} ∪ {q1}
= {q1,q2}
35
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Add transitions for set of states {q0, q1, q2} to the transition table T’.
Input / Next State
State / Alphabet
0 1
→q0 q0 {q1, q2}
Since no new states are left to be added in the transition table T’, so we stop.
The state [q0, q1,q2] and [ q1,q2 ] is the final state as well because it contains a final state q2.
Step 4 : Finally, Transition table for Deterministic Finite Automata (DFA) is-
36
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Solution:
Step2: Transition table for the given Non-Deterministic Finite Automata (NFA) is-
*q1 Ø Ø
q2 {*q1, q2} q2
Step 03: Now convert NFA transition Table To its corresponding DFA transition Table
First consider the first row of NFA transition table which will becomes the first row of DFA transition
table. It’s given below
37
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Add transitions for set of states {q1, q2} to the transition table T’.
q2 {q1, q2} q2
Add transitions for dead state {Ø} to the transition table T’.
q2 {q1, q2} q2
Ø Ø Ø
Step 4 : Finally, Transition table for Deterministic Finite Automata (DFA) is-
q2 *{q1, q2} q2
Ø Ø Ø
38
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Now, Deterministic Finite Automata (DFA) may be drawn as-
Note:
After conversion, the number of states in the resulting DFA may or may not be same as NFA.
The maximum number of states that may be present in the DFA are 2 Number of states in the NFA.
In the resulting DFA, all those states that contain the final state(s) of NFA are treated as final
states.
In general, the following relationship exists between the number of states in the NFA and DFA-
1 <= n <= 2m
Here,
n = Number of states in the DFA
m = Number of states in the NFA
39
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
NFA WITH £ (Epsilon) TRANSITION
If we want to construct an finite automata which accept a language, sometimes it becomes very
difficult or to be impossible to construct a DFA or NFA. In this case we can use NFA with £ (Epsilon)
transition.
Definition of NFA with £ Transition: If any finite automata contains £ transition, then that finite
MNFA,£ = ( Q, ∑, δ , q0 , F )
Where,
Q - No of States
∑ - is input alphabet
δ - is a transition function or mapping function Q X { ∑ U £ } → 2Q
q0 – is Initial state
F – is a set of final states.
Example: NFA with £ Transition Diagram
→q0 q0 Ø Ø q1
q1 Ø q1 Ø q2
*q2 Ø q2 q2 Ø
40
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Epsilon Closure ( E-Closure ) : All the states that can be reached from a particular state only by
seeing ‘ £ ‘ Symbol. Every state on £ goes to itself.
Example :
41
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example 1: Convert Following NFA with £ moves to NFA without £ moves.
→q0 q1 Ø Ø
q1 Ø Ø q2
*q2 Ø q2 Ø
Finding £ Closure :
ε-closure ( q0 ) = { q0 }
ε-closure ( q1 ) = { q1, q2 }
ε-closure ( q2 ) = { q2 }
Extended Transition Function:
δ ( q0,a ) = ε-closure ( δ ( δ ( q0, ε ), a))
ε-closure ( δ (ε-closure ( q0 ), a ))
ε-closure ( δ (q0 , a ))
ε-closure ( q1 )
= { q1, q2 }
δ ( q0,b ) = ε-closure ( δ ( δ ( q0, £ ), b))
ε-closure ( δ (ε-closure ( q0 ), b ))
ε-closure ( δ (q0 , b ))
ε-closure ( Ф )
={Ф}
δ ( q1,a ) = ε-closure ( δ ( δ ( q1, £ ), a))
ε-closure ( δ (ε-closure ( q1 ), a ))
ε-closure ( δ ( ( q1, q2 ) , a ) )
ε-closure ( δ ( q1, a) U δ ( q2, a) )
ε-closure ( Ф U Ф) = ( Ф
={Ф}
42
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
δ ( q1,b) = ε-closure ( δ ( δ ( q1, £ ), b))
ε-closure ( δ (ε-closure ( q1 ), b ))
ε-closure ( δ ( ( q1, q2 ) , b ) )
ε-closure ( δ ( q1, b) U δ ( q2, b) )
ε-closure ( Ф U q2 )
ε-closure ( q2 )
= { q2 }
δ ( q2,a ) = ε-closure ( δ ( δ ( q2, £ ), a))
ε-closure ( δ (ε-closure ( q2 ), a ))
ε-closure ( δ ( q2 , a ) )
ε-closure ( Ф )= { Ф }
δ ( q2,b ) = ε-closure ( δ ( δ ( q2, £ ), b))
ε-closure ( δ (ε-closure ( q2 ), b ))
ε-closure ( δ (q2 , b ))
ε-closure ( q2 )
= { q2 }
Transition Table for NFA without £
*q1 Ø q2
*q2 Ø q2
State q1 and q2 become the final state as ε-closure of q1 and q2 contain the final state q2.
Transition diagram for NFA without £ moves is
43
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example 2: Convert Following NFA with £ moves to NFA without £ moves.
→q0 q0 Ø q1
*q1 q3 q2 Ø
q2 q1 q3 Ø
q3 q3 q3 Ø
Finding £ Closure :
ε-closure ( q0 ) = { q0, q1 }
ε-closure ( q1 ) = { q1 }
ε-closure ( q2 ) = { q2 }
ε-closure ( q3 ) = { q3 }
Extended Transition Function:
δ ( q0,0 ) = ε-closure ( δ ( δ ( q0, £ ), 0))
ε-closure ( δ (ε-closure ( q0 ), 0 ))
ε-closure ( δ (q0,q1) , 0 ))
ε-closure (δ (q0,0 )U δ (q1,0 ))
ε-closure (q0,q3)
ε-closure (q0) U ε-closure (q3)
={ q0, q1 } U { q3}
= { q0,q1,q3}
δ ( q0,1 )= ε-closure ( δ ( δ ( q0, £ ), 1))
ε-closure ( δ ( ε-closure ( q0 ), 1 ))
ε-closure ( δ (q0,q1) , 1 ))
44
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
ε-closure (δ (q0,1 )U δ (q1,1 ))
ε-closure (Ф,q2)
ε-closure (Ф) U ε-closure (q2)
={ Ф } U { q2}
= { q2}
δ ( q1,0 ) = ε-closure ( δ ( δ ( q1, £ ), 0))
ε-closure ( δ (ε-closure ( q1 ), 0 ))
ε-closure ( δ (q1 , 0 ))
ε-closure ( q3 )
= { q3 }
δ ( q1,1 ) =ε-closure ( δ ( δ ( q1, £ ), 1))
ε-closure ( δ (ε-closure ( q1 ), 1 ))
ε-closure ( δ (q1 , 1))
ε-closure ( q2 )
= { q2 }
δ ( q2,0 ) = ε-closure ( δ ( δ ( q2, £ ), 0))
ε-closure ( δ (ε-closure ( q2 ), 0 ))
ε-closure ( δ (q2 , 0 ))
ε-closure ( q1 )
= { q1 }
δ ( q2,1 ) = ε-closure ( δ ( δ ( q2, £ ), 1))
ε-closure ( δ (ε-closure ( q2 ), 1 ))
ε-closure ( δ (q2 , 1 ))
ε-closure ( q3 )
= { q3 }
δ ( q3,0 ) = ε-closure ( δ ( δ ( q3, £ ), 0))
ε-closure ( δ (ε-closure ( q3 ), 0 ))
ε-closure ( δ (q3 , 0 ))
ε-closure ( q3 )
= { q3 }
45
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
δ ( q3,1 ) = ε-closure ( δ ( δ ( q3, £ ), 1))
ε-closure ( δ (ε-closure ( q3 ), 1 ))
ε-closure ( δ (q3 , 1 ))
ε-closure ( q3 )
= { q3 }
Transition Table for NFA without £
*q1 q3 q2
q2 q1 q3
q3 q3 q3
State q0 and q1 become the final state as ε-closure of q0 and q0 contain the final state q1.
46
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
CONVERSION FROM NFA WITH ΕPSILON TO DFA
Step 1: We will take the ε-closure for the starting state of NFA as a starting state of DFA.
Step 2: Find the states for each input symbol that can be traversed from the present. That means the
union of transition value and their closures for each state of NFA present in the current state of
DFA.
Step 3: If we found a new state, take it as current state and repeat step 2.
Step 4: Repeat Step 2 and Step 3 until there is no new state present in the transition table of DFA.
Step 5: Mark the states of DFA as a final state which contains the final state of NFA.
Solution:
Let us obtain ε-closure of each state.
1. ε-closure {q0} = {q0, q1, q2}
2. ε-closure {q1} = {q1}
3. ε-closure {q2} = {q2}
4. ε-closure {q3} = {q3}
5. ε-closure {q4} = {q4}
47
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
δ (A, 1) = ε-closure {δ((q0, q1, q2), 1) }
→*A q3 q3
*B Ф q4
C Ф Ф
DFA Transition Diagram
48
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example2: Convert the NFA with ε into its equivalent DFA.
Solution:
Let us obtain ε-closure of each state.
1. ε-closure {q0} = {q0, q1, q2}
2. ε-closure {q1} = {q1,q2}
3. ε-closure {q2} = {q2}
49
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
δ (A, 2) = ε-closure {δ((q0, q1, q2), 2) }
ε-closure { ε ∪ q2 }
ε-closure { q2 }
= {q2}
For State C
δ (C, 0) = ε-closure { δ ( q2, 0) }
ε-closure { Ф }
={Ф}
δ (C, 1) = ε-closure { δ ( q2, 1) }
ε-closure { Ф }
={Ф}
δ (C, 2) = ε-closure { δ ( q2, 2) }
ε-closure { q2 }
= { q2 }
DFA Transition Table
Input / Next State
State / Alphabet
0 1 2
50
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
*B Ф { q1,q2} q2
*C Ф Ф q2
As A = {q0, q1, q2} in which final state q2 lies hence A is final state. B = {q1, q2} in which the state
q2 lies hence B is also final state. C = {q2}, the state q2 lies hence C is also a final state.
MOORE MACHINE: Moore machine is a Finite automaton with output. If the output is associated with the
present state then such a machine is called Moore Machine.
Formal Definition of Moore Machine
The Moore Machine is classified in 6-tuple -(Q, ∑, q0, , O, δ, λ)
Where,
Q - Finite set of states.
∑ - Input alphabet.
q0 - Start state or initial state
O - Output alphabet.
δ - An input transition function, δ: Q × ∑ → Q.
λ - A output transition function, which maps Q → O.
Example : Moore Machine Diagram
51
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Transition table for Moore Machine is:
Next State
Present State Output ( λ )
Input = 0 Input = 1
q0 q1 q2 1
q1 q2 q1 1
q2 q2 q0 0
MEALY MACHINE: Mealy Machine is a Finite automata with output. Whose output depends on the
present state as well as the present input. In Mealy machine every transition for a particular input
symbol has a fixed output.
Formal Definition of Mealy Machine
The Mealy machine can be described by 6 tuples -(Q, ∑, q0, , O, δ, λ)
Where
Q: finite set of states
∑: finite set of input alphabet
q0: initial state of machine
O: Output alphabet
δ: transition function where Q × ∑ → Q , it maps a state and the input alphabet to the next state.
λ': output function where Q × ∑ →O, it maps each state to the output alphabetically.
Example : Mealy Machine Diagram
52
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Transition table for Moore Machine is:
Input = 0 Input = 1
Present State
State Output State Output
→q1 q1 0 q2 0
q2 q2 1 q3 0
q3 q2 0 q3 1
→q0 q0 q1 0
q1 q0 q1 1
Hence the transition table for the Mealy machine can be drawn as follows:
Input = a Input = b
Present State
State Output State Output
→q0 q0 0 q1 1
q1 q0 0 q1 1
54
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example 2: Convert the given Moore machine into its equivalent Mealy machine.
Solution:
The transition table of given Moore machine is as follows:
Next State
Present State Output ( λ )
Input = a Input = b
→q0 q1 q0 0
q1 q1 q2 0
q2 q1 q0 1
q0 q1 0 q0 0
q1 q1 0 q2 1
q2 q1 0 q0 0
The equivalent Mealy machine will be,
Example 3: Convert the given Moore machine into its equivalent Mealy machine.
Next State
Present State Output ( λ )
Input = a Input = b
q0 q0 q1 0
q1 q2 q0 1
q2 q1 q2 2
56
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Solution: The transaction diagram for the given problem can be drawn as:
Hence the transition table for the Mealy machine can be drawn as follows:
Input = a Input = b
Present State
State Output State Output
q0 q0 0 q1 1
q1 q2 2 q0 0
q2 q1 1 q2 2
Example 1: Convert the following Mealy machine into equivalent Moore machine.
q1 q1 0 q2 0
q2 q2 1 q3 0
q3 q2 0 q3 1
For state q1, there is only one incident edge with output 1. So, we don't need to split .
For state q2, there is 2 incident edge with output 0 and 1. So, we will split this state into two states
58
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
q20( state with output 0) and q21(with output 1).
For state q3, there is 2 incident edge with output 0 and 1. So, we will split this state into two states
q30( state with output 0) and q31( state with output 1).
Reconstruct the table with new states
Input = 0 Input = 1
Present State
State Output State Output
q1 q1 0 q20 0
q20 q21 1 q30 0
q21 q21 1 q30 0
q30 q20 0 q31 1
q31 q20 0 q31 1
q1 q1 q20 0
59
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
Example 2: Convert the following Mealy machine into equivalent Moore machine.
Input = a Input = b
Present State
State Output State Output
q1 q1 1 q2 0
q2 q4 1 q4 1
q3 q2 1 q3 1
q4 q3 0 q1 1
For state q1, there is only one incident edge with output 1. So, we don't need to split this state in
Moore machine.
60
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
For state q2, there is 2 incident edge with output 0 and 1. So, we will split this state into two states
q20( state with output 0) and q21(with output 1).
For state q3, there is 2 incident edge with output 0 and 1. So, we will split this state into two states
q30( state with output 0) and q31( state with output 1).
For state q4, there is only one incident edge with output 1. So, we don't need to split this state in
Moore machine.
Reconstruct the table with new states
Input = a Input = b
Present State
State Output State Output
q1 q1 1 q20 0
q20 q4 1 q4 1
q21 q4 1 q4 1
q4 q30 0 q1 1
q1 q1 q20 1
q20 q4 q4 0
q21 q4 q4 1
q4 q30 q1 1
61
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM
62
CH SUBBAREDDY_FLAT NOTES[R22] – III YEAR II SEM