Nondeterministic Finite
Automata
(Part 1)
1
Nondeterministic Finite Automata (NFA)
Alphabet = {a}
q1 a q2
a
q0
a
q3
2
Nondeterministic Finite Automata (NFA)
Alphabet = {a}
Two choices q1 a q2
a
q0
a
q3
3
Nondeterministic Finite Automata (NFA)
Alphabet = {a}
Two choices a
q1 q2 No transition
a
q0
a
q3 No transition
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 dies
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
• All the input is consumed, and the
automaton is in a final state
13
Example
aa is accepted by the NFA:
“accept”
q1 a q2 q1 a q2
a a
q0 q0
a 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:
• All the input is consumed and the
automaton is in a non-final state
OR
• The input cannot be consumed
21
Example
a is rejected by the NFA:
“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 “reject” q3
All possible computations lead to rejection
22
Rejection example
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
a No transition:
the automaton dies
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 dies
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 q0
a a
q3 q3 “reject”
All possible computations lead to rejection
31
Language accepted: ?L {aa}
q1 a q2
a
q0
a
q3
32
-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 dies
42
Input cannot be consumed
a a a
“reject”
q0 a q1 q2 a q3
String aaa is rejected
43
Language accepted: L {aa
?}
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
L ab, abab, ababab, ...
?
ab
q0 a q1 b q2 q3
58
Another NFA Example
0
q0 q1 0, 1 q2
1
59
Language accepted
L(M ) = {λ, 10
?
, 1010, 101010, ...}
= {10} *
0
q0 q1 0, 1 q2
1 (redundant
state)
60
Remarks:
•Simple automata:
= {0,1}
M1 M2
q0 q0
L(M1 ) = {}? L(M 2 ) = {?λ}
What was the DFA?
61
Formal Definition of NFAs
An NFA is also defined by the 5-tuple:
M Q, , , q0 , F
Q: Set of states, i.e. q0 , q1, q2
: Input aplhabet, i.e. a, b
: Transition function, which is a mapping
between Q x ∑ -> subset of Q
q0 : Initial state
F : Final states
Example #1 (Formal Definition Example)
L(M ) = {λ, 10, 1010, 101010, ...}
= {10} *
0
q0 q1 0, 1 q2
1
63
Example #1
Transition Function
q0 , 1 q1
0
q0 q1 0, 1 q
2
1
64
Example #1
(q1,0) {q0 , q2}
0
q0 q1 0, 1 q
2
1
65
Example #1
(q2 ,1)
0
q0 q1 0, 1 q
2
1
66
Example #1
• Q = {q0,q1,q2}
L(M ) = {λ, 10, 1010, 101010, ...} • = {0,1}
= {10} * • start state = q0
0 • F = {q0}
q0 q1 0, 1 q2 • Transition table
1 symbols
0 1
q0
* Φ {q1}
states
q1 {q0,q2} {q2}
q2 Φ Φ
67
Example #2: NFA for strings containing 01
• Q = {q0,q1,q2}
0,1 0,1 • = {0,1}
• start state = q0
0 1 • F = {q2}
q0 q1 q2
• Transition table
symbols
0 1
q0 {q0,q1} {q0}
states
What was the q1
Φ {q2}
DFA? *
q2 {q2} {q2}
68