You are on page 1of 42

Chapter Four

1
 A nondeterministic finite automaton has the
ability to be in several states at once.
 Transitions from a state on an input symbol
can be to any set of states.

2
 Start in one start state.
 Accept if any sequence of choices leads to a
final state.
 Intuitively: the NFA always “guesses right.”

3
 States = squares.
 Inputs = r (move to an adjacent red square)
and b (move to an adjacent black square).
 Start state, final state are in opposite corners.

4
=black r b
1 2 3
1 2,4 5
=red
2 4,6 1,3,5
4 5 6 3 2,6 5
4 2,8 1,5,7
7 8 9 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
5
 An NFA is a five-tuple:

M = (Q, Σ, δ, q0, F)

Q A finite set of states


Σ A finite input alphabet
q0 The initial/starting state, q0 is in Q
F A set of final/accepting states, which is a subset of Q
δ A transition function, which is a total function from Q
x Σ to 2Q

δ: (Q x Σ) –> 2Q -2Q is the power set of Q, the set of all subsets


of Q

6
Example
1. Let a non-deterministic finite automaton be
Q = {a, b, c}
∑ = {0, 1}
q0 = {a}
F = {c}
The transition function t is as shown below −

7
Present State Next State for input 0 Next State for input 1
A A,B B
B C A,C
C B,C C

Its graphical representation would be as


follows 

8
2.:
State Input Next state(s)
a
S a {A}
a
S A S b {B}
A a {A, C}
b a A b {}
B a {}
B C
S B b {B, C}
b C a {}
b
C b {}

The strings aa, bbb are both accepted by the NFSA.

9
 Three major differences

1. The range of δ is in the power set 2Q


2. ε transitions are possible in NFA. NFA can make
a transition without consuming an input symbol.
3. In an NFA, the set δ(qi,a) may be empty; there is
no transition defined for this specific situation.

10
Example #1: some 0’s followed by
some 1’s
0 1 0/1

0 1
Q = {q0, q1, q2} q0 q1 q2

Σ = {0, 1}
Start state is q0
F = {q2}

δ: 0 1
q0 {q0, q1} {}

{} {q1, q2}
q1
{q2} {q2}
q2
11
 Example #2: pair of 0’s or pair of 1’s

0/1 0/1
Q = {q0, q1, q2 , q3 , q4}
Σ = {0, 1} 0
q0 q3 0 q4
Start state is q0
F = {q2, q4}
1 0/1
1
δ: 0 1
q0 q1 q2
{q0, q3} {q0, q1}

q1 {} {q2}

q2 {q2} {q2}

q3 {q4} {}

q4 {q4} {q4}
12
 A string w is accepted by an NFA if δ(q0, w)
contains at least one final state.
 The language of the NFA is the set of strings
it accepts.

13
 A DFA can be turned into an NFA that
accepts the same language.
 If δD(q, a) = p, let the NFA have δN(q, a) =
{p}.
 Then the NFA is always in a set containing
exactly one state – the state the DFA is in
after reading the same input.

14
 Surprisingly, for any NFA there is a DFA
that accepts the same language.
 Proof is the subset construction.
 The number of states of the DFA can be
exponential in the number of states of the
NFA.
 Thus, NFA’s accept exactly the regular
languages.

15
 Given an NFA with states Q, inputs Σ,
transition function δN, start state q0, and
final states F, construct equivalent DFA
with:
 States 2Q (Set of subsets of Q).
 Inputs Σ.
 Start state {q0}.
 Final states = all those with a member of F.

16
 The DFA states have names that are sets
of NFA states.
 But as a DFA state, an expression like {p,q}
must be read as a single symbol, not as a
set.

17
 The transition function δD is defined by:
δD({q1,…,qk}, a) is the union over all i = 1,…,k of
δN(qi, a).

 Example: We’ll construct the DFA equivalent


of our “chessboard” NFA.

18
r b r b
1 2,4 5 {1} {2,4} {5}
2 4,6 1,3,5 {2,4}
3 2,6 5 {5}
4 2,8 1,5,7
5 2,4,6,8 1,3,7,9
6 2,8 3,5,9
7 4,8 5
*
8 4,6 5,7,9 Alert: What we’re doing here is
9 6,8 5 the lazy form of DFA construction,
where we only construct a state
if we are forced to.
19
r b r b
1 2,4 5 {1} {2,4} {5}
2 4,6 1,3,5 {2,4} {2,4,6,8} {1,3,5,7}
3 2,6 5 {5}
4 2,8 1,5,7 {2,4,6,8}
5 2,4,6,8 1,3,7,9 {1,3,5,7}
6 2,8 3,5,9
7 4,8 5
8 4,6 5,7,9
* 9 6,8 5

20
r b r b
1 2,4 5 {1} {2,4} {5}
2 4,6 1,3,5 {2,4} {2,4,6,8} {1,3,5,7}
3 2,6 5 {5} {2,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,6,8} {1,3,5,7,9}
6 2,8 3,5,9 * {1,3,7,9} {2,4,6,8} {5}
7 4,8 5 * {1,3,5,7,9} {2,4,6,8} {1,3,5,7,9}
8 4,6 5,7,9
* 9 6,8 5

25
M=({A,B},{0,1}, δ,A,{A}) where
0 1 δ is given by:

* A A B
B B A,B
0 1
* A A B
B B {A,B}
{A,B} {A,B} {A,B}
*

