0% found this document useful (0 votes)
160 views

Regular Expression, DFA and NFA: Prepared By: Prof. J. S. Dhobi Prof. M. D. Mehta

The document discusses regular expressions and deterministic finite automata (DFA). It defines regular expressions and provides examples. It then defines a DFA as a 5-tuple and explains how a DFA works by scanning input symbols and transitioning between states to either accept or reject the input string.

Uploaded by

Shital Sonawane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views

Regular Expression, DFA and NFA: Prepared By: Prof. J. S. Dhobi Prof. M. D. Mehta

The document discusses regular expressions and deterministic finite automata (DFA). It defines regular expressions and provides examples. It then defines a DFA as a 5-tuple and explains how a DFA works by scanning input symbols and transitioning between states to either accept or reject the input string.

Uploaded by

Shital Sonawane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 82

1

Regular Expression, DFA and


NFA

Prepared by:
Prof. J. S. Dhobi
Prof. M. D. Mehta
Regular Expressions 2

 Regular expressions describe regular languages .


 Notation to specify a language
 In a regular expression, x* means zero or more occurrence of x. It can generate {e, x,
xx, xxx, xxxx, .....}
 In a regular expression, x+ means one or more occurrence of x. It can generate {x, xx,
xxx, xxxx, .....}

Example: describes the language

(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

Regular Expressions Regular Set

(0 + 10*) L = { 0, 1, 10, 100, 1000, 10000, … }

(0*10*) L = {1, 01, 10, 010, 0010, …}

(0 + ε)(1 + ε) L = {ε, 0, 1, 01}

(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

 A deterministic finite automaton (DFA) M is defined by a 5-tuple


M=(Q,,,q0,F)
 Q: finite set of states
 : finite alphabet
 : transition function :QQ
 q0Q: start state
 FQ: set of accepting states
Transition Graph 10
a, b

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

For every state, there is a transition


for every symbol in the alphabet
head 12
Initial Configuration
Input Tape
a b b a
Input String

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

all the input string is scanned


and the last state is accepting

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

Accept trap state


state
27

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}
*

 { , 11, 1111, 111111, }


Transition Function 37

 :Q  Q
  (q , x )  q 
x
q q

Describes the result of a transition


from state q with symbol x
38

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 
*

Describes the resulting state


after scanning string w from state q
43

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

We say that a language L 


is accepted (or recognized)
 
by DFA M if L M  L 
DFA Examples 47

 Example 1:
 Design a FA with ∑ = {0, 1} accepts those string which starts with 1
and ends with 0.

 In state q1, if we read 1, we will be in state q1, but if we read 0 at


state q1, we will reach to state q2 which is the final state. In state q2,
if we read either 0 or 1, we will go to q2 state or q1 state
respectively. Note that if the input ends with 0, it will be in the final
state
DFA Examples 48

 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

 Here two consecutive 1's or single 1 is acceptable, hence.


 The stages q0, q1, q2 are the final states. The DFA will generate
the strings that do not contain consecutive 1's like 10, 110, 101,.....
etc.
More DFA Examples
52
  {a , b }
a, b
a, b

q0 q0

L (M )  { } L (M )   *

Empty language All strings


53
  {a , b }

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 ( )

 The languages accepted by all DFAs


 form the family of regular languages

Example regular languages:
59
 abba   , ab, abba
{a b : n  0} awa : w  a , b  
n *

{ all strings in {a,b}* with prefix ab }


{ all binary strings without substring 001}
{x : x  {1} and x is even}
*

{ } {  } {a , b } *

There exist automata that accept these


languages (see previous slides).
60
There exist languages which are not Regular:

L {a nb n : n  0}

ADDITION  {x  y  z : x  1 , y  1 , z  1 ,
n m k

nm k}

There is no DFA that accepts these languages

(we will prove this in a later class)


Non Deterministic Finite Automata 61

 NFA stands for non-deterministic finite automata. It is easy to construct


an NFA than DFA for a given regular language.
 The finite automata are called NFA when there exist many paths for
specific input from the current state to the next state.
 Every NFA is not DFA, but each NFA can be translated into DFA.
 NFA is defined in the same way as DFA but with the following two
exceptions, it contains multiple next states, and it contains ε transition.
Non Deterministic Finite Automata 62
 In the following image, we can see that from state q0 for input a, there
are two next states q1 and q2, similarly, from q0 for input b, the next
states are q0 and q1. Thus it is not fixed or determined that with a
particular input where to go next. Hence this FA is called non-
deterministic finite automata.
Examples of NFA 63
 Example 1: Design a NFA for the transition table as given below:
Examples of NFA 64
Examples of NFA 65
 Example 2: Design an NFA with ∑ = {0, 1} accepts all string ending with 01.
 Solution :
NFA to DFA Conversion 66

 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.

 Let, M = (Q, ∑, δ, q0, F) is an NFA which accepts the language L(M).


There should be equivalent DFA denoted by M' = (Q', ∑', q0', δ', F') such
that L(M) = L(M').
NFA to DFA Conversion 67

 Steps for converting NFA to DFA:


 Step 1: Initially Q' = ϕ
 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)
