Week3
Week3
Automata
(Part 2)
1
Definition
A Nondeterministic Finite Automaton (NFA)
implying that the machine can exist in more than one
state at the same time
transitions could be non-deterministic
2
Deterministic and nondeterministic computation with an
accepting branch
3
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)
0
q0 q1 0, 1 q2
1
5
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 Φ Φ
6
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
DFA? *
q2 {q2} {q2}
7
Example #3 L = { w | w ends in 01}
• Q = {q0,q1,q2}
0,1 • = {0,1}
• start state = q0
0 1
q0 q1 q2 • F = {q2}
• Transition table
symbols
0 1
q0 {q0,q1} {q0}
states
q1 {q2}
Φ
q2
* Φ Φ
Example #4:
L = {w|w is a binary string s.t. the 2nd symbol is a 1}
9
What is an “error state”?
A DFA for recognizing the key word “while”
w h i l e
q0 q1 q2 q3 q4 q5
qerr
Any symbol
1 2 3 r b
1 {2,4} {5}
4 5 6 2 {4,6} {1,3,5}
3 {2,6} {5}
7 8 9
states
4 {2,8} {1,5,7}
5 {2,4,6,8} {1,3,7,9}
6 {2,8} {3,5,9}
r b b 7 {4,8} {5}
1 2 1 5 8 {4,6} {5,7,9}
4 3 1 *9 {6,8} {5}
5 3
7 7
9 Accept, since final state reached
Extension of δ to NFA Paths
Extended Transition Function *
Basis: * (q, ) {q}
Induction:
• Let * (q0, w) {p1, p2 , pk}
• * (pi, a) Si for i=1,2...,k
* q0 , a q1
q4 q5
a a
q0 a q1 b q2 q3
14
∗
𝛿 𝑞0 , 𝑎𝑎 = {𝑞4 , 𝑞5 }
q4 q5
a a
q0 a q1 b q2 q3
15
* q0 , ab q2 , q3 , q0
q4 q5
a a
q0 a q1 b q2 q3
16
Language of an NFA
An NFA accepts w if there exists at least
one path from the start state to an
accepting (or final) state that is labeled by
w
L(N) = { w | δ*(q0,w) ∩ F ≠ Φ }
The Language of an NFA M
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
22
Differences: DFA vs. NFA
DFA NFA
1. All transitions are 1. Some transitions could be
deterministic non-deterministic
• Each transition leads to exactly • A transition could lead to a
one state subset of states
2. For each state, transition on 2. Not all symbol transitions
all possible symbols (alphabet) need to be defined explicitly
But, DFAs and NFAs are equivalent in
should be defined (if undefined will go to an
error state – this is just a
3. Accepts input if the last state
their power to capture languages !!
visited is in F design convenience, not to be
confused with “non-
4. Sometimes harder to determinism”)
construct because of the
number of states 3. Accepts input if one of the
5. Practical implementation is last states is in F
feasible 4. Generally easier than a DFA
to construct
5. Practical implementations
limited but emerging (e.g.,
Micron automata processor)
Technologies for NFAs
DFA M2 0,1
LM 2 {10} * 0
q0 q1 1 q2
1
0
25
Equivalence of Machines
if L M1 L M 2
26
Equivalence of DFA & NFA
Theorem (2 Theorems):
Should be A language L is accepted by a DFA if and only if it is
true for accepted by an NFA.
any L
Proof:
1. If part: (1st Theorem)
A language L is accepted by a DFA if it is accepted by an NFA
Prove by showing every NFA can be converted to an equivalent
DFA (in the next few slides…)
Construction:
Notice that the input alphabets of the two automata are the same,
and the start state of D is the set containing only the start state of N.
1 0
NFA: DFA: 0 1
{q0} {q0,q1} {q0,q2}
0,1 0
1
0 1
q0 q1 q2
δD 0 1 δD 0 1
Ø Ø Ø {q0} {q0,q1} {q0}
δN 0 1
{q0} {q0,q1} {q0} {q0,q1} {q0,q1} {q0,q2}
q0 {q0,q1} {q0}
{q1} Ø {q2} *{q0,q2} {q0,q1} {q0}
q1 Ø {q2}
* {q2} Ø Ø
*q2 Ø Ø
{q0,q1} {q0,q1} {q0,q2}
0. Enumerate all possible subsets
* {q0,q2} {q0,q1} {q0}
1. Determine transitions
* {q1,q2} Ø {q2}
2. Retain only those states
* {q0,q1,q2} {q0,q1} {q0,q2} reachable from {q0}
NFA to DFA: Repeating the example using
LAZY CREATION
L = {w | w ends in 01}
1 0
NFA: DFA: 0 1
0,1 {q0} {q0,q1} {q0,q2}
0
0 1 1
q0 q1 q2
δN 0 1 δD 0 1
{q0} {q0,q1} {q0}
q0 {q0,q1} {q0}
{q0,q1} {q0,q1} {q0,q2}
q1 Ø {q2}
*q2 Ø Ø *{q0,q2} {q0,q1} {q0}
Main Idea:
Introduce states as you go
(on a need basis)
Example #2 Subset Construction
3 δ a b
b a
a 1 {1,2,3,4,5} {4,5}
a
a 2 b 2 {3} {5}
a,b
1 5 {2}
3 ∅
a,b a 4 {5} {4}
4
b
*5 ∅ ∅
δ a b δ a b
1 {1,2,3,4,5} {4,5} {1} {1,2,3,4,5} {4,5}
{1,2,3,4,5} {1,2,3,4,5} {2,4,5}
2 {3} {5}
{4,5} {5} {4}
3 ∅ {2} {2,4,5} {3,5} {4,5}
4 {5} {4} {5} ∅ ∅
{4} {5} {4}
*5 ∅ ∅
{3,5} ∅ {2}
∅ ∅ ∅
{2} {3} {5}
{3} ∅ {2}
33
a
δ a b
b a
12345 245
{1} {1,2,3,4,5} {4,5}
35
a
*{1,2,3,4,5} {1,2,3,4,5} {2,4,5} a,b a
b
a b
*{4,5} {5} {4} ∅
1 3
*{2,4,5} {3,5} {4,5} a,b b
b a
*{5} ∅ ∅
a 2
{4} {5} {4}
45 5 b
*{3,5} ∅ {2}
b a
4
∅ ∅ ∅
{2} {3} {5}
b
{3} ∅ {2}
34
A bad case
#states(DFA) >> #states(NFA)
L = {w | w is a binary string s.t., the kth symbol
from its end is a 1}
?
A few subtle properties of DFAs and NFAs
The machine never really terminates.
It is always waiting for the next input symbol or making
transitions.
39
To simulate any transition:
Example of an -NFA
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.
L = {w | w is empty, or if non-empty will end in 01}
0,1
0 1
q0 q1 q2
Simulate for w=101:
start q’0
q0’
q0’ q0
δE 0 1 1
1
ECLOSE(q’0)
*q’0 Ø Ø {q’0,q0} q0
Ø
q0 {q0,q1} {q0} {q0} ECLOSE(q0) x 0
q1 Ø {q2} {q1} q1 q0
1
* q2 Ø Ø {q2}
q2 q0
x
40
To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.
Example of another -NFA
0,1 Simulate for w=101:
q0
0
q1
1
q2 ?
1
start q’0 q3
δE 0 1
*q’0 Ø Ø {q’0,q0,q3}
q0 {q0,q1} {q0} {q0,q3}
q1 Ø {q2} {q1}
* q2 Ø Ø {q2}
q3 Ø {q2} {q3}
41
Equivalency of DFA, NFA, -NFA
Theorem: A language L is accepted by some
-NFA if and only if L is accepted by some DFA
Implication:
0,1
0 1
q0 q1 q2
start q’0
δE 0 1
δD 0 1
0,1 0
0
0 1 {q0,q1}
q0 q1 q2 0 1 {q0,q2}
0
1
start start {q’0, q0} 1
q’0 q0 1
ECLOSE union
δE 0 1
δD 0 1
47