```Non-Deterministic
Finite Automata
Costas Busch - LSU
1
Nondeterministic Finite Automaton (NFA)
Alphabet = {a }
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
2
Alphabet = {a }
Two choices
a
q0
q1
a
q 2 No transition
a
q 3 No transition
Costas Busch - LSU
3
First Choice
a
a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
4
First Choice
a
a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
5
First Choice
a
a
All input is consumed
a
q0
q1
a
q2
“accept”
a
q3
Costas Busch - LSU
6
Second Choice
a
a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
7
Second Choice
a
a
Input cannot be consumed
a
q0
a
q1
a
q2
Automaton Halts
q 3 “reject”
Costas Busch - LSU
8
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
Costas Busch - LSU
9
aa is accepted by the NFA:
“accept”
a
q0
q1
a
q2
a
q0
a
q3
because this
computation
accepts aa
q1
a
q3
a
q2
“reject”
this computation
is ignored
Costas Busch - LSU
10
Rejection example
a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
11
First Choice
a
“reject”
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
12
Second Choice
a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
13
Second Choice
a
a
q0
q1
a
q2
a
q3
“reject”
Costas Busch - LSU
14
Another Rejection example
a
a
a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
15
First Choice
a
a
a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
16
First Choice
a
a
a
Input cannot be consumed
a
q0
q1
a
a
q2
“reject”
Automaton halts
q3
Costas Busch - LSU
17
Second Choice
a
a
a
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
18
Second Choice
a
a
a
Input cannot be consumed
a
q0
q1
a
q2
Automaton halts
a
q3
“reject”
Costas Busch - LSU
19
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 accepting state
OR
• The input cannot be consumed
Costas Busch - LSU
20
a is rejected by the NFA:
“reject”
a
q0
q1
a
q2
a
q0
a
q3
“reject”
q1
a
q2
a
q3
All possible computations lead to rejection
Costas Busch - LSU
21
aaa is rejected by the NFA:
“reject”
a
q0
q1
a
q2
a
q0
a
q3
q1
a
q3
a
q2
“reject”
All possible computations lead to rejection
Costas Busch - LSU
22
Language accepted: L  {aa }
a
q0
q1
a
q2
a
q3
Costas Busch - LSU
23
Lambda Transitions
q0
a
q1

q2
Costas Busch - LSU
a
q3
24
a
a
q0
a
q1

q2
Costas Busch - LSU
a
q3
25
a
a
q0
a
q1

q2
Costas Busch - LSU
a
q3
26
input tape head does not move
a
a
q0
a
q1

q2
a
q3
Automaton changes state
Costas Busch - LSU
27
all input is consumed
a
a
“accept”
q0
a
q1

q2
a
q3
String aa is accepted
Costas Busch - LSU
28
Rejection Example
a
a
a
q0
a
q1

q2
Costas Busch - LSU
a
q3
29
a
a
a
q0
a
q1

q2
Costas Busch - LSU
a
q3
30
a
a
a
q0
a
q1

q2
Costas Busch - LSU
a
q3
31
Input cannot be consumed
a
a
a
Automaton halts
“reject”
q0
a
String aaa
q1

q2
a
q3
is rejected
Costas Busch - LSU
32
Language accepted: L  {aa }
q0
a
q1

q2
Costas Busch - LSU
a
q3
33
Another NFA Example
q0
a
b
q1
q2

q3

Costas Busch - LSU
34
a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
35
a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
36
a b
“accept”
q0
a
b
q1
q2

q3

Costas Busch - LSU
37
Another String
a b a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
38
a b a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
39
a b a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
40
a b a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
41
a b a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
42
a b a b
q0
a
b
q1
q2

q3

Costas Busch - LSU
43
a b a b
“accept”
q0
a
b
q1
q2

q3

Costas Busch - LSU
44
Language accepted
L  ab , abab , ababab , ... 
 ab 
q0
a

b
q1
q2

q3

Costas Busch - LSU
45
Another NFA Example
0
q0
1
q1
0, 1
q2

Costas Busch - LSU
46
Language accepted
L ( M )   , 10 , 1010 , 101010 , ... 
 10 *
0
q0
1
q1
0, 1

Costas Busch - LSU
q2
(redundant
state)
47
Remarks:
•The  symbol never appears on the
input tape
•Simple automata:
M1
M2
q0
q0
L ( M 1 ) = {}
L ( M 2 ) = {λ}
Costas Busch - LSU
48
•NFAs are interesting because we can
express languages easier than DFAs
NFA M 1
q0
a
DFA M 2
a
q2
q1
a
q0
L ( M 1 ) = {a}
a
q1
L ( M 2 ) = {a}
Costas Busch - LSU
49
Formal Definition of NFAs
M  Q ,  ,  , q 0 , F 
Q : Set of states, i.e. q 0 , q1 , q 2 
:
Input aplhabet, i.e. a , b 
 
 : Transition function
q 0 : Initial state
F : Accepting states
Costas Busch - LSU
50
Transition Function 
 q , x   q 1 , q 2 ,  , q k 
q1
x
q
x
q2
x
resulting states with
following one transition
with symbol x
qk
Costas Busch - LSU
51
States reachable from q 0 scanning 1
  q 0 , 1   q1 
0
q0
1
q1
0, 1
q2

Costas Busch - LSU
52
States reachable from q 1 scanning 0
 ( q1 , 0 )  { q 0 , q 2 }
0
q0
1
q1
0, 1
q2

Costas Busch - LSU
53
States reachable from q 0 scanning nothing
 ( q0 ,  )  {q0 , q 2 }
0
q0
1
q1
0, 1
q2

Costas Busch - LSU
54
States reachable from q 2 scanning 1
 ( q 2 ,1)  
0
q0
1
q1
0, 1
q2

Costas Busch - LSU
55
Extended Transition Function 
*
Same with  but applied on strings

*
q 0 , a   q 1 
q5
q4
a
q0
a
a
b
q1
q2

q3

Costas Busch - LSU
56
States reachable from q 0 scanning aa

*
q 0 , aa   q 4 , q 5 
q5
q4
a
q0
a
a
b
q1
q2

q3

Costas Busch - LSU
57
States reachable from q 0 scanning ab

*
q 0 , ab   q 2 , q 3 , q 0 
q5
q4
a
q0
a
a
b
q1
q2

q3

Costas Busch - LSU
58
Special case:
for any state q
q 
*
q ,  
Costas Busch - LSU
59
In general
qj  
*
q i , w  : there is a walk from q i to q j
with label w
w
qi
qj
w   1 2   k
qi
1
k
2
Costas Busch - LSU
qj
60
The Language of an NFA M
The language accepted by
M
is:
L  M   w1 , w 2 ,..., w n 
Where for each w m
*
 (q 0 , w m )  { q i ,..., q k ,  , q j }
and there is some
q k  F (accepting state)
Costas Busch - LSU
61
w m  L M

*
 (q 0 , w m )
qi
wm
q0
qk
wm
wm
Costas Busch - LSU
qk  F
qj
62
F  q 0 , q 5 
q5
q4
a
q0
a
a
b
q1
q2

q3


*
q 0 , aa   q 4 , q5 
aa  L ( M )
F
Costas Busch - LSU
63
F  q 0 , q 5 
q5
q4
a
q0
a
a
b
q1
q2

q3


*
q 0 , ab   q 2 , q 3 , q 0 
ab  L  M 
F
Costas Busch - LSU
64
F  q 0 , q 5 
q5
q4
a
q0
a
a
b
q1
q2

q3


*
q 0 , abaa   q 4 , q5 
abaa  L ( M )
F
Costas Busch - LSU
65
F  q 0 , q 5 
q5
q4
a
q0
a
a
b
q1
q2

q3


*
 q 0 , aba   q1 
aba  L  M 
F
Costas Busch - LSU
66
q5
q4
a
q0
a
a
b
q1
q2

q3

L  M   ab   ab  { aa }
*
Costas Busch - LSU
*
67
NFAs accept the Regular
Languages
Costas Busch - LSU
68
Equivalence of Machines
Definition:
Machine M 1
is equivalent to machine M 2
if L  M 1   L  M 2 
Costas Busch - LSU
69
Example of equivalent machines
NFA M 1
L  M 1   {10 } *
0
q0
q1
1
DFA M 2
L  M 2   {10 } *
0 ,1
0
q0
1
q1
1
q2
0
Costas Busch - LSU
70
Theorem:
Languages
accepted
by NFAs

Regular
Languages
Languages
accepted
by DFAs
NFAs and DFAs have the same computation power,
namely, they accept the same set of languages
Costas Busch - LSU
71
Proof:
we need to show
Languages
accepted
by NFAs

Regular
Languages
AND
Languages
accepted
by NFAs

Costas Busch - LSU
Regular
Languages
72
Proof-Step 1
Languages
accepted
by NFAs

Regular
Languages
Every DFA is trivially a NFA
Any language L accepted by a DFA
is also accepted by a NFA
Costas Busch - LSU
73
Proof-Step 2
Languages
accepted
by NFAs

Regular
Languages
Any NFA can be converted to an
equivalent DFA
Any language L accepted by a NFA
is also accepted by a DFA
Costas Busch - LSU
74
Conversion of NFA to DFA
a
NFA M
q0
a

q1
q2
b
DFA M 
q 0 
Costas Busch - LSU
75
*
 (q 0 , a )  { q 1 , q 2 }
a
NFA M
q0
a

q1
q2
b
DFA M 
q 0 
a
q1 , q 2 
Costas Busch - LSU
76
empty set
*
 (q 0 , b )  
a
NFA M
q0
a

q1
q2
b
DFA M 
q 0 
a
q1 , q 2 

trap state
b
Costas Busch - LSU
77
*
 (q 1 , a )  { q 1 , q 2 }
*
 (q 2 , a )  
a
NFA M
q0
a

q1
union
q2
q1 , q 2 
b
a
DFA M 
a
q 0 
q1 , q 2 
b

Costas Busch - LSU
78
*
 (q 1 , b )  { q 0 }
*
 (q 2 , b )  { q 0 }
a
NFA M
q0
a

q1
union
q2
q 0 
b
DFA M 
a
b
a
q 0 
q1 , q 2 
b

Costas Busch - LSU
79
a
NFA M
q0
a

q1
q2
b
DFA M 
a
b
a
q 0 
q1 , q 2 
b

Costas Busch - LSU
a,b
trap state
80
END OF CONSTRUCTION
a
NFA M
q0
a

q1
q1  F
q2
b
a
DFA M 
b
a
q 0 
q1 , q 2 
b

Costas Busch - LSU
q1 , q 2   F 
a,b
81
General Conversion Procedure
Input: an NFA M
Output: an equivalent DFA M
with L  M   L ( M )
Costas Busch - LSU

82
The NFA has states
q 0 , q1 , q 2 ,...
The DFA has states from the power set
 , q 0 , q 1 , q 0 , q 1 , q 1 , q 2 , q 3 , ....
Costas Busch - LSU
83
Conversion Procedure Steps
step
1.
Initial state of NFA: q 0

*
q 0 ,    q 0 ,  
Initial state of DFA: q 0 ,  
Costas Busch - LSU
84
Example

a
NFA M
q0
a

q1
*
q 0 ,    q 0 
q2
b
DFA M 
q 0 
Costas Busch - LSU
85
step
2. For every DFA’s state { q i , q j ,..., q m }
compute in the NFA
 * q i , a 


  * qj ,a
...
  * q m , a
Union
 { q k , q l,..., q n }

 { q i , q j ,..., q m }, a   { q k , q l,..., q n }
Costas Busch - LSU
86
 * ( q 0 , a )  { q1 , q 2 }
Example
a
NFA M
a
q0

q1
q2
b
DFA M 
q 0 
a
q1 , q 2 
 q 0 , a   q1 , q 2 
Costas Busch - LSU
87
step
3. Repeat Step 2 for every state in DFA and
symbols in alphabet until no more states
can be added in the DFA
Costas Busch - LSU
88
Example
a
NFA M
q0
a

q1
q2
b
DFA M 
a
b
a
q 0 
q1 , q 2 
b

Costas Busch - LSU
a,b
89
step
4. For any DFA state
{ q i , q j ,..., q m }
if some q j is accepting state in NFA
Then, { q i , q j ,..., q m }
is accepting state in DFA
Costas Busch - LSU
90
Example
a
NFA M
q0
a

q1
q1  F
q2
b
a
DFA M 
b
a
q 0 
q1 , q 2 
b

Costas Busch - LSU
q1 , q 2   F 
a,b
91
Lemma:
If we convert NFA M to DFA M 
then the two automata are equivalent:
L  M   L  M 
Proof:
We need to show:
L  M   L  M 
AND
L  M   L  M 
Costas Busch - LSU
92
First we show:
L  M   L  M 
We only need to prove:
w  L(M )
w  L ( M )
Costas Busch - LSU
93
NFA
Consider
w  L(M )
w
q0
qf
symbols
w   1 2   k
q0
1
2
Costas Busch - LSU
k
qf
94
symbol
qi
i
qj
denotes a possible sub-path like
qi


symbol
i
Costas Busch - LSU

qj
95
We will show that if w  L ( M )
w   1 2   k
NFA M :
q0
1
k
2
qf
then
1
DFA M  :
{q 0 ,  }
state
label
2
w  L ( M )
Costas Busch - LSU
k
{ q f , }
state
label
96
More generally, we will show that if in M :
(arbitrary string)
NFA M :
q0
v  a1 a 2  a n
a1
qi
a2
qj
ql
an
qm
then
DFA M  :
a1
{q 0 ,  }
a2
{ q i , } { q j , }
Costas Busch - LSU
an
{ q l ,  } { q m , }
97
Proof by induction on | v |
Induction Basis: | v | 1
NFA M :
DFA M  :
q0
v  a1
a1
qi
a1
{q 0 ,  }
{ q i , }
is true by construction of M 
Costas Busch - LSU
98
Induction hypothesis: 1  | v | k
v  a1 a 2  a k
Suppose that the following hold
NFA M :
DFA M  :
q0
a1
a1
{q 0 ,  }
qi
a2
qj
qd
ak
a2
{ q i , }
qc
ak
{ q j , }
Costas Busch - LSU
{ q c , }
{ q d , }
99
Induction Step: | v | k  1
v  a1 a 2  a k a k 1  v a k 1
 

v
Then this is true by construction of M 
NFA M :
q0
a1
a2
qi
qj
qc
ak
qd
a k 1
qe
v
DFA M  :
a1
{q 0 ,  }
ak
a2
{ q i , }
{ q j , }
v
Costas Busch - LSU
{ q c , }
a k 1
{ q d , }
{ q e , }
100
Therefore if w  L ( M )
w   1 2   k
NFA M :
q0
1
k
2
qf
then
DFA M  :
1
{q 0 ,  }
2
w  L ( M )
Costas Busch - LSU
k
{ q f , }
101
We have shown:
L  M   L  M 
With a similar proof
we can show: L  M
Therefore: L M
  L  M 
  L M  
END OF PROOF
Costas Busch - LSU
102
```