NFA to DFA Conversion 68

 Example 1: Convert the given NFA to DFA.

Solution: For the given transition diagram we will first construct the


transition table.
NFA to DFA Conversion 69
State 0 1
→ q0 q0 q1
q1 {q1, q2} q1
*q2 q2 {q1, q2}

Now we will obtain δ' transition for state q0.


δ'([q0], 0) = [q0]  
δ'([q0], 1) = [q1] 
The δ' transition for state q1 is obtained as:
δ'([q1], 0) = [q1, q2]       (new state generated)  
δ'([q1], 1) = [q1]  
The δ' transition for state q2 is obtained as:
δ'([q2], 0) = [q2]  
δ'([q2], 1) = [q1, q2]  
NFA to DFA Conversion 70
 Now we will obtain δ' transition on [q1, q2].
δ'([q1, q2], 0) = δ(q1, 0) ∪ δ(q2, 0)  
                      = {q1, q2} ∪ {q2}  
                   = [q1, q2]  
 δ'([q1, q2], 1) = δ(q1, 1) ∪ δ(q2, 1)  
                      = {q1} ∪ {q1, q2}  
                      = {q1, q2}  
                    = [q1, q2]  
 The state [q1, q2] is the final state as well because it contains a
final state q2.
NFA to DFA Conversion 71
 The transition table for the constructed DFA will be:

State 0 1

→[q0] [q0] [q1]

[q1] [q1, q2] [q1]

*[q2] [q2] [q1, q2]

*[q1, q2] [q1, q2] [q1, q2]


NFA to DFA Conversion 72
 The transition graph for the constructed DFA will be:
NFA-^ To NFA 73
 NFA with ε can be converted to NFA without ε, and this NFA without ε
can be converted to DFA. To do this, we will use a method, which can
remove all the ε transition from given NFA. The method will be:
 Find out all the ε transitions from each state from Q. That will be called
as ε-closure{q1} where qi ∈ Q.
 Then δ' transitions can be obtained. The δ' transitions mean a ε-closure
on δ moves.
 Repeat Step-2 for each input symbol and each state of given NFA.
 Using the resultant states, the transition table for equivalent NFA without
ε can be built.
NFA-^ To NFA 74
 Example: Convert the following NFA with ε to NFA without ε.

Solutions: We will first obtain ε-closures of q0, q1 and q2 as follows:


ε-closure(q0) = {q0}  
ε-closure(q1) = {q1, q2}  
ε-closure(q2) = {q2}  
NFA-^ To NFA 75
 Now the δ' transition on each input symbol is obtained as:
δ'(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))  
            = Ф  
NFA-^ To NFA 76
 Now the δ' transition on q1 is obtained as:
δ'(q1, a) = ε-closure(δ(δ^(q1, ε),a))  
            = ε-closure(δ(ε-closure(q1),a))  
     = ε-closure(δ(q1, q2), a)  
 = ε-closure(δ(q1, a) ∪ δ(q2, a))  
= ε-closure(Ф ∪ Ф)  
  = Ф  
δ'(q1, b) = ε-closure(δ(δ^(q1, ε),b))  
   = ε-closure(δ(ε-closure(q1),b))  
   = ε-closure(δ(q1, q2), b)  
    = ε-closure(δ(q1, b) ∪ δ(q2, b))  
   = ε-closure(Ф ∪ q2)  
     = {q2}  
NFA-^ To NFA 77
 The δ' transition on q2 is obtained as:
δ'(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}  
NFA-^ To NFA 78
 Now we will summarize all the computed δ' transitions:
δ'(q0, a) = {q0, q1}  
δ'(q0, b) = Ф  
δ'(q1, a) = Ф  
δ'(q1, b) = {q2}  
δ'(q2, a) = Ф  
δ'(q2, b) = {q2}  
NFA-^ To NFA 79
 The transition table can be:

States a b

→q0 {q1, q2} Ф

*q1 Ф {q2}

*q2 Ф {q2}
NFA-^ To NFA 80
 The transition graph can be:

State q1 and q2 become the final state as ε-closure of q1 and q2


contain the final state q2. The NFA can be shown by the
following transition diagram.
RE To NFA Conversion 81
 Common regular expression used in make ∈-NFA:
RE To NFA Conversion 82

Example: Create a ∈-NFA for regular expression: (a/b)*a


Solution :

You might also like