View

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