0% found this document useful (0 votes)
255 views115 pages

Understanding Non-Deterministic Finite Automata

The document describes non-deterministic finite automata (NFA) using examples. It explains that an NFA can have multiple possible state transitions for a given input symbol, unlike a deterministic finite automaton which has a single transition. It provides examples to show that a string is accepted if there is a computation path where all input is consumed and the automaton reaches an accepting state, and rejected if no such path exists. Lambda transitions, which do not consume input, are also discussed.

Uploaded by

tariqravian
Copyright
© Attribution Non-Commercial (BY-NC)
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)
255 views115 pages

Understanding Non-Deterministic Finite Automata

The document describes non-deterministic finite automata (NFA) using examples. It explains that an NFA can have multiple possible state transitions for a given input symbol, unlike a deterministic finite automaton which has a single transition. It provides examples to show that a string is accepted if there is a computation path where all input is consumed and the automaton reaches an accepting state, and rejected if no such path exists. Lambda transitions, which do not consume input, are also discussed.

Uploaded by

tariqravian
Copyright
© Attribution Non-Commercial (BY-NC)
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

Non-Deterministic

Finite Automata

Fall 2005 Costas Busch - RPI 1


Nondeterministic Finite Automaton (NFA)

Alphabet = {a}

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 2


Alphabet = {a}

Two choices q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 3


Alphabet = {a}

Two choices q1 a q2 No transition


a
q0
a
q3 No transition

Fall 2005 Costas Busch - RPI 4


First Choice

a a

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 5


First Choice

a a

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 6


First Choice

a a

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 7


First Choice

a a
All input is consumed
q1 a q2 “accept”
a
q0
a
q3

Fall 2005 Costas Busch - RPI 8


Second Choice

a a

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 9


Second Choice

a a

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 10


Second Choice

a a

q1 a q2
a
q0
a
No transition:
q3
the automaton hangs

Fall 2005 Costas Busch - RPI 11


Second Choice

a a
Input cannot be consumed
q1 a q2
a
q0
a
q3 “reject”

Fall 2005 Costas Busch - RPI 12


An NFA accepts a string:
if there is a computation of the NFA
that accepts the string

i.e., all the input is consumed and the automaton


is in an accepting state

Fall 2005 Costas Busch - RPI 13


Example

aa is accepted by the NFA:

“accept”
q1 a q2 q1 a q2
a a
q0
a
q0
a
q3 q3 “reject”
because this
computation
accepts aa
Fall 2005 Costas Busch - RPI 14
Rejection example

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 15


First Choice

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 16


First Choice

a
“reject”
q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 17


Second Choice

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 18


Second Choice

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 19


Second Choice

q1 a q2
a
q0
a
q3 “reject”

Fall 2005 Costas Busch - RPI 20


An NFA rejects a string:
if there is no computation of the NFA
that accepts the string.

For each computation:


• All the input is consumed and the
automaton is in a non final state

OR
• The input cannot be consumed
Fall 2005 Costas Busch - RPI 21
Example

a is rejected by the NFA:

“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3
“reject” q3

All possible computations lead to rejection


Fall 2005 Costas Busch - RPI 22
Rejection example

a a a

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 23


First Choice

a a a

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 24


First Choice

a a a

q1 a q2
a
q0 No transition:
a
the automaton hangs
q3

Fall 2005 Costas Busch - RPI 25


First Choice

a a a
Input cannot be consumed
q1 a q2 “reject”
a
q0
a
q3

Fall 2005 Costas Busch - RPI 26


Second Choice

a a a

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 27


Second Choice

a a a

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 28


Second Choice

a a a

q1 a q2
a
q0
a
No transition:
q3
the automaton hangs

Fall 2005 Costas Busch - RPI 29


Second Choice

a a a
Input cannot be consumed
q1 a q2
a
q0
a
q3 “reject”

Fall 2005 Costas Busch - RPI 30


aaa is rejected by the NFA:

“reject”
q1 a q2 q1 a q2
a a
q0
a
q0
a
q3 q3 “reject”

All possible computations lead to rejection


Fall 2005 Costas Busch - RPI 31
Language accepted: L  {aa}

q1 a q2
a
q0
a
q3

Fall 2005 Costas Busch - RPI 32


Lambda Transitions

q0 a q1  q2 a q3