26
M=({q0,q1,q2},{a,b}, δ,q0,{q2}) where δ is given by:

a b
q0 q0,q1 q2
q1 q0 q1
a b
* q2  q0,q1
q0 {q0,q1} q2
q2  {q0,q1}
{q0,q1} {q0,q1} {q1,q2}

* {q1,q2} q0 {q0,q1}
  
27
M=({P,Q,R,S},{0,1}, δ,P,{S}) where δ is given by:
0 1
0 1
P {P,Q} P
P P,Q P
{P,Q} {P,Q,R} {P,R}
Q R R
{P,R} {P,Q,S} P
R S 
{P,Q,R} {P,Q,R,S} {P,R}
* S S S
{P,Q,S} {P,Q,R,S} {P,R,S}
*
{P,R,S} {P,Q,S} {P,S}
*
{P,S} {P,Q,S} {P,S}
*
{P,Q,R,S} {P,Q,R,S} {P,R,S}
* 28
M=({q0,q1,q2,q3},{a,b}, δ,q0,{q2}) where δ is given
by:
a b a b
q0 q0,q1 q0 q0 {q0,q1} q0
q1 q2 q1 {q0,q1} {q0,q1,q2} {q0,q1}
q2 q3 q3
* {q0,q1,q2} {q0,q1,q2,q3} {q0,q1,q3}
* q3  q2
* {q0,q1,q3} {q0,q1,q2} {q0,q1,q2}

{q0,q1,q2,q3} {q0,q1,q2,q3} {q0,q1,q2,q3}


*

29
 We can allow state-to-state transitions on ε
input.
 These transitions are done spontaneously,
without looking at the input string.
 A convenience at times, but still only regular
languages are accepted.

30
ε 0 1 ε
A {E} {B} ∅
1 1 B ∅ {C} {D}
1 B C D
C ∅ {D} ∅
A ε ε 0 * D ∅ ∅ ∅
E {F} ∅ {B, C}
0 E F
0 F {D} ∅ ∅

31
 CL(q) = set of states you can reach from
state q following only arcs labeled ε.
 Example: CL(A) = {A}; ε
CL(E) = {B, C, D, E}. B 1 C 1 D
1
A ε ε 0
0 E F
0

 Closure of a set of states = union of the


closure of each state.
32
˄
 Basis: δ(q, ε) = CL(q).
 Induction: ˄ (q, xa) is computed as follows:
δ
˄ (q, x) = S.
1. Start with δ
2. Take the union of CL(δ(p, a)) for all p in S.
 Intuition: ˄ (q, w) is the set of states you can
δ
reach from q following a path labeled w.

33
ε
1 B 1 C 1 D
˄
 δ (A, ε) = CL(A) = {A}. A ε ε 0
˄
δ (A, 0) = CL({E}) = {B, C, D, E}. 0 E
0
F
 ˄ (A, 01) = CL({C, D}) = {C, D}.
δ
 Language of an ε-NFA is the set of strings
w such that (q0, w) contains a final state.

34
 Every NFA is an ε-NFA.
 It just has no transitions on ε.
 Converse requires us to take an ε-NFA and
construct an NFA that accepts the same
language.
 We do so by combining ε–transitions with the
next transition on a real input.

35
 Start with an ε-NFA with states Q, inputs Σ,
start state q0, final states F, and transition
function δE.
 Construct an “ordinary” NFA with states Q,
inputs Σ, start state q0, final states F’, and
transition function δN.

36
 Compute δN(q, a) as follows:
1. Let S = CL(q).
2. δN(q, a) is the union over all p in S of δE(p, a).
 F’ = the set of states q such that CL(q)
contains a state of F.
 Intuition: δN incorporates ε–transitions
before using a but not after.

37
Interesting
closures:
CL(B) = {B,D}; ε
CL(E) = {B,C,D,E}
1 B 1 C 1 D
0 1
0 1 ε A {E} {B}
A ε ε 0
A {E} {B} ∅ 0 E F
*B ∅ {C} 0
B ∅ {C} {D} C ∅ {D}
C ∅ {D} ∅
*D ∅ ∅
* D ∅ ∅ ∅
E {F} ∅ {B, C} *E {F} {C, D}
F {D} ∅
F {D} ∅ ∅
NFA without ε-Transition
Since closure of
ε-NFA E includes B and
Since closures of C; which have
B and E include transitions on 1
final state D. to C and D.
38
1. Convert the following NFA with -transitions
into NFA without -transitions.

0 1 2

ε ε
q0 q1 q2

Answer:
0,1,2
0 1 2

0,1 1,2
q0 q1 q2

39
2. Convert the following NFA with -transitions into NFA without -
transitions.

0
ε 1
Answer: q0 q1 q2
0
0 1
1
0 q3
0,1
0 1
q0 q1 q2
0
0 1
0
q3
0,1
40
Convert the following NFA with -transitions into NFAs without -
transitions.
a)
1

0 ε
b)-NFA q0 following transition
with the q1 table q2

0 1 ε
A {A} ∅ {B}
B ∅ {C} {D}
C {D} ∅ ∅
* D ∅ {C} {B}
41
 DFA’s, NFA’s, and ε–NFA’s all accept exactly
the same set of languages: the regular
languages.
 The NFA types are easier to design and may
have exponentially fewer states than a DFA.
 But only a DFA can be implemented!

42

You might also like