M 416: Introduction to Theory of
Computation
Helwan University
Dr. Rashad A Ragab
Lecture 4
1 Lecture 4
Nondeterministic Finite Automaton
(NFA)
Generalization of DFA. Allows:
– 0 or more next states for the same (q, σ).
Guessing
– Transitions labeled by the empty string.
Changing state without reading input
Motivation: Flexibility.
– Easier to prove many closure properties.
Lecture 3 2
1
How does an NFA work?
w is accepted by an NFA provided there is a
sequence of guesses that leads to a final
state.
Language accepted by NFA is the set of all
strings accepted by it.
Lecture 3 3
Example: {w in {0,1}* | the second last
symbol of w is a 1}
Lecture 3 4
2
NFA A = {w in {0,1}* | the second last
symbol of w is a 1}
110
Lecture 3 5
NFA A = {w in {0,1}* | the second last
symbol of w is a 1}
110
Lecture 3 6
3
NFA A = {w in {0,1}* | the second last
symbol of w is a 1}
110
Lecture 3 7
NFA A = {w in {0,1}* | the second last
symbol of w is a 1}
110
Lecture 3 8
4
Formal definition of NFA
Notation: Σe = Σ U {e}.
NFA M = (Q, Σ, ∆, s, F) where:
– Q - finite set of states
– Σ - input alphabet
– s - initial state
– F ⊆ Q - set of final states
– ∆ is a subset of Q X Σe X Q.
If (p, u, q) in ∆, then NFA in state p can read u and
go to q.
Lecture 3 9
Formal definition of NFA acceptance
Define ∆*(q, w) as a set of states: p ε ∆*(q, w) if
there is a directed path from q to p labeled w
– Example: consider NFA of Lecture 3
∆*(q0, 1) = ?
– Ans: {q0, q1}
∆*(q0, 11) = ?
– Ans: {q0, q1, q2}
10 Lecture 4
5
NFA acceptance (contd.)
w is accepted by NFA M iff ∆*(q0, w) ∩ F is
nonempty.
L(M) = {w in Σ* | w is accepted by M}.
11 Lecture 4
NFA vs. DFA
Is NFA more powerful than DFA?
– Ans: No.
Theorem:
– For every NFA M there is an equivalent DFA M'
Proof Idea:
– NFA is in a set of states at any point during reading a string.
– DFA will use a lot of states to keep track of this.
Important Assumption:
– No transition labeled by epsilon.
(Will get rid of this assumption later.)
12 Lecture 4
6
Equivalent DFA construction.
NFA M = (Q, Σ, ∆, s, F)
DFA M' = (Q', Σ, δ, s', F') where:
– Q' = 2Q
– s' = {s}
– F' = {P | P ∩ F is nonempty}
– δ({p1, p2, pm}, σ) = ∆*(p1, σ) ∪ ∆*(p2, σ) ∪ ... ∪ ∆*(pm, σ)
i.e. find all the states that can be reached on σ from all
the NFA states in a DFA state.
13 Lecture 4
Example: Equivalent DFA construction
NFA
14 Lecture 4
7
Equivalent DFA construction (contd.)
15 Lecture 4
How to handle epsilon transitions?
Define e-closure of state q as ∆*(q, ε).
– notation: e-closure(q).
Example:
16 Lecture 4
8
Handling epsilon transitions (contd.)
Extend e-closure to sets of states by:
– e-closure({s1, ... , sm}) = e-closure(s1) ∪ ... ∪ e-closure(sm)
Now let
s' = e-closure({s}).
and,
δ({p1,..., pm}, σ) = e-closure(∆*(p1, σ)) ∪ ... ∪ e-closure(∆*(pm, σ))
to complete construction of DFA.
17 Lecture 4
Example: Handling epsilon transitions.
18 Lecture 4
9
DFA = ?
19 Lecture 4
Language Operations
1. Concatenation. Notation: L•L' or just LL'
– L • L' = {uv | u in L, v in L'}.
2. Kleene Star. Notation: L*
– L* = { w in Σ* | w = w1...wk for some k >= 0 and each wi in L}.
Examples: if L = {a(2n+1) | n >= 0}. L' = {b(2n) | n > = 0}.
LL' = ?
– Ans: LL' = {a(2n+1) b(2m) | n, m > = 0}
L* = ?
– Ans: {an | n >= 0}
U, ., * are called regular operations.
20 Lecture 4
10
Closure properties of regular
languages.
Previously we saw closure under ∪ and ∩.
New: Regular languages are closed under
– Concatenation
– Kleene star
– Complement.
21 Lecture 4
Examples
L = {w in {a,b}* | w has even a’s }
22 Lecture 4
11
Examples
L' = {w in {a,b}* | w has at least one b}
23 Lecture 4
Construction for L••L'
L’’ = (K,Σ,∆,s,F)
K = K1 ∪ K2
s = s1
F = F2
∆ = ∆1 ∪ ∆2 ∪ F1 X {e} X
24 {s2}
Lecture 4
12
L* and L'*
L*
M = (K, Σ, ∆, s, F)
K = {s} ∪ K1
F = {s} ∪ F1
∆ = ∆1 ∪ F1 X {e} X {s1} ∪ {(s, e, s1)}
Given M1 = (K1, Σ, ∆1, s1, F1)
L’*
25 Lecture 4
Complement of L and L'
Complement of L
Complement of L’
26 Lecture 4
13
General Construction for
Complement
DFA M = (K, Σ, δ, s, F)
K = K1
s = s1
F = K - F1
δ = δ1
L(M) = Complement of L(M1)
DFA M1 = (K1, Σ, δ1, s1, F1)
Exercise: Will this construction work for NFAs?
Explain your answer.
27 Lecture 4
14