Fall 2005 Costas Busch - RPI 33


a a

q0 a q1  q2 a q3

Fall 2005 Costas Busch - RPI 34


a a

q0 a q1  q2 a q3

Fall 2005 Costas Busch - RPI 35


(read head does not move)

a a

q0 a q1  q2 a q3

Fall 2005 Costas Busch - RPI 36


a a

q0 a q1  q2 a q3

Fall 2005 Costas Busch - RPI 37


all input is consumed

a a

“accept”

q0 a q1  q2 a q3

String aa is accepted
Fall 2005 Costas Busch - RPI 38
Rejection Example

a a a

q0 a q1  q2 a q3

Fall 2005 Costas Busch - RPI 39


a a a

q0 a q1  q2 a q3

Fall 2005 Costas Busch - RPI 40


(read head doesn’t move)

a a a

q0 a q1  q2 a q3

Fall 2005 Costas Busch - RPI 41


a a a

q0 a q1  q2 a q3

No transition:
the automaton hangs
Fall 2005 Costas Busch - RPI 42
Input cannot be consumed

a a a

“reject”

q0 a q1  q2 a q3

String aaa is rejected


Fall 2005 Costas Busch - RPI 43
Language accepted: L  {aa}

q0 a q1  q2 a q3

Fall 2005 Costas Busch - RPI 44


Another NFA Example

q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 45
a b

q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 46
a b

q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 47
a b

q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 48
a b

“accept”
q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 49
Another String

a b a b

q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 50
a b a b

q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 51
a b a b

q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 52
a b a b

q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 53
a b a b

q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 54
a b a b

q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 55
a b a b

q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 56
a b a b

“accept”
q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 57
Language accepted

L   ab, abab, ababab, ...



  ab

q0 a q1 b q2  q3


Fall 2005 Costas Busch - RPI 58
Another NFA Example

0
q0 q1 0, 1 q2
1

Fall 2005 Costas Busch - RPI 59
Language accepted

L(M ) = { λ, 10, 1010, 101010, ...}


= { 10} *
0
q0 q1 0, 1 q2
1 (redundant
state)

Fall 2005 Costas Busch - RPI 60
Remarks:
•The  symbol never appears on the
input tape

•Simple automata:

M1 M2
q0 q0

L(M1 ) = {} L(M 2 ) = {λ}


Fall 2005 Costas Busch - RPI 61
•NFAs are interesting because we can
express languages easier than DFAs

NFA M1 DFA M2 a
q2
q0 a q1
a
q0 a q1

L( M1 ) = {a} L( M 2 ) = {a}
Fall 2005 Costas Busch - RPI 62
Formal Definition of NFAs
M   Q, ,  , q0 , F 

Q : Set of states, i.e.  q0 , q1, q2 


 : Input aplhabet, i.e.  a, b
: Transition function

q0 : Initial state

F : Accepting states
Fall 2005 Costas Busch - RPI 63
Transition Function 
Is the result of following exactly
one transition

  q0 , 1   q1
0
q0 q1 0, 1 q
2
1

Fall 2005 Costas Busch - RPI 64
 (q1,0)  {q0 , q2}

0
q0 q1 0, 1 q
2
1

Fall 2005 Costas Busch - RPI 65
 (q0 ,  )  {q2 }

0
q0 q1 0, 1 q
2
1

Fall 2005 Costas Busch - RPI 66
 (q2 ,1)  

0
q0 q1 0, 1 q
2
1

Fall 2005 Costas Busch - RPI 67
Extended Transition Function  *

 *  q0 , a    q1

q4 q5
a a
q0 a q1 b q2  q3

Fall 2005 Costas Busch - RPI 68
 *  q0 , aa    q4 , q5 

q4 q5
a a
q0 a q1 b q2  q3

Fall 2005 Costas Busch - RPI 69
 *  q0 , ab    q2 , q3 , q0 

q4 q5
a a
q0 a q1 b q2  q3

Fall 2005 Costas Busch - RPI 70
Formally
q j   *  qi , w : there is a walk from qi to q j
with label w

qi w qj

w   1 2  k
1 2 k
qi qj

Fall 2005 Costas Busch - RPI 71


The Language of an NFA M
F   q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3

 *  q0 , aa    q4 , q5 aa  L(M )
