Professional Documents
Culture Documents
1
Nondeterministic Finite Accepter (NFA)
Alphabet = {a}
q1 a q2
a
q0
a
q3
2
Nondeterministic Finite Accepter (NFA)
Alphabet = {a}
Two choices q1 a q2
a
q0
a
q3
3
Nondeterministic Finite Accepter (NFA)
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
q1 a q2
a
q0
a
q3
7
First Choice
a a
All input is consumed
q1 a q2 “accept”
a
q0
a
q3
8
Second Choice
a a
q1 a q2
a
q0
a
q3
9
Second Choice
a a
q1 a q2
a
q0
a
q3
10
Second Choice
a a
q1 a q2
a
q0
a
No transition:
q3
the automaton hangs
11
Second Choice
a a
Input cannot be consumed
q1 a q2
a
q0
a
q3 “reject”
12
An NFA accepts a string:
when there is a computation of the NFA
that accepts the string
AND
all the input is consumed and the automaton
is in a final state
13
Example
“accept”
q1 a q2 q1 a q2
a a
q0
a
q0
a
q3 q3 “reject”
because this
computation
accepts aa 14
Rejection example
q1 a q2
a
q0
a
q3
15
First Choice
q1 a q2
a
q0
a
q3
16
First Choice
a
“reject”
q1 a q2
a
q0
a
q3
17
Second Choice
q1 a q2
a
q0
a
q3
18
Second Choice
q1 a q2
a
q0
a
q3
19
Second Choice
q1 a q2
a
q0
a
q3 “reject”
20
An NFA rejects a string:
when there is no computation of the NFA
that accepts the string:
OR
“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 q3
“reject”
a a a
q1 a q2
a
q0
a
q3
23
First Choice
a a a
q1 a q2
a
q0
a
q3
24
First Choice
a a a
q1 a q2
a
q0 No transition:
a
the automaton hangs
q3
25
First Choice
a a a
Input cannot be consumed
q1 a q2 “reject”
a
q0
a
q3
26
Second Choice
a a a
q1 a q2
a
q0
a
q3
27
Second Choice
a a a
q1 a q2
a
q0
a
q3
28
Second Choice
a a a
q1 a q2
a
q0
a
No transition:
q3
the automaton hangs
29
Second Choice
a a a
Input cannot be consumed
q1 a q2
a
q0
a
q3 “reject”
30
aaa is rejected by the NFA:
“reject”
q1 a q2 q1 a q2
a a
q0
a
q0
a
q3 q3 “reject”
q1 a q2
a
q0
a
q3
32
Lambda Transitions
q0 a q1 q2 a q3
33
a a
q0 a q1 q2 a q3
34
a a
q0 a q1 q2 a q3
35
(read head does not move)
a a
q0 a q1 q2 a q3
36
a a
q0 a q1 q2 a q3
37
all input is consumed
a a
“accept”
q0 a q1 q2 a q3
String aa is accepted
38
Rejection Example
a a a
q0 a q1 q2 a q3
39
a a a
q0 a q1 q2 a q3
40
(read head doesn’t move)
a a a
q0 a q1 q2 a q3
41
a a a
q0 a q1 q2 a q3
No transition:
the automaton hangs
42
Input cannot be consumed
a a a
“reject”
q0 a q1 q2 a q3
q0 a q1 q2 a q3
44
Another NFA Example
q0 a q1 b q2 q3
45
a b
q0 a q1 b q2 q3
46
a b
q0 a q1 b q2 q3
47
a b
q0 a q1 b q2 q3
48
a b
“accept”
q0 a q1 b q2 q3
49
Another String
a b a b
q0 a q1 b q2 q3
50
a b a b
q0 a q1 b q2 q3
51
a b a b
q0 a q1 b q2 q3
52
a b a b
q0 a q1 b q2 q3
53
a b a b
q0 a q1 b q2 q3
54
a b a b
q0 a q1 b q2 q3
55
a b a b
q0 a q1 b q2 q3
56
a b a b
“accept”
q0 a q1 b q2 q3
57
Language accepted
q0 a q1 b q2 q3
58
Another NFA Example
0
q0 q1 0, 1 q2
1
59
Language accepted
•Simple automata:
M1 M2
q0 q0
NFA M1 DFA M2 a
q2
q0 a q1
a
q0 a q1
L( M1 ) = {a} L( M 2 ) = {a}
62
Formal Definition of NFAs
M Q, , , q0 , F
q0 : Initial state
F : Final states
63
Transition Function
q0 , 1 q1
0
q0 q1 0, 1 q
2
1
64
(q1,0) {q0 , q2}
0
q0 q1 0, 1 q
2
1
65
(q0 , ) {q0 , q2}
0
q0 q1 0, 1 q
2
1
66
(q2 ,1)
0
q0 q1 0, 1 q
2
1
67
Extended Transition Function *
* q0 , a q1
q4 q5
a a
q0 a q1 b q2 q3
68
* q0 , aa q4 , q5
q4 q5
a a
q0 a q1 b q2 q3
69
* q0 , ab q2 , q3 , q0
q4 q5
a a
q0 a q1 b q2 q3
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
71
The Language of an NFA M
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
76
Formally
The language accepted by NFA M is:
LM w1, w2 , w3 ,...
77
w LM * (q0 , w)
qi
w
qk qk F
q0 w
w qj
78
NFAs accept the Regular
Languages
79
Equivalence of Machines
if LM1 LM 2
80
Example of equivalent machines
NFA M1
0
LM1 {10} *
q0 q1
1
DFA M2 0,1
0
LM 2 {10} *
q0 q1 1 q2
1
0
81
We will prove:
Languages
Regular
accepted
Languages
by NFAs
Languages
accepted
by DFAs
NFAs and DFAs have the
same computation power
82
Step 1
Languages
accepted Regular
Languages
by NFAs
Languages
accepted Regular
Languages
by NFAs
DFA M
q0
85
Convert NFA to DFA
a
NFA M
q a q q
0 1 2
b
DFA M
q0 a
q1, q2
86
Convert NFA to DFA
a
NFA M
q a q q
0 1 2
b
DFA M
q0 a
q1, q2
b
87
Convert NFA to DFA
a
NFA M
q a q q
0 1 2
b
a
DFA M
q0 a
q1, q2
b
88
Convert NFA to DFA
a
NFA M
q a q q
0 1 2
b
b a
DFA M
q0 a
q1, q2
b
89
Convert NFA to DFA
a
NFA M
q a q q
0 1 2
b
b a
DFA M
q0 a
q1, q2
b
a, b
90
Convert NFA to DFA
a
NFA M
q a q q
0 1 2
b
LM L(M )
a
DFA M b
q0 a
q1, q2
b
a, b
91
NFA to DFA: Remarks
We want to convert it
to an equivalent DFA M
q0 , q1, q2 ,...
93
Procedure NFA to DFA
94
Example
M a
NFA
q0 a q1 q2
b
DFA M
q0
95
Procedure NFA to DFA
2. For every DFA’s state {qi , q j ,..., qm }
DFA M
q0 a
q1, q2
q0 , a q1, q2
97
Procedure NFA to DFA
98
Example
M a
NFA
q0 a q1 q2
b
b a
DFA M
q0 a
q1, q2
b
a, b
99
Procedure NFA to DFA
3. For any DFA state {qi , q j ,..., qm }
100
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
101
Theorem
Take NFA M
LM LM
102
Proof
LM LM
103
First we show: LM LM
104
w L(M )
M: q0 w qf
w 1 2 k
1 2 k
M: q0 qf
105
We will show that if w L(M )
w 1 2 k
1 2 k
M: q0 qf
1 2 k
M:
{q0 } {q f ,}
w L(M )
106
More generally, we will show that if in M:
(arbitrary string) v a1a2 an
a1 a2 an
M: q0 qi qj ql qm
a1 a2 an
M:
{q0} {qi ,} {q j ,} {ql ,} {qm ,}
107
Proof by induction on |v|
Induction Basis: v a1
a1
M: q0 qi
a1
M:
{q0 } {qi ,}
108
Induction hypothesis: 1 | v | k
v a1a2 ak
a1 a2 ak
M: q0 qi qj qc qd
a1 a2 ak
M:
{q0} {qi ,} {q j ,} {qc ,} {qd ,}
109
Induction Step: | v | k 1
v a1a2 ak ak 1 vak 1
v
a1 a2 ak
M: q0 qi qj qc qd
v
a1 a2 ak
M:
{q0} {qi ,} {q j ,} {qc ,} {qd ,}
v 110
Induction Step: | v | k 1
v a1a2 ak ak 1 vak 1
v
a1 a2 ak ak 1
M: q0 qi qj qc qd qe
v
a1 a2 ak ak 1
M:
{q0} {qi ,} {q j ,} {qc ,} {qd ,} {qe ,}
v 111
Therefore if w L(M )
w 1 2 k
1 2 k
M: q0 qf
1 2 k
M:
{q0 } {q f ,}
w L(M )
112
We have shown: LM LM
(proof is similar)
113