Theory of Computation: Dr. Krishnendu Rarhi E: Krishnendu.e9621@cumail - in
Theory of Computation: Dr. Krishnendu Rarhi E: Krishnendu.e9621@cumail - in
Theory of Computation: Dr. Krishnendu Rarhi E: Krishnendu.e9621@cumail - in
Introduction
Regular Languages
Regular Expression
any no. of vowels followed by any no. of { ε , a ,aou, aiou, b, abcd…..} where ε represent
v*.c* ( where v – vowels and c – consonants)
consonants empty string (in case 0 vowels and o consonants )
• Offers a declarative way to express the pattern of any string we want to accept
• E.g., 01*+ 10*
Finite Automata
Regular = (DFA, NFA,
expressions
ε-NFA)
Syntactical
expressions Automata/machines
Regular
Languages
Formal language
classes
Why?
• L* is an infinite set iff |L|≥1 and L≠{ε}
• If L={ε}, then L* = {ε} Why?
ε -NFA NFA
Theorem 2
Kleene Theorem
Reg
DFA
Ex
Theorem 1
Dr. Krishnendu Rarhi©
DFA Reg Ex
Theorem 1
DFA to RE construction
Informally, trace all distinct paths (traversing cycles only once)
from the start state to each of the final states
and enumerate all the expressions along the way
Example: 1 0 0,1
q 0 q 1 q
0 1 2
1* 00* 1 (0+1)*
Q) What is the language?
1*00*1(0+1)*
Dr. Krishnendu Rarhi©
Reg Ex ε -NFA
Theorem 2
RE to ε-NFA construction
Example: (0+1)*01(0+1)*
(0+1)* 01 (0+1)*
ε ε
0 0
ε ε ε ε
ε 0 1
ε 1
ε ε 1
ε
ε ε
The above automata will accept all strings which have even number of
a’s. For zero a’s, it will be in q0 which is final state. For one ‘a’, it will
go from q0 to q1 and the string will not be accepted. For two a’s at any
positions, it will go from q0 to q1 for 1st ‘a’ and q1 to q0 for second ‘a’.
So, it will accept all strings with even number of a’s.
Dr. Krishnendu Rarhi©
Designing FA from RE
• String with ‘ab’ as substring : The regular expression for
strings with ‘ab’ as substring is (a|b)*ab(a|b)*. We can construct
finite automata as shown in Figure
The above automata will accept all string which have ‘ab’ as substring. The
automata will remain in initial state q0 for b’s. It will move to q1 after
reading ‘a’ and remain in same state for all ‘a’ afterwards. Then it will move
to q2 if ‘b’ is read. That means, the string has read ‘ab’ as substring if it
reaches q2.
Dr. Krishnendu Rarhi©
Designing FA from RE
• String with count of ‘a’ divisible by 3 : The regular expression for
strings with count of a divisible by 3 is {a3n | n >= 0}. We can construct
automata as shown in Figure
If weabove
The wantautomata
to designwill
a finite
acceptautomata with
all string of a3n. Theofautomata
number
form a’s as 3n+1,
will
remain in initial state
same automata can q0
be for ɛ and
used withit will
finalbestate
accepted.
as q1 For stringof‘aaa’,
instead q0. it will
move
If from q0 to q1 then q1 toautomata
q2 and then q2 to q0. For every
kn set of three
a’s, it will come to q0, hence accepted. Otherwise, it will be in |q1nor
we want to design a finite with language {a >=q2,0}, k
states are required. We have used k = 3 in our example.
hence rejected.
Dr. Krishnendu Rarhi©
Designing FA from RE
• Binary numbers divisible by 3 : The regular expression for binary numbers
which are divisible by three is (0|1(01*0)*1)*. The examples of binary
number divisible by 3 are 0, 011, 110, 1001, 1100, 1111, 10010 etc. The
DFA corresponding to binary number divisible by 3 can be shown in Figure
The above automata will accept all binary numbers divisible by 3. For 1001,
the automata will go from q0 to q1, then q1 to q2, then q2 to q1 and finally
q2 to q0, hence accepted. For 0111, the automata will go from q0 to q0, then
q0 to q1, then q1 to q0 and finally q0 to q1, hence rejected.
Dr. Krishnendu Rarhi©
Designing FA from RE
• String with regular expression (111 + 11111)* : The string
accepted using this regular expression will have 3, 5, 6(111 twice), 8
(11111 once and 111 once), 9 (111 thrice), 10 (11111 twice) and all
other counts of 1 afterwards. The DFA corresponding to given regular
expression is given in Figure
The above automata will accept all binary numbers divisible by 3. For 1001,
the automata will go from q0 to q1, then q1 to q2, then q2 to q1 and finally
q2 to q0, hence accepted. For 0111, the automata will go from q0 to q0, then
q0 to q1, then q1 to q0 and finally q0 to q1, hence rejected.
Dr. Krishnendu Rarhi©
Designing FA from RE (Example)
• Will be the minimum number of states for strings with odd
number of a’s?
The regular expression for odd number of a is b*ab*(ab*ab*)* and
corresponding automata is given in Figure and minimum number of
states are 2.
Here, State A represent set of all string of length zero (0), state B represent set of all string of
length one (1), state C represent set of all string of length two (2). State C is the final state
and D is the dead state it is so because after getting any alphabet as input it will not go into
final state ever.Number of states: n+2
Where n is |w|=n
The above automata will accept all the strings having the length of the string exactly 2. When
the length of the string is 1, then it will go from state A to B. When the length of the string is 2,
then it will go from state B to C and when the length of the string is greater than 2, then it will
go from state C to D (Dead state) and after it from state D TO D itself.
Here, State A represent set of all sting of length zero (0), state B represent
set of all sting of length one (1), and state C represent set of all sting of
length two (2). Number of states: n+1
Where n is |w|>=n
The above automata will accept all the strings having the length of the string
at least 2. When the length of the string is 1, then it will go from state A to B.
When the length of the string is 2, then it will go from state B to C and lastly
when the length of the string is greater than 2, then it will go from state C to
C itself.
Dr. Krishnendu Rarhi©
Designing Deterministic Finite Automata
• Problem-2: Construction of a DFA for the set of string over {a, b} such that length
of the string |w|<=2 i.e, length of the string is atmost 2.
Explanation – The desired language will be like:
L = {?, aa, ab, ba, bb}
Here, State A represent set of all sting of length zero (0), state B represent set of all
sting of length one (1), state C represent set of all sting of length two (2), state A, B,
C is the final state and D is the dead state it is so because after getting any alphabet
as input it will not go into final state ever.
Number of states: n+2
Where n is |w|<=n
The above automata will accept all the strings having the length of the string at most
2. When the length of the string is 1, then it will go from state A to B. When the
length of the string is 2, then it will go from state B to C and lastly when the length of
the string is greater than 2, then it will go from state C to D (Dead state).
Sometimes, it is not easy to convert regular expression to DFA. First you can convert regular
expression to NFA and then NFA to DFA.
Dr. Krishnendu Rarhi©
Conversion of NFA to DFA (Example)
• The number of states in the minimal deterministic finite
automaton corresponding to the regular expression (0 + 1)* (10)
is ___________.
First, we will make an NFA for the above expression. To make an NFA for (0
+ 1)*, NFA will be in same state q0 on input symbol 0 or 1. Then for
concatenation, we will add two moves (q0 to q1 for 1 and q1 to q2 for 0) as
shown in Figure