Regular Expression, DFA and NFA: Prepared By: Prof. J. S. Dhobi Prof. M. D. Mehta
Regular Expression, DFA and NFA: Prepared By: Prof. J. S. Dhobi Prof. M. D. Mehta
Prepared by:
Prof. J. S. Dhobi
Prof. M. D. Mehta
Regular Expressions 2
(a b c) *
a, bc * , a, bc, aa, abc, bca,...
Definition of a Regular Expression 3
R is a regular expression if it is:
1. a for some a in the alphabet , standing for the language {a}
2. ε, standing for the language {ε}
3. Ø, standing for the empty language
4. R1+R2 where R1 and R2 are regular expressions, and + signifies union
(sometimes | is used)
5. R1R2 where R1 and R2 are regular expressions and this signifies
concatenation
6. R* where R is a regular expression and signifies closure
7. (R) where R is a regular expression, then a parenthesized R is also a
regular expression
Some RE Examples 4
(a+b)*abb Set of strings of a’s and b’s ending with the string
abb. So L = {abb, aabb, babb, aaabb, ababb,
…………..}
(aa + ab + ba + bb)* String of a’s and b’s of even length can be
obtained by concatenating any combination of the
strings aa, ab, ba and bb including null, so L = {aa,
ab, ba, bb, aaab, aaba, …………..}
Examples of Regular Expression 5
Example 1: Write the regular expression for the language accepting
all the string which are starting with 1 and ending with 0, over ∑ =
{0, 1}.
Solution : R.E. = 1 (0+1)* 0
Example 2: Write the regular expression for the language starting
and ending with a and having any having any combination of b's in
between.
Solution : R.E. = a b* b
Example 3: Write the regular expression for the language starting
with a but not having consecutive b's.
Solution: The regular expression has to be built for the language:
L = {a, aba, aab, aba, aaa, abab, .....}
So, R.E. = {a + ab}*
Deterministic Finite Automaton
6
(DFA)
Input Tape
String
Output
“Accept”
Finite
or
Automaton
“Reject”
Deterministic Finite State Automata (DFA)
7
……..
0 1 1 0 0
Finite
Control
One-way, infinite tape, broken into cells
One-way, read-only tape head.
Finite control, i.e.,
finite number of states, and
transition rules between them, i.e.,
a program, containing the position of the read head, current symbol being scanned, and
the current “state.”
Deterministic Finite State Automata 8
(DFA)
A string is placed on the tape, read head is positioned at the left
end, and the DFA will read the string one symbol at a time until all
symbols have been read. The DFA will then either accept or reject
the string
Finite Automaton (def.) 9
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
initial accepting
state state
transition
state
Alphabet {a , b } 11
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Initial state
Scanning the Input 13
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Scanning the Input 14
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Scanning the Input 15
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Input finished 16
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
accept
A Rejection Case
17
a b a
Input String
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Rejection case 18
a b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Rejection case 19
a b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Input finished 20
a b a
a, b
reject
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Another Rejection Case
21
Tape is empty
( )
Input Finished
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
reject
Language Accepted: L abba 22
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
To accept a string: 23
To reject a string:
all the input string is scanned
and the last state is non-accepting
Another Example 24
L , ab , abba
a, b
q5
b a a a, b
b
q0 a q1 b q2 b q3 a q4
Accept Accept Accept
state state state
25
Empty Tape
( )
Input Finished a, b
q5
b a a a, b
b
q0 a q1 b q2 b q3 a q4
accept
Another Example 26
a a,b
q0 b q1 a, b q2
a a b
Input String
a a,b
q0 b q1 a, b q2
28
a a b
a a,b
q0 b q1 a, b q2
29
a a b
a a,b
q0 b q1 a, b q2
Input finished 30
a a b
a a,b
accept
q0 b q1 a, b q2
A rejection case 31
b a b
Input String
a a,b
q0 b q1 a, b q2
32
b a b
a a,b
q0 b q1 a, b q2
33
b a b
a a,b
q0 b q1 a, b q2
Input finished 34
b a b
a a,b
q0 b q1 a, b q2
reject
35
Language Accepted:
L {a b : n 0 }
n
a a,b
q0 b q1 a, b q2
Another Example 36
Alphabet: {1}
1
q0 q1
1
Language Accepted:
EVEN {x : x and x is even}
*
:Q Q
(q , x ) q
x
q q
Example:
q0 , a q1
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
39
q0 , b q5
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
40
q2 , b q3
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
Transition Table for
41
symbols
a b
q0
q1 q5
q1 q5 q2
states
q2 q5 q3
a,b
q3 q4 q5
q4 q5 q5 q5
q5 q5 q5 a,b
b a a b
q0 a q1 b q2 b q3 a q4
Extended Transition Function 42
:Q Q
* *
(q ,w ) q
*
Example: q0 , ab q2
*
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
44
q0 , abbbaa q5
*
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
45
q1 , bba q4
*
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
Language Accepted by DFA 46
Language of DFA M:
it is denoted as L M and contains
all the strings accepted by M
Example 1:
Design a FA with ∑ = {0, 1} accepts those string which starts with 1
and ends with 0.
Example 2:
Design FA with ∑ = {0, 1} accepts even number of 0's and even
number of 1's.
DFA Examples 49
Here q0 is a start state and the final state also. Note carefully that a
symmetry of 0's and 1's is maintained. We can associate meanings to
each state as:
q0: state of even number of 0's and even number of 1's.
q1: state of odd number of 0's and even number of 1's.
q2: state of odd number of 0's and odd number of 1's.
q3: state of even number of 0's and odd number of 1's.
DFA Examples 50
Example 3: Design a DFA L(M) = {w | w ε {0, 1}*} and W is a string that
does not contain consecutive 1's.
Solution : When three consecutive 1's occur the DFA will be:
DFA Examples 51
q0 q0
L (M ) { } L (M ) *
a, b
q0 a, b q0
L (M ) { }
Language of the empty string
{a , b } 54
L M = { all strings with prefix ab }
a, b
q0 a q1 b q2
b a accept
q3 a, b
55
L M = { all binary strings containing
substring 001 }
0,1
1 0
1
0 0 00 1 001
0
56
L M = { all binary strings without
substring 001 }
1 0 0,1
1
0 1
0 00 001
0
57
L(M ) awa : w a , b
*
a
b
b
q0 a q2 q3
b a
q4
a, b
Regular Languages 58
M
Definition:
A language L
is regular if there is L (M ) L
a DFA that accepts it ( )
{ } { } {a , b } *
L {a nb n : n 0}
ADDITION {x y z : x 1 , y 1 , z 1 ,
n m k
nm k}
In NFA, when a specific input is given to the current state, the machine
goes to multiple states. It can have zero, one or more than one move on a
given input symbol. On the other hand, in DFA, when a specific input is
given to the current state, the machine goes to only one state. DFA has
only one move on a given input symbol.
State 0 1
States a b
*q1 Ф {q2}
*q2 Ф {q2}
NFA-^ To NFA 80
The transition graph can be: