Non-Deterministic Finite Automata
Non-Deterministic Finite Automata
Finite Automata
1
Nondeterministic Finite Automaton (NFA)
Alphabet = {a}
q1 a q2
a
q0
a
q3
2
Alphabet = {a}
Two choices q1 a q2
a
q0
a
q3
3
Alphabet = {a}
4
First Choice
a a
q1 a q2
a
q0
a
q3
5
First Choice
a a
q1 a q2
a
q0
a
q3
6
First Choice
a a
All input is consumed
q1 a q2 “accept”
a
q0
a
q3
7
Second Choice
a a
q1 a q2
a
q0
a
q3
8
Second Choice
a a
Input cannot be consumed
q1 a q2
a
q0 Automaton Halts
a
q3 “reject”
9
An NFA accepts a string:
if there is a computation of the NFA
that accepts the string
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
11
Rejection example
q1 a q2
a
q0
a
q3
12
First Choice
a
“reject”
q1 a q2
a
q0
a
q3
13
Second Choice
q1 a q2
a
q0
a
q3
14
Second Choice
q1 a q2
a
q0
a
q3 “reject”
15
Another Rejection example
a a a
q1 a q2
a
q0
a
q3
16
First Choice
a a a
q1 a q2
a
q0
a
q3
17
First Choice
a a a
Input cannot be consumed
q1 a q2 “reject”
a
q0
a
Automaton halts
q3
18
Second Choice
a a a
q1 a q2
a
q0
a
q3
19
Second Choice
a a a
Input cannot be consumed
q1 a q2
a
q0 Automaton halts
a
q3 “reject”
20
An NFA rejects a string:
if there is no computation of the NFA
that accepts the string.
OR
• The input cannot be consumed
21
a is rejected by the NFA:
“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 “reject” q3
“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 q3 “reject”
q1 a q2
a
q0
a
q3
24
Lambda Transitions
q0 a q1 q2 a q3
25
a a
q0 a q1 q2 a q3
26
a a
q0 a q1 q2 a q3
27
input tape head does not move
a a
q0 a q1 q2 a q3
28
all input is consumed
a a
“accept”
q0 a q1 q2 a q3
String aa is accepted
29
Rejection Example
a a a
q0 a q1 q2 a q3
30
a a a
q0 a q1 q2 a q3
31
(read head doesn’t move)
a a a
q0 a q1 q2 a q3
32
Input cannot be consumed
a a a
Automaton halts
“reject”
q0 a q1 q2 a q3
q0 a q1 q2 a q3
34
Another NFA Example
q0 a q1 b q2 q3
35
a b
q0 a q1 b q2 q3
36
a b
q0 a q1 b q2 q3
37
a b
“accept”
q0 a q1 b q2 q3
38
Another String
a b a b
q0 a q1 b q2 q3
39
a b a b
q0 a q1 b q2 q3
40
a b a b
q0 a q1 b q2 q3
41
a b a b
q0 a q1 b q2 q3
42
a b a b
q0 a q1 b q2 q3
43
a b a b
q0 a q1 b q2 q3
44
a b a b
“accept”
q0 a q1 b q2 q3
45
Language accepted
q0 a q1 b q2 q3
46
Exercise: What language is accepted by
the following NFA ?
0
q0 q1 0, 1 q2
1
47
Language accepted
•Simple automata:
M1 M2
q0 q0
L( M 1 ) ? L(M 2 ) ?
49
Remarks:
•The symbol never appears on the
input tape
•Simple automata:
M1 M2
q0 q0
L(M1 ) = {} L(M 2 ) ?
50
Remarks:
•The symbol never appears on the
input tape
•Simple automata:
M1 M2
q0 q0
M Q, , , q0 , F
Q: q0 , q1, q2
Set of states, i.e.
q0 : Initial state
F: Accepting states
52
Transition Function
q , x q1, q2,, qk
q1
x resulting states with
q x
q1 following one transition
x
with symbol x
qk
53
q0 , 1 q1
0
q0 q1 0, 1 q
2
1
54
(q1,0) {q0 , q2}
0
q0 q1 0, 1 q
2
1
55
(q0 , ) {q0 , q2 }
0
q0 q1 0, 1 q
2
1
56
(q2 ,1)
0
q0 q1 0, 1 q
2
1
57
*
Extended Transition Function
Same with but applied on strings
q0 , a q1
*
q4 q5
a a
q0 a q1 b q2 q3
58
q0 , aa q4 , q5
*
q4 q5
a a
q0 a q1 b q2 q3
59
q0 , ab q2, q3, q0
*
q4 q5
a a
q0 a q1 b q2 q3
60
Special case:
q q ,
*
61
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
62
The Language of an NFA M
The language accepted by M is:
63
wm LM
(q0 ,wm )
*
qi
wm
q0 w qk qk F
m
wm qj
64
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
q0 , aa ?
*
65
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
q0 , aa q4 , q5
*
F
66
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
q0 , aa q4 , q5
*
aa L(M )
F
67
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
q0 , ab ?
*
68
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
q0 , ab q2 , q3 , q0
*
F
69
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
q0 , ab q2 , q3 , q0
*
ab LM
F
70
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
* q0 , abaa ?
71
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
* q0 , abaa q4 , q5
F
72
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
* q0 , abaa q4 , q5 aaba L(M )
F
73
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
* q0 , aba ?
74
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
q4 q5
a a
q0 a q1 b q2 q3
77
NFAs accept the Regular
Languages
78
Equivalence of Machines
Definition:
if L M1 L M 2
79
Example of equivalent machines
NFA M1
LM1 {10} * 0
q0 q1
1
DFA M2 0,1
LM 2 {10} * 0
q0 q1 1 q2
1
0
80
Theorem:
Languages
Regular
accepted
Languages
by NFAs
Languages
accepted
by DFAs
Languages
accepted Regular
Languages
by NFAs
AND
Languages
accepted Regular
Languages
by NFAs
82
Proof-Step 1
Languages
accepted Regular
Languages
by NFAs
Languages
accepted Regular
Languages
by NFAs
Any NFA can be converted to an
equivalent DFA
DFA M
q0
85
* (q0 , a ) {q1, q2 }
NFA M a
q0 a q1 q2
b
DFA M
q0 a
q1,q2
86
* (q0 , b ) empty set
NFA M a
q0 a q1 q2
b
DFA M
q0 a
q1,q2
b
trap state
87
(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
88
* (q1 , b)
NFA M a * (q2 , b) {q0}
a union
q0 q1 q2
b q0
a
DFA M b
q0 a
q1,q2
b
89
NFA M a
q0 a q1 q2
b
a
DFA M b
q0 a
q1,q2
b
a, b trap state
90
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
91
General Conversion Procedure
Input: an NFA M
92
The NFA has states q0 , q1, q2 ,...
93
Conversion Procedure Steps
step
1. Initial state of NFA: q0
94
Example
NFA M a
q0 a q1 q2
b
DFA M
q0
95
step
2. For every DFA’s state {qi , q j ,..., qm }
q0 , a q1, q2
DFA M
q0 a
q1,q2
97
step
3. Repeat Step 2 for every state in DFA and
symbols in alphabet until no more states
can be added in the DFA
98
Example
NFA M a
q0 a q1 q2
b
a
DFA M b
q0 a
q1,q2
b
a, b
99
step
4. For any DFA state {qi , q j ,..., qm }
100
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
101
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
102
Exercise: Convert following NDFA to DFA
103
Exercise: Convert following NDFA to DFA
104
Single Accepting
State for NFAs
Lecture 14
Dated: 26.07.2017
105
Any NFA can be converted
to an equivalent NFA
106
Example
a NFA
a b
a Equivalent NFA
a b
b
107
In General
NFA
Equivalent NFA
Single
accepting
state
108
Extreme Case
109
Properties of
Regular Languages
110
For regular languages L1 and L2
we will prove that:
Union: L1 L2
Concatenation: L1L2
Star: Are regular
L1 *
Languages
Reversal: L1 R
Complement: L1
Intersection: L1 L2
111
We say: Regular languages are closed under
Union: L1 L2
Concatenation: L1L2
Star: L1 *
Reversal: L1 R
Complement: L1
Intersection: L1 L2
112
Regular language L1 Regular language L2
LM1 L1 LM 2 L2
NFA M1 NFA M2
113
Example
M1
n0
a
L1 {a b}
n
b
M2
L2 ba b a
114
Union
NFA for L1 L2
M1
M2
115
Example
NFA for L1 L2 {a b} {ba}
n
L1 {a b}n
a
b
L2 {ba}
b a
116
Concatenation
M1 M2
117
Example
L1 {a b}n
a
L2 {ba}
b b a
118
Star Operation
NFA for L1 *
L1 *
M1
119
Example
w w1w2 wk
NFA for L1* {a b} *
n
wi L1
L1 {a b} n
a
b
120
Reverse
R
NFA for L1
L1 M1 M1
M1
a
R
L1 {ba }
n b
122
Complement
L1 M1 L1 M1
L1 {a b}
n b a, b
M1
a a, b
L1 {a, b} * {a b}
n
b a, b
124
Intersection
L1 regular
We show L1 L2
L2 regular regular
125
DeMorgan’s Law: L1 L2 L1 L2
L1 , L2 regular
L1 , L2 regular
L1 L2 regular
L1 L2 regular
L1 L2 regular
126
Example
L1 {a b}
n regular
L1 L2 {ab}
L2 {ab, ba} regular regular
127
Another Proof for Intersection Closure
Machine M1 Machine M2
FA for L1 FA for L2
qi , p j
State in M1 State in M2
129
FA M1 FA M2
q1 a q2 p1 a p2
transition transition
FA M
q1, p1 a q2 , p2
transition
130
FA M1 FA M2
q0 p0
initial state initial state
FA M
q0 , p0
Initial state
131
FA M1 FA M2
qi pj pk
FA M
qi , p j qi , pk
accept states
n0 m0
L1 {a b} n
L2 {ab } m
M1 M2
a b
q0 b q1 p0 a p1
a, b b a
q2 p2
a, b a, b
133
Automaton for intersection
n n
L {a b} {ab } {ab}
a, b
q0 , p0 a q0 , p1 b q1, p1 a q2 , p2
b a b a
q1, p2 b q0 , p2 q2 , p1
a b
a, b
134
M simulates in parallel M1 and M 2
M 2 accepts string w
L ( M ) L ( M1 ) L ( M 2 )
135
End of Lecture
136
Exercise: Convert following NDFA to DFA
2 a
a b
1 3 3 F
c
c 4
Yes: ab abab cc ccab ccacc ccac abacab
No: b a cb cca
137