F
Fall 2005 Costas Busch - RPI 72
F   q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3

 *  q0 , ab    q2 , q3 , q0  ab  L M 
F
Fall 2005 Costas Busch - RPI 73
F   q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3

 *  q0 , abaa    q4 , q5 aaba  L(M )


F
Fall 2005 Costas Busch - RPI 74
F   q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3

 *  q0 , aba    q1 aba  L M 


F
Fall 2005 Costas Busch - RPI 75
q4 q5
a a
q0 a q1 b q2  q3

L  M   ab  *   ab  * {aa }

Fall 2005 Costas Busch - RPI 76


Formally
The language accepted by NFA M is:
L M    w1, w2 , w3 ,...

where  * (q0 , wm )  {qi , q j ,..., qk ,}

and there is some qk  F (accepting state)


Fall 2005 Costas Busch - RPI 77
w L M   * (q0 , w)
qi
w1

q0
qk qk  F
w2

w3 qj

Fall 2005 Costas Busch - RPI 78


NFAs accept the Regular
Languages

Fall 2005 Costas Busch - RPI 79


Equivalence of Machines

Definition:

Machine M1 is equivalent to machine M 2

if L M 1   L M 2 

Fall 2005 Costas Busch - RPI 80


Example of equivalent machines
NFA M1
0
L M1   {10} *
q0 q1
1

DFA M2 0,1
0
L M 2   {10} *
q0 q1 1 q2
1
0
Fall 2005 Costas Busch - RPI 81
We will prove:


Languages
Regular
accepted
Languages
by NFAs
Languages
accepted
by DFAs
NFAs and DFAs have the
same computation power
Fall 2005 Costas Busch - RPI 82
We will show:

Languages
accepted  Regular
Languages
by NFAs

Languages
accepted  Regular
Languages
by NFAs

Fall 2005 Costas Busch - RPI 83


Proof-Step 1

Languages
accepted  Regular
Languages
by NFAs

Proof: Every DFA is trivially an NFA

Any language L accepted by a DFA


is also accepted by an NFA
Fall 2005 Costas Busch - RPI 84
Proof-Step 2

Languages
accepted  Regular
Languages
by NFAs

Proof: Any NFA can be converted to an


equivalent DFA

Any language L accepted by an NFA


is also accepted by a DFA
Fall 2005 Costas Busch - RPI 85
Convert NFA to DFA
a
NFA M
q a q 0
 q 1 2
b

DFA M
 q0 

Fall 2005 Costas Busch - RPI 86


Convert NFA to DFA
a
NFA M
q a q 0
 q 1 2
b

DFA M
 q0  a
 q1, q2 

Fall 2005 Costas Busch - RPI 87


Convert NFA to DFA
a
NFA M
q a q 0
 q 1 2
b

DFA M
 q0  a
 q1, q2 
b


Fall 2005 Costas Busch - RPI 88
Convert NFA to DFA
a
NFA M
q a q 0
 q 1 2
b

a
DFA M
 q0  a
 q1, q2 
b


Fall 2005 Costas Busch - RPI 89
Convert NFA to DFA
a
NFA M
q a q 0
 q 1 2
b

b a
DFA M
 q0  a
 q1, q2 
b


Fall 2005 Costas Busch - RPI 90
Convert NFA to DFA
a
NFA M
q a q 0
 q 1 2
b

b a
DFA M
 q0  a
 q1, q2 
b

 a, b
Fall 2005 Costas Busch - RPI 91
Convert NFA to DFA
a
NFA M
q a q 0
 q 1 2
b
L M   L(M )
a
DFA M b

 q0  a
 q1, q2 
b

 a, b
Fall 2005 Costas Busch - RPI 92
NFA to DFA: Remarks

We are given an NFA M

We want to convert it
to an equivalent DFA M 

With L M   L(M )
Fall 2005 Costas Busch - RPI 93
If the NFA has states

q0 , q1, q2 ,...

the DFA has states in the powerset

,  q0  ,  q1 ,  q1, q2  ,  q3 , q4 , q7  ,....

Fall 2005 Costas Busch - RPI 94


Procedure NFA to DFA

1. Initial state of NFA: q0

Initial state of DFA:  q0 

Fall 2005 Costas Busch - RPI 95


Example
M a
NFA
q0 a q1  q2
b

