NFAs

advertisement
Nondeterministic Finite Automata (NFAs)
As mentioned earlier, a finite automaton is nondeterministic if from a given state and symbol
there may be more than one move, or no move at all. Thus, an NFA can be defined as
follows: M = (Q, , , q0, F) where Q, , q0, and F are the same as in a DFA, but the
transition function : Q  (  {})  2Q. There are three types of moves we haven’t seen
before:
(q1, a) = {q2, q3} May move to more than one state
(q1, ) = {q4, q5} May make a move without using any input
(q1, b) =  There is no move defined from this configuration.
In the diagram above note that we can either stay in q0 or move to q3 when an a is read.
Because of the -move from q0 to q1 we may effectively leave the a in the input, move to q1
and then move to q2 when the a is read a move. To make things even more interesting
suppose we go from q0 to itself when the a is read and then take the -move out of q0. In this
case, we have gone to q1 on an a. Thus, (q0, a) = {q0, q1, q2, q3} when the -move is taken
into account. We’ll see more of this shortly when we convert NFAs to DFAs. Basically we
need to look at every path labeled a that starts in q0.
We must now redefine our notion of acceptance—an NFA accepts a string w if there is some
sequence of transitions, corresponding to w, from q0 to a final state. Looking at the graphical
representation, we effectively have to check all possible paths to a final state to determine if
the string is in the language. Note that this does not mean the machine automatically makes
the correct choice each time, just that there is a sequence of choices that will lead to a final
state. Similarly, a string is rejected only if there is no possible sequence of moves that will
take the machine to a final state with that string as input.
Let’s consider what this means when we parse a string and the automaton stops:
If, on input w, M stops in a final state, we know the string w is in the language.
If, on input w, M stops in a nonfinal state it does not mean that w is not in the language
accepted by the machine, merely that the path we traveled did not lead to a final state.
We shall shortly show that NFA’s and DFA’s are equivalent i.e. any language that can be
recognized by an NFA can be recognized by a DFA. One direction in the proof is easy since
every DFA is an NFA.
Then, why do we bother with NFA’s? It’s frequently the most straightforward way to construct
a machine just as recursion is often the easiest way to construct a solution to a problem.
NFA Example
M = ( {q0, q1, q2, q3}, {0, 1}, , q0, {q3} ) where the transition function is as follows:

0
1
q0
{q1}
{q2}
q1
{q1, q3}
{q1}
q2
{q2}
{q2, q3}
q3


Draw the graph corresponding to this automaton to the right of the table.
Looking at your graph it should be straightforward to see that L(M) = {w  {0, 1}* | w begins
and ends with the same symbol}
M accepts w iff there is a path from q0 to q3 labeled w. Let’s look at the possible paths for the
string 0100
0
1
0
q0  q1  q1  q3
0
 q1  q3
 q1
We could put in state q4 and make it a trap state if we ever leave q3. This permits us to read
all of the input. Otherwise, we would reject if the machine stopped without reading all of w.
What kinds of strings are accepted by the machine whose diagram was shown above? Try
listing all strings of length five or less accepted by the machine.
Extending the Transition function
Just as with a DFA, we can extend the transition function to work on strings. In this case we
take a state and a string of symbols from the alphabet (including  since we have *). Thus
we define the extended transition function *: Q  *  2Q. Notice that rather than ending up
in a single state we could move to any state in a set of states, including the empty set.
If *(qi, w) = Qj then Qj is the set of all possible states the automaton may be in having started
in qi and having read w. We could give a mathematical definition for the extended transition
function, but instead we’ll define it based on the associated transition graph.
Definition 2.5 For an NFA, the extended transition function is defined so that *(qi, w) contains
qj if and only if there is a walk in the transition graph from qi to qj labeled w. This holds for all
qi, qj Q and w  *.
Even though  is not explicitly mentioned in the definition, a path labeled w may have edges
labeled by  in it. For example
Let’s consider the automaton depicted below and look at the extended transition function:
(q0, a) = {q0, q1, q2} since there are edges labeled a to each of the three states.
*(q2, c) = {q1, q2} since the path c takes us from q2 to q1. Since we’re interested in the label
of the path we don’t write  when we specify the path, but the path may still contain edges
labeled .
Note also that *(qi, ) always includes qi for all i. Moveover, * is completely defined since
between any pair of vertices in the transition graph, there either is or is not a walk. To actually
compute * we must generate all walks. However, this is not as straightforward as before
since there may be  edges involved.
We can now formalize the definition of acceptance for an NFA M:
L(M) = { w  * | *(q0, w)  F  }.
This basically says there is some path through the machine from the start state to a final state.
However, there may be some -edges in that walk.
Let’s now look at a simple proof about the equivalence of machines. This is problem 18 on
page 55. Define an NFA with multiple start states by the quintuple M = (Q, , , Q0, F) where
Q0  Q is a set of possible initial states. The language accepted by such an automaton is
defined as L(M) = { w | *(q0, w) contains qf for some q0  Q0 and qf  F}. We now show that
for every NFA with multiple start states there is an NFA with a single start state that accepts
the same language. Before reading the proof, think about how you might do this. Hint: moves are very helpful.
Proof: Construct a new automaton M′ = (Q  {p0}, , ′, p0, F)
 and F remain the same, and we introduce a new state p 0 that is the start state of M′. We
keep all the transitions defined for M and introduce new transitions: (p0, ) = {qi  Q0). It
should be clear that if w  L(M) then there is a path labeled w from some q0  Q0 to a final
state. But then, the path that begins with the -edge from p0 to q0 and follows the same wlabeled path in M is a path labeled w in M′ so w  L(M′). Conversely, suppose x  L(M′).
Then there must be a path in the transition diagram for M′ that has the label x, begins at p 0,
and ends at some qf  F. But, the first edge of the path must be one of the -edges, say the
edge from p0 to qi. Notice that the remainder of the path has the label x and uses only edges
from the transition diagram for M so that x  L(M) also.
Section 2.3 Equivalence of Deterministic and Nondeterministic Finite Acceptors
Definition 2.7 Two finite accepters M1 and M2 are said to be equivalent if L(M1) = L(M2).
That is, the language accepted by M1 is the same as the language accepted by M2. We’ll
shortly show that DFAs and NFAs accept the same languages. That is, for every NFA there is
a DFA that accepts the same language and vice versa. Since every DFA is an NFA, all we
need to prove is that if L = L(M) for some NFA M, then there is a DFA D such that L(D) = L.
Let’s look at the machine below which accepts all strings over {0, 1} which begin and end with
the same symbol and have length two or more:
Let’s look at a DFA that accepts the same language. One approach that can be used is to
construct a machine in which the edge labeled 0 leads to a submachine that accepts all
strings beginning and ending in 0. Similarly, the edge labeled 1 goes from qo to a
submachine that accepts all strings that begin and end in 1. Now, what must these
submachines look like? Since the submachines are identical except for the labels on the
edges and states, let’s consider strings that begin and end in 0. Suppose on an input of 0 the
machine moves from q0 to q1. If (q1, 0) = q2, then q2 should be an accept state.
Now, we need to consider what other edges and states we need. If the machine sees a 0
while in q2 it should loop. If in q1 or q2 a 1 is read, then the machine will move to a new state
q3 where it will wait until another 0 is read. Thus, in q3, we’ll loop on a 1 and move back to q2
on a 0. Thus, we have the following moves:
(q1, 0) = q2
(q1, 1) = q3
(q2, 0) = q2
(q2, 1) = q3
(q3, 0) = q2
(q3, 1) = q3
Thus an input of 0 always leads to q2 and an input of 1 goes to q3. Below is the machine built
using this strategy.
Note that this machine is not minimal and we shall later show how to minimize the number of
states needed to recognize this language.
Suppose an NFA M accepts a string w. The proof that NFAs are no more powerful than DFAs
depends on three observations:
1. After accepting a string M may be in any state in a set {qi, …, qk}.
2. The DFA must be in one definite state.
3. Since Q is finite, so is 2Q.
Thus, to make the equivalent DFA, we label the states of the machine we’re building with
subsets of Q i.e. each state of the DFA corresponds to a subset of Q. Each state in the DFA
is labeled by the corresponding set. As you might suspect, most of the time not all states in
2Q are needed. Thus, we usually only construct new states as needed. This method is
known as the subset construction.
Let’s go back to the NFA that accepts all strings of length at least two that begin and end in
the same symbol.
The states of the DFA we’re building are {, {q1}, {q2}, {q3} …} In practice, we’ll generate the
states as needed and use only those states. The start state of our DFA will be {q0}. The
transition function will be called ´to distinguish it from the transition function of the NFA. In
M, an input of 0 leads to {q1} and an input of 1 leads to {q2}. Thus, two moves for the
deterministic machine are ′({q0}, 0) = {q1}, and ′({q0}, 1) = {q2}. At this point the partially
constructed DFA is :
From q1 in the original machine M we can reach either q1 or q3 i.e. (q1, 0) = {q1, q3} so the
corresponding move in the DFA is ′({q1}, 0) = {q1, q3}. The table below contains the moves of
the DFA being built.
′
0
1
{q0}
{q1}
{q2}
{q1}
{q1, q3}
{q1}
{q2}
{q2}
{q2, q3}
{q1, q3}
{q1, q3}
{q1}
{q2, q3}
{q2}
{q2, q3}
Now, looking at the table for states {q1} and {q2}, we see we will need new states {q1, q3} and
{q2, q3}. The final states of the DFA are any states that contain one or more final states of
the original NFA. In this case, the final states are {q1, q3} and {q2, q3}. Here’s the final DFA:
Note that we did not have any moves corresponding to Ø, {q3}, {q1, q2} or {q1, q2, q3}. Also,
there are fewer states than in the machine we drew earlier. We formalize this procedure in
the proof of Theorem 2.2.
Theorem 2.2: Let L be the language accepted by an NFA MN = (QN, , N, q0, FN). Then
there is a DFA MD = (QD, , D, {q0}, FD) such that L = L(MD).
Proof sketch: Again we’re arguing from the graph and will follow the procedure which is
formalized below to construct the transition graph GD. Recall that in GD every vertex must
have an edge with labeled a for every a  . The procedure continues until no new states are
generated.
procedure nfa to dfa
1. Create a graph GD with vertex {q0}.
2. Repeat the following steps until no more edges are missing
a. Select a vertex {qi, qj, …, qk} which does not have a transition for some a  
b. Form the union *(qi, a)  *(qj, a)  …  *(qk, a) = {qr, qs, …, qt}
c. Create a vertex in GD with label {qr, qs, …, qt} if it doesn’t already exist
d. Insert the edge from {qi, qj, …, qk} to {qr, qs, …, qt} in GD and label it a
3. Make every state in GD whose label contains any qf  F a final state
4. If MN accepts , then make {q0} a final state also.
Justification as to why this works:
The process must stop since GD must have a finite number of edges. The maximum number
of edges is 2QN • ||.
Note: In the discussion that follows a subscript of D means we’re talking about a deterministic
machine or its graph and a subscript of N indicates nondeterminism.
To show that L(MD) = L(MN), we need to use induction on the length of a string.
Hypothesis: Assume that if |v|  n if there is a walk labeled v from q0 to qi in GN, then there is
a walk in GD labeled v from {q0} to a state Qi = {…,qi,…}. (Remember that some edges of the
walk may be represent -moves.)
Induction step: Now, let w = va where a  . Suppose there is a walk in GN from q0 to qk
labeled w. Then, in GN, there must be a walk labeled v from q0 to qi and an edge or sequence
of edges labeled a from qi to qk. By hypothesis, GD contains a walk labeled v from {q0} to Qi.
Since qi  Qi, in step 2b of the procedure, the set created must contain *(qi, a) which
contains state qk. Thus, there is an edge from Qi to some state whose label contains qk. This
works for all strings of length n + 1. Whenever *N(q0, w) contains a final state qf, then the
label of *D({q0}, w) also contains qf.
This basically takes us one direction. We must also show that if the label of *D({q0}, w)
contains a final state, then so does *N(q0, w).
Section 2.4 Reduction of the Number of States in Finite Automata
Ideally, we’d like our automata to have as few states as possible. There are some obvious
things we can do to reduce the number of states. For example, if a state is unreachable (i.e.
you can’t get there on a walk starting from q0) then we may remove it. Similarly, if there is no
path from a particular state that leads to a final state then we may remove that state. Other
than that, we need to determine if some states in the machine are basically equivalent.
Specifically, we determine if two states are indistinguishable, and if so remove one of them.
Formally, we have definition 2.8
Two states p and q of a DFA are indistinguishable if *(p, w)  F implies *(q, w)  F and
*(p, w)  F implies *(q, w)  F. In other words, there is no string on which they behave
differently. If there is a string w  * such that *(p, w)  F and *(q, w)  F then p and q are
said to be distinguishable by string w. Basically, what we are doing is forming an
equivalence relation on the states.
The method below is presented because it’s in the text. There is another method we will use
that is described following this procedure.
Procedure mark will identify distinguishable states.
1. Remove all inaccessible states.
2. Consider all pairs of states (p, q). If p  F and q  F then p and q are distinguishable
3. Repeat the following process until no new pairs are marked
For all pairs (p, q) and all a  , compute (p, a) = pa and (q, a) = qa. If (pa, qa) is marked
distinguishable, then mark p and q as being distinguishable also.
Theorem 2.3 The procedure mark applied to any DFA terminates and determines all pairs of
distinguishable states.
After the marking has been done we need to reduce the automaton. Basically this involves
combining each set of indistinguishable states into a single state. I’m going to skip this
because I think another method is easier to use. Let’s go back to the first DFA we
constructed for strings beginning and ending in the same symbol and reduce it to the second
version.
We’ll do this with a series of tables. As in the mark procedure we initially divide the set of
states into two groups—the final or accepting states and the nonaccepting states. The blue
strip separates the nonaccept states from the accept states. Let’s call the nonaccept states
group A and the accept states group B. For each state in the original machine, determine if
the transitions lead to group A or group B. For example, since q1 and q4 are both nonaccept
states in group A, then, from q0, both 0 and 1 take us to group A. A 0 in q1 leads to a final
state (group B) while a 1 in q1 leads to a nonaccept state in group A. This describes the first
two rows of the table below. The rest of it is filled in in a similar way.
q0
q1
q3
q4
q5
0
A
B
B
A
A
1
A
A
A
B
B
q2
q6
B
A
A
B
Observe that the states in group B behave differently so they are distinguishable. In
group A, we split the states into three groups. As shown in the table on the left, for the
next iteration q0 will be in our new group A, q1 and q3 will be in group B, q4 and q5
comprise group C while groups D and E contain q2 and q6, respectively. After the
moves are entered, the diagram on the right is obtained. Notice that, for each group,
the states in that group behave in the same way and are thus indistinguishable.
Now, the DFA below, corresponding to the table on the right above, is the minimal DFA
for this language.
Download