Title: 2'4 Nondeterministic Finite State Automata
12.4 Nondeterministic Finite State Automata
Definition 6 A nondeterministic finite state
automaton(NFA) M (?, Q, ?, q 0, F), where
? is a finite set alphabet,
Q is a finite set of states,
? Q ? ??? ? 2 Q is a transition function,
where 2 Q is a power set of Q,
q 0 ? Q is the initial state,
F ? Q is the set of final states.
2Example 1 A nondeterministic finite state
automaton(NFA) M (?, Q, ?, q 0, F), where
Q q 0, q 1, q 2, q 3,
q 0 ? Q is the initial state,
? 0, 1,
F q 3,
? Q? ??? ? 2 Q is defined as follows.
3The transition ?(q 0, 0) q 0, q 1 says that
at the state q 0 after reading the symbol 0 the
machine can enter either the state q 0 or the
state q 1.
Each move of the machine may have more than one
state to select. Now, we get a problem that how
to determine wether an input string is accepted
by the machine or not.
Consider the example 1 as the machine reading the
input 01010. The NFA M may have the following
possible moves.
4Case 1 q 0 01010 -- 0 q 1 1010 -- 0 1 q 2 010
-- 0 10 q 3 10 --?
Case 2 q 0 01010 -- 0 q 0 1010 -- 0 1 q 0 010
-- 0 10 q 0 10 -- 0 10 1 q 0 0 -- 0 10 1 0 q 0
Case 3 q 0 01010 -- 0 q 0 1010 -- 0 1 q 0 010
-- 0 10 q 0 10 -- 0 10 1 q 0 0 -- 0 10 1 0 q 1
Case 4 q 0 01010 -- 0 q 0 1010 -- 0 1 q 0 010
-- 0 10 q 1 10 -- 0 10 1 q 2 0 -- 0 10 1 0 q 3
5There are 4 different ways for M to read the
input 01010. Although 3 ways do not lead to a
final state, but there is one way, i.e., in case
4, that the machine enters a final state q 3.
Since there is a way for M to enter a final state
q 3 after reading the input 01010, we say that
the machine M accepts 01010.
Definition 7 A string ? is accepted by an NFA M
(?, Q, ?, q 0, F), if there is some way that q
0 ? -- ? q f , where q f ? F. The language
accepted by the machine M is L(M) ? ? ? if
there is some way that q 0 ? -- ?q f , where q
f ? F.
6The language L accepted by the following NFA M is
the set of strings ending with 010.
Compare the design of the DFA accepting language
L in section 2.3 with the design of the above
NFA. It is quite obvious that the design of an
NFA is much easier than that of a DFA, since we
take less care of interaction between states.
Althogh the design of an NFA is much easier than
that of a DFA, later we will show that NFAs and
DFAs are of same expressive power, i.e., if L
L(M) for some NFA M, then there is a DFA M such
that L L(M), and vise versa.
Therefore, we may first design an NFA, then
convert to a DFA.
7Example 2 Design an NFA M to accept strings of
0s and 1s such that the 3rd symbol from the
right end is 0.
Solution
An equivalent DFA is as follows.
8An NFA with ?-moves is much easier to design and
to understand, and more flexible and important to
use.
If we allow an NFA M to have ?-moves, then we
allow M to change states without reading any
input symbol.
Because of ?-moves, it is quite easy to group a
substructure to form a module. And the
connection between modules is much easier to
handle.
9Example 3 Design an NFA M to accept strings of
0s and 1s such that the strings either contain
a substring 101 or end with the symbols 00.
10The transition function ? is as follows.
11For the input 10100, M has the following ways to
accept it.
Case 1 q 0 10100 -- q 110100 --1q 20100 --
10q 3100 -- 101q 400 -- 1010q 40 -- 10100q 4
Case 2 q 0 10100 -- q 510100 --1q 50100 --
10q 5100 -- 101q 500 -- 1010q 60 -- 10100q 7
12Example 4 Design an NFA M to accept strings
over 0, 1 so that the evaluation of the string
? according to the following operation is same as
the evaluation of ? R.
Solution
Let M ( ?0, 1, Q, ?, q 0, F ), where
each state q ? Q and q ? q 0, q is of the form
f, b, z, f, b, z ?0, 1, ?,
F z, ?, z z 0 or 1,
if g, b, z ? ?(f, c, z, a), then g fa and
c ba, and
?(q 0, ?)?, a, a a 0 or 1.
13Each state q ? Q and q ? q 0, q is of the form
f, b, z, where
f is the evaluation of the substring from the
initial state up to current state,
z is a fixed value used to indicate the
evaluation of the reversal of a string ,
and b is the value of the coming substring
evaluated in reverse direction.
The idea is as follows.
(1) If g, b, z ? ?(f, c, z, a), then g fa,
c ba.
In another word, g fa is evaluated forward, and
c ba is evaluate backward.
(2) z, ?, z is a final state which can not
proceed any more. Since to evaluate forward equal
to the backward evaluation z, it should be a
final state.
14(3) The start state should be of the form ?, z,
z. Although we do not know what will be the
input ?, we can assume the evaluation of the
string ? R is z. And it is possible to trace
back.
(4) ?(1, 0, 0, 1) 0, 0, 0, 0, 1, 0,
since
considering the first component 0 of 0, 0, 0
and 0, 1, 0, the forward evaluation 11 0, and
considering the second components of 0, 0, 0
and 0, 1, 0, the backward evaluation 10 R 01
0, and 11R 11 0.
(5) ?(?, 0, 0, 0) 0, ?, 0, 0, 0, 0,
where 0, ?, 0 is a final state.
(6) An NFA M is listed below.
15(No Transcript)
16The expressive power of NFAs seems more powerful
than that of DFAs. It is easier to design an
NFA than to design a DFA to accept the same
language.
Now we have a problem that whether or not NFAs
are really more powerful than DFAs. Is there
any language L accepted by an NFA but not DFAs?
By the following theorem, we shall see that NFAs
are equivalent to DFAs in the sense that if
there is a language L accepted by an NFA, then
there is a DFA that accepts L, and vise versa.
We are going to use constructive method to prove
the following theorem , and that is the method we
use to convert an NFA to an equivalent DFA.
17First, we need to talk about ?-closure of a set
of states.
Definition 8 An ?-closure of a set P of states
is the set ? -closure( P ) defined recursively by
(1) P ? ?-closure( P ).
(2) If q ? ?-closure( P ), then ?( q, ? ) ?
?-closure( P ).
18Example 5 Considering the following transition
diagram, find ?-closure( q 0, q 2 ).
Solution
?( q 0, ? ) q 1, q 5, ?( q 2, ? ) q 3, q 7
?( q 1, ? ) ? ?( q 3, ? ) ?( q 7, ? ), ?( p
5, ? ) q 6
Therefore, ?-closure( q 0, q 2 ) q 0, q 2, q
1, q 5 , q 6 , q 3, q 7
19Theorem 1 L L( M 1 ) for some NFA M 1 iff L
L( M 2 ) for some DFA M 2.
Proof
(1) Suppose that L L( M 2 ) for some DFA M 2 .
Since a DFA M 2 can be treated as an NFA M 1,
where M 1 M 2, we have that L L( M 1 ) for
some NFA M 1.
(2) Suppose that L L( M 1 ) for some NFA M 1
( ?, Q, ?, q 0, F).
Construct a DFA M 2 ( ?, Q, ?, q 0 , F ),
where
F P ?Q P ? F ?? ,
20?(P, a) ? q ?P ?-closure(?(q, a)),
Then for any input ? ? ?, ?(q 0 , ?) the set
of all possible states that can be reached after
reading the input ? by machine M 1 ?(q 0 ,
?) the state for the machine M 2 to enter after
reading the input ?.
We can prove the above statement by induction.
First, if ? ?, we have that ?(q 0 , ?) ?(q 0
, ?) ?-closure( q 0 ) ?(q 0 , ?) q 0
.
Second, if ? n and ?(q 0 , ?) ?(q 0 ,
?) P,
then ?(q 0 , ?a) ? q ? P ?-closure(?(q, a))
?(P, a) ?(q 0 , ?a) for a ? ?.
And it is obvious that L L(M 1) L(M 2).
21Example 6 Design a DFA M 2 ( ?, Q, ?, q 0 ,
F ) to accept the set L of strings of 0s and
1s such that the strings end with the symbols
010.
Solution
The following is the design of an NFA M 1 ( ?,
Q, ?, q 0, F) accepting L.
We now convert the NFA M 1 to an equivalent DFA M
2 . There is no ?-move in the machine.
Therefore, we do not need to worry about
?-closure of a set of states.
22First, we have the state q 0 q 0 and the
transitions ?(q 0 , 0) q 0, q 1) and ?(q
0 , 1) q 0).
23Second, consider the transitions ?(q 0, q 1 ,
0) q 0, q 1) and ?(q 0, q 1 , 1) q 0 ,
q 2).
24Third, consider the transitions ?(q 0, q 2 ,
0) q 0, q 1 , q 3) and ?(q 0, q 2 , 1)
q 0).
25Fourth, consider the transitions ?(q 0, q 1 , q
3 , 0) q 0, q 1) and ?(q 0, q 1 , q 3 ,
1) q 0 , q 2), done!
26Example 7 Design a DFA M 2 ( ?, Q, ?, q 0 ,
F ) to accept the set L of strings of 0s and
1s such that the strings either contain a
substring 101 or end with the symbols 00.
Solution
The following is the design of an NFA M 1 ( ?,
Q, ?, q 0, F) accepting L.
We now convert the NFA M 1 to an equivalent DFA M
2 .
27First, according to a part of transition diagram
as follows we can get the state q 0 q 0, q
1, q 5.
28Second, ? (q 0 , 0) q 1, q 5 , q 6 and ?
(q 0 , 1) q 1, q 2, q 5.
29Third, ? ( q 1, q 5 , q 6, 0 ) q 1, q 5 , q
6 , q 7, and ? (q 1, q 5 , q 6, 1) q 1, q
2, q 5, .
30Fourth, ? ( q 1, q 2 , q 5, 0 ) q 1, q 3 , q
5 , q 6, and ? (q 1, q 2 , q 5, 1) q 1, q
2, q 5, .
31Fifth, ? ( q 1, q 3 , q 5 , q 6, 0 ) q 1, q
5 , q 6 , q 7, and ? (q 1, q 3 , q 5 , q 6,
1) q 1, q 2, q 4 , q 5 , .
32Sixth, ? ( q 1, q 5 , q 6 , q 7, 0 ) q 1, q
5 , q 6 , q 7, and ? (q 1, q 5 , q 6 , q 7,
1) q 1, q 2, q 5 , .
33Seventh, ? ( q 1, q 2 , q 4 , q 5, 0 ) q 1,
q 3 , q 4 , q 5 , q 6, and ? (q 1, q 2 , q 4 ,
q 5, 1) q 1, q 2 , q 4, q 5 , .
34Eighth, ? ( q 1, q 3 , q 4 , q 5 , q 6, 0 )
q 1, q 4 , q 5 , q 6 , q 7, and ? (q 1, q 3 ,
q 4 , q 5 , q 6, 1) q 1, q 2 , q 4, q 5 , .
35Ninth, ? ( q 1, q 4 , q 5 , q 6 , q 7, 0 ) q
1, q 4 , q 5 , q 6 , q 7, and ? (q 1, q 4 , q
5 , q 6 , q 7, 1) q 1, q 2 , q 4, q 5 , .
36After rewrite the machine M2, we come up with the
following result.
The above machine can be simplified as follows.
37Example 8 Design a DFA M to accept strings over
0, 1 so that the evaluation of the string ?
according to the following operation is same as
the evaluation of ? R.
Solution Convert the previous NFA in example 4
to the following DFA.