DFA M
 q0 

Fall 2005 Costas Busch - RPI 96


Procedure NFA to DFA
2. For every DFA’s state {qi , q j ,..., qm }

Compute in the NFA


 *  qi , a  ,
 *  q j , a ,  {qi , qj ,..., qm
}
...
Add transition to DFA
 {qi , q j ,..., qm }, a   {qi , qj ,..., qm
}
Fall 2005 Costas Busch - RPI 97
Exampe
M a
NFA
q0 a q1  q2
b
 * (q0 , a )  {q1, q2 }

DFA M 

 q0  a
 q1, q2 

   q0  , a    q1, q2 
Fall 2005 Costas Busch - RPI 98
Procedure NFA to DFA

Repeat Step 2 for all letters in alphabet,


until
no more transitions can be added.

Fall 2005 Costas Busch - RPI 99


Example
M a
NFA
q0 a q1  q2
b

b a
DFA M
 q0  a
 q1, q2 
b

 a, b
Fall 2005 Costas Busch - RPI 100
Procedure NFA to DFA
3. For any DFA state {qi , q j ,..., qm }

If some q j is accepting state in NFA

Then, {qi , q j ,..., qm }


is accepting state in DFA

Fall 2005 Costas Busch - RPI 101


Example
M a
NFA
q0 a q1  q2 q1  F
b
a
DFA M b

 q0  a
 q1, q2 
b  q1, q2   F 
 a, b
Fall 2005 Costas Busch - RPI 102
Theorem
Take NFA M

Apply conversion procedure


to obtain DFA M 

Then M and M are equivalent :

L M   L M  

Fall 2005 Costas Busch - RPI 103


Proof

L M   L M 

L M   L M   AND L M   L M  

Fall 2005 Costas Busch - RPI 104


First we show: L M   L M  

Take arbitrary: w L(M )

We will prove: w  L(M )

Fall 2005 Costas Busch - RPI 105


w L(M )

NFA M: q0 w qf

w   1 2  k
1 2 k
NFA M: q0 qf

Fall 2005 Costas Busch - RPI 106


i
qi qj

denotes

  i 
qi qj

Fall 2005 Costas Busch - RPI 107


We will show that if w L(M )

w   1 2  k
1 2 k
NFA M: q0 qf

then

1 2 k
DFA M:
{q0 } {q f ,}
w  L(M )
Fall 2005 Costas Busch - RPI 108
More generally, we will show that if in M:
(arbitrary string) v  a1a2 an
a1 a2 an
NFA M: q0 qi qj ql qm

then

a1 a2 an
DFA M:
{q0} {qi ,} {q j ,} {ql ,} {qm ,}

Fall 2005 Costas Busch - RPI 109


Proof by induction on |v|

Induction Basis: v  a1

a1
NFA M: q0 qi

a1
DFA M:
{q0 } {qi ,}

Is true by construction of M:


Fall 2005 Costas Busch - RPI 110
Induction hypothesis: 1 | v | k
v  a1a2 ak
a1 a2 ak
M: q0 qi qj qc qd
NFA

a1 a2 ak
DFA M:
{q0} {qi ,} {q j ,} {qc ,} {qd ,}

Fall 2005 Costas Busch - RPI 111


Induction Step: | v | k  1
v  a1a2 ak ak 1  vak 1
   
v

a1 a2 ak
M: q0 qi qj qc qd
NFA

v
a1 a2 ak
DFA M:
{q0 } {qi ,} {q j ,} {qc ,} {qd ,}

Fall 2005 Costas Busch - RPI


v 112
Induction Step: | v | k  1
v  a1a2 ak ak 1  vak 1
   
v

a1 a2 ak ak 1
NFA M: q0 qi qj qc qd qe

v
a1 a2 ak ak 1
DFA M:
{q0} {qi ,} {q j ,} {qc ,} {qd ,} {qe ,}

Fall 2005
v
Costas Busch - RPI 113
Therefore if w L(M )

w   1 2  k
1 2 k
NFA M: q0 qf

then

1 2 k
DFA M:
{q0 } {q f ,}
w  L(M )
Fall 2005 Costas Busch - RPI 114
We have shown: L M   L M  

We also need to show: L M   L M  

(proof is similar and omitted)

Fall 2005 Costas Busch - RPI 115

You might also like