You are on page 1of 103

Non-Deterministic

Finite Automata

Fall 2006 Costas Busch - RPI 1


Nondeterministic Finite Automaton (NFA)

Alphabet = {a}

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 2


Alphabet = {a}

Two choices q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 3


Alphabet = {a}

Two choices a
q1 q2 No transition
a
q0
a
q3 No transition

Fall 2006 Costas Busch - RPI 4


First Choice

a a

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 5


First Choice

a a

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 6


First Choice

a a
All input is consumed

q1 a q2 “accept”
a
q0
a
q3

Fall 2006 Costas Busch - RPI 7


Second Choice

a a

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 8


Second Choice

a a
Input cannot be consumed

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

Fall 2006 Costas Busch - RPI 9


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

i.e., all the input string is processed and the


automaton is in an accepting state

Fall 2006 Costas Busch - RPI 10


aa is accepted by the NFA:

“accept”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 q3 “reject”
because this
this computation
computation
is ignored
accepts aa
Fall 2006 Costas Busch - RPI 11
Rejection example

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 12


First Choice

a
“reject”
q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 13


Second Choice

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 14


Second Choice

q1 a q2
a
q0
a
q3 “reject”

Fall 2006 Costas Busch - RPI 15


Another Rejection example

a a a

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 16


First Choice

a a a

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 17


First Choice

a a a
Input cannot be consumed

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

Fall 2006 Costas Busch - RPI 18


Second Choice

a a a

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 19


Second Choice

a a a
Input cannot be consumed

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

Fall 2006 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 2006 Costas Busch - RPI 21
a is rejected by the NFA:

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

All possible computations lead to rejection


Fall 2006 Costas Busch - RPI 22
aaa is rejected by the NFA:

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

All possible computations lead to rejection


Fall 2006 Costas Busch - RPI 23
Language accepted: L  {aa}

q1 a q2
a
q0
a
q3

Fall 2006 Costas Busch - RPI 24


Lambda Transitions

q0 a q1  q2 a q3

Fall 2006 Costas Busch - RPI 25


a a

q0 a q1  q2 a q3

Fall 2006 Costas Busch - RPI 26


a a

q0 a q1  q2 a q3

Fall 2006 Costas Busch - RPI 27


input tape head does not move

a a

q0 a q1  q2 a q3

Fall 2006 Costas Busch - RPI 28


all input is consumed

a a

“accept”

q0 a q1  q2 a q3

String aa is accepted
Fall 2006 Costas Busch - RPI 29
Rejection Example

a a a

q0 a q1  q2 a q3

Fall 2006 Costas Busch - RPI 30


a a a

q0 a q1  q2 a q3

Fall 2006 Costas Busch - RPI 31


(read head doesn’t move)

a a a

q0 a q1  q2 a q3

Fall 2006 Costas Busch - RPI 32


Input cannot be consumed

a a a

Automaton halts
“reject”

q0 a q1  q2 a q3

String aaa is rejected


Fall 2006 Costas Busch - RPI 33
Language accepted: L  {aa}

q0 a q1  q2 a q3

Fall 2006 Costas Busch - RPI 34


Another NFA Example

q0 a q1 b q2  q3


Fall 2006 Costas Busch - RPI 35
a b

q0 a q1 b q2  q3


Fall 2006 Costas Busch - RPI 36
a b

q0 a q1 b q2  q3


Fall 2006 Costas Busch - RPI 37
a b

“accept”

q0 a q1 b q2  q3


Fall 2006 Costas Busch - RPI 38
Another String

a b a b

q0 a q1 b q2  q3


Fall 2006 Costas Busch - RPI 39
a b a b

q0 a q1 b q2  q3


Fall 2006 Costas Busch - RPI 40
a b a b

q0 a q1 b q2  q3


Fall 2006 Costas Busch - RPI 41
a b a b

q0 a q1 b q2  q3


Fall 2006 Costas Busch - RPI 42
a b a b

q0 a q1 b q2  q3


Fall 2006 Costas Busch - RPI 43
a b a b

q0 a q1 b q2  q3


Fall 2006 Costas Busch - RPI 44
a b a b

“accept”

q0 a q1 b q2  q3


Fall 2006 Costas Busch - RPI 45
Language accepted

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



 ab

q0 a q1 b q2  q3


Fall 2006 Costas Busch - RPI 46
Another NFA Example

0
q0 q1 0, 1 q2
1

Fall 2006 Costas Busch - RPI 47
Language accepted

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


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

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

•Simple automata:

M1 M2
q0 q0

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


Fall 2006 Costas Busch - RPI 49
•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 2006 Costas Busch - RPI 50
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 2006 Costas Busch - RPI 51
Transition Function 

 q , x   q1, q2,, qk 

q1
x resulting states with
q x
q1 following one transition
x
with symbol x

qk
Fall 2006 Costas Busch - RPI 52
 q0 , 1  q1

0
q0 q1 0, 1 q
2
1

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

0
q0 q1 0, 1 q
2
1

Fall 2006 Costas Busch - RPI 54
 (q0 ,  )  {q2 }

0
q0 q1 0, 1 q
2
1

Fall 2006 Costas Busch - RPI 55
 (q2 ,1)  

0
q0 q1 0, 1 q
2
1

Fall 2006 Costas Busch - RPI 56
*
Extended Transition Function 
Same with  but applied on strings

 q0 , a   q1 
*

q4 q5
a a
q0 a q1 b q2  q3

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

q4 q5
a a
q0 a q1 b q2  q3

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

q4 q5
a a
q0 a q1 b q2  q3

Fall 2006 Costas Busch - RPI 59
Special case:

for any state q

q   q ,  
*

Fall 2006 Costas Busch - RPI 60


In general
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 2006 Costas Busch - RPI 61


The Language of an NFA M
The language accepted by M is:

LM   w1,w2,...wn 

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


*

and there is some qk  F (accepting state)


Fall 2006 Costas Busch - RPI 62
wm  LM 
 (q0 ,w m )
*

qi
wm

q0 w
qk qk  F
m

wm qj

Fall 2006 Costas Busch - RPI 63


F  q0 ,q5 
q4 q5
a a
q0 a q1 b q2  q3


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


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


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

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


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

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

Fall 2006 Costas Busch - RPI 68


NFAs accept the Regular
Languages

Fall 2006 Costas Busch - RPI 69


Equivalence of Machines

Definition:

Machine M1 is equivalent to machine M2

if L M1   L M 2 

Fall 2006 Costas Busch - RPI 70


Example of equivalent machines

NFA M1
LM1   {10} * 0
q0 q1
1

DFA M2 0,1
LM 2   {10} * 0
q0 q1 1 q2
1
0
Fall 2006 Costas Busch - RPI 71
Theorem:


Languages
Regular
accepted
Languages
by NFAs
Languages
accepted
by DFAs

NFAs and DFAs have the same computation power,


accept the same set of languages
Fall 2006 Costas Busch - RPI 72
Proof: we only need to show

Languages
accepted
by NFAs
 Regular
Languages

AND
Languages
accepted  Regular
Languages
by NFAs

Fall 2006 Costas Busch - RPI 73


Proof-Step 1

Languages
accepted
by NFAs
 Regular
Languages

Every DFA is trivially an NFA

Any language L accepted by a DFA


is also accepted by an NFA
Fall 2006 Costas Busch - RPI 74
Proof-Step 2

Languages
accepted
by NFAs
 Regular
Languages

Any NFA can be converted to an


equivalent DFA

Any language L accepted by an NFA


is also accepted by a DFA
Fall 2006 Costas Busch - RPI 75
Conversion NFA to DFA
NFA M
a
q a q 0
 q 1 2
b

DFA M
q0 

Fall 2006 Costas Busch - RPI 76


 * (q0 , a )  {q1 , q2 }
NFA M a
q0 a q1  q2
b

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

Fall 2006 Costas Busch - RPI 77


 * (q0 , b )   empty set

NFA M a
q0 a q1  q2
b

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

 trap state
Fall 2006 Costas Busch - RPI 78
 (q1 , a )  {q1 , q2 }
*

NFA M a  * (q2 , a )  

q0 a q1  q2 union

b q1,q2

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


Fall 2006 Costas Busch - RPI 79
 (q1 , b )  {q0 }
*

NFA M a  * (q2 , b )  {q0 }


a  union
q0 q1 q2
b q0 

a
DFA M b

q0  a
q1,q2
b


Fall 2006 Costas Busch - RPI 80
NFA M a
q0 a q1  q2
b

a
DFA M b

q0  a
q1,q2
b

 a, b trap state
Fall 2006 Costas Busch - RPI 81
END OF CONSTRUCTION

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

q0  a
q1,q2
b
q1, q2  F 

 a, b
Fall 2006 Costas Busch - RPI 82
General Conversion Procedure

Input: an NFA M

Output: an equivalent DFA M 


with LM   L(M )

Fall 2006 Costas Busch - RPI 83


The NFA has states q0 , q1, q2 ,...

The DFA has states from the power set

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

Fall 2006 Costas Busch - RPI 84


Conversion Procedure Steps

step
1. Initial state of NFA: q0

Initial state of DFA: q0 

Fall 2006 Costas Busch - RPI 85


Example
NFA M a
q0 a q1  q2
b

DFA M
q0 

Fall 2006 Costas Busch - RPI 86


step
2. For every DFA’s state {qi , q j ,..., qm }

compute in the NFA


 * qi , a 

  * qj ,a  Union
 {qk , ql,..., qn }
...
  * qm , a 
add transition to DFA
 {qi , qj ,..., qm }, a   {qk , ql,..., qn }
Fall 2006 Costas Busch - RPI 87
Example  * (q0 , a)  {q1, q2}
NFA M a
q0 a q1  q2
b

 q0 , a   q1, q2 
DFA M
q0  a
q1,q2

Fall 2006 Costas Busch - RPI 88


step
3. Repeat Step 2 for every state in DFA and
symbols in alphabet until no more states
can be added in the DFA

Fall 2006 Costas Busch - RPI 89


Example
NFA M a
q0 a q1  q2
b

a
DFA M b

q0  a
q1,q2
b

 a, b
Fall 2006 Costas Busch - RPI 90
step
4. 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 2006 Costas Busch - RPI 91


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

q0  a
q1,q2
b
q1, q2  F 

 a, b
Fall 2006 Costas Busch - RPI 92
Lemma:
If we convert NFA M to DFA M 
then the two automata are equivalent:
L M   L M  

Proof:
We only need to show: L M   L  M  
AND
L M   L  M  
Fall 2006 Costas Busch - RPI 93
First we show: L M   L  M  

We only need to prove:

w L(M ) w  L(M )

Fall 2006 Costas Busch - RPI 94


NFA
Consider w L(M )

q0 w qf

symbols
w  1 2  k
1 2 k
q0 qf

Fall 2006 Costas Busch - RPI 95


symbol
i
qi qj

denotes a possible sub-path like

symbol
  i 
qi qj

Fall 2006 Costas Busch - RPI 96


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 ,}
state
w  L(M ) state
label label
Fall 2006 Costas Busch - RPI 97
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

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

Fall 2006 Costas Busch - RPI 98


Proof by induction on |v|

Induction Basis: |v | 1 v  a1

a1
NFA M: q0 qi

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

is true by construction of M 

Fall 2006 Costas Busch - RPI 99


Induction hypothesis: 1 | v | k
v  a1a2 ak

Suppose that the following hold


a1 a2 ak
NFA M: q0 qi qj qc qd

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

Fall 2006 Costas Busch - RPI 100


Induction Step: | v | k  1
v  a1a2 ak ak 1  vak 1
 
v
Then this is true by construction of M 
a1 a2 ak ak 1
NFA M: q0 qi qj qc qd qe

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

Fall 2006
v
Costas Busch - RPI 101
Therefore if w L(M )
w  1 2  k
1 2 k
q0 qf
NFA M:
then

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

With a similar proof


we can show: L M   L  M  

Therefore: LM   LM 

END OF LEMMA PROOF


Fall 2006 Costas Busch - RPI 103

You might also like