You are on page 1of 13

Theory of Computation (With Automata Theory)

TOPIC TITLE: Equivalence of NFAs and DFAs


Specific Objectives:
At the end of the topic session, the students are expected to:
Cognitive:
1. Explain the rationale behind the equivalence between NFAs and
DFAs.
2. Apply the procedure for converting NFAs with no -transitions to
their equivalent DFAs.
Affective:
1. Listen to others with respect.
2. Participate in class discussions actively.

MATERIALS/EQUIPMENT:
o
o

topic slides
OHP

TOPIC PREPARATION:
o
o
o
o

Equivalence of NFAs and DFAs

Have the students review related topics that were discussed in


previous courses.
Prepare the slides to be presented in class.
It is imperative for the instructor to incorporate various kinds of
teaching strategies while discussing the suggested topics.
Prepare additional examples on the topic to be presented.

*Property of STI
Page 1 of 13

Theory of Computation (With Automata Theory)

NFA and DFA Equivalence


Page 1 of 21

Equivalence of NFAs and DFAs


NFA and DFA Equivalence
Introduction
Consider the following DFA M1 from the previous discussions.
0

q0

q1

q2

q3

0, 1

After analyzing this DFA, it can be said that DFA M1 accepts strings over the
alphabet = {0, 1} that start with a 0 and end with a 1.
NFA and DFA Equivalence
Page 2 of 21

Consider now the following NFA N1 also from the previous discussions.
0, 1

q0

q1

q2

After analyzing this NFA, it can be said that NFA N1 also accepts strings over the
alphabet = {0, 1} that start with a 0 and end with 1.
Since both can accept the same set of strings over the same alphabet, DFA M1
and NFA N1 recognize the same language. In other words, they have the same
language. That is,
L(M1) = L(N1) = {w w starts with 0 and ends with 1}
The language of M1 is equal to the language of N1, which is the set of all strings w
such that w starts with a 0 and ends with a 1.

Equivalence of NFAs and DFAs

*Property of STI
Page 2 of 13

Theory of Computation (With Automata Theory)

NFA and DFA Equivalence


Page 3 of 21

If an NFA and a DFA recognize the same language, they are said to be equivalent.
Therefore, DFA M1 is equivalent to NFA N1.
In general, any two machines (whether they are both DFAs, both NFAs, or one is a
DFA while the other is an NFA) are equivalent if they recognize the same
language.
If a language is recognized by a DFA, then there is an NFA that will also recognize
it. This is quite obvious since all DFAs are also NFAs.
But the question now is, if a language is recognized by an NFA, is there a DFA that
will also recognize it?
In other words, is there an equivalent DFA for any NFA?
The answer is not quite obvious since not all NFAs are DFAs.
To answer this question, a procedure must be constructed to convert any NFA to
its equivalent DFA. If no procedure can be constructed, then the answer is no.

NFA and DFA Equivalence


Page 4 of 21

Converting NFAs with no -Transitions into their Equivalent DFA


The procedure that will be discussed here is applicable only to converting NFAs
with no -transitions into their equivalent DFA.
Consider NFA N1 which has three states q0, q1, and q2. Recall that an NFA can be
in one or more states at any given time. So at any time, the NFA may be in:
1.
2.
3.
4.
5.
6.
7.

8.

Equivalence of NFAs and DFAs

state q0 only there is currently only one copy of the NFA and it is at
state q0.
state q1 only there is currently only one copy of the NFA and it is at
state q1.
state q2 only there is currently only one copy of the NFA and it is at
state q2.
states q0 and q1 there are currently two copies of the NFA. One
copy is at state q0 while the other is at state q1.
states q0 and q2 there are currently two copies of the NFA. One
copy is at state q0 while the other is at state q2.
states q1 and q2 there are currently two copies of the NFA. One
copy is at state q1 while the other is at state q2.
states q0, q1, and q2 there are currently three copies of the NFA.
One copy is at state q0, the second copy is at state q1, and the third
copy is at state q2.
dead state (stopped processing) All copies of the NFA have
stopped and ceased to exist.

*Property of STI
Page 3 of 13

Theory of Computation (With Automata Theory)

NFA and DFA Equivalence


Page 5 of 21

Observe that the possible states the NFA can assume are q0 only, q1 only, q2 only,
q0 and q1, q0 and q2, q1 and q2, q1 and q2 and q3, and the dead state.
It can therefore be said that possible states the NFA can be is the power set of its
set of states Q. Recall that the power set is simply the set of all subsets. If Q is
the set of states of the given NFA N1, then:
Q = {q0, q1, q2}
The power set of Q is then
P(Q) = {, {q0}, {q1}, {q2}, {q0,q1}, {q0, q2}, {q1, q2}, {q0, q1, q2}}
where the null set corresponds to the dead state.
Notice that each subset in the power set of Q corresponds to one of the states the
NFA N1 can assume at any given time.
Since a DFA can only be in one state at any one time while an NFA can be in
several states at a time, this mismatch can be resolved by constructing the
equivalent DFA such that it will have a single state that will represent the situation
where the NFA is in several states. In other words, the constructed DFA will have
a single state for each element of the power set.
For example, the state in which the NFA is in states q0 and q1 will be represented
by a single state (that may be called q01) in its equivalent DFA. If the NFA is in
states q0, q1, and q2 at the same time, then the equivalent DFA will have only one
state (that may be called q012) to represent that situation.
n

If the NFA has n states, then the equivalent DFA will have 2 states since there will
n
be 2 possible subsets given a set with n elements. This implies that the
equivalent DFA will have a finite number of states since the number of states in any
NFA is also finite.
In the example given, since the NFA N1 has 3 states, its equivalent DFA will have
3
at most 2 = 8 states.
NFA and DFA Equivalence
Page 6 of 21

The states of the equivalent DFA will then be

The next step now is to determine the start state and the final states of the
equivalent DFA.
The initial state of the NFA N1 is q0 so the initial state of the equivalent DFA will
also be q0.

Equivalence of NFAs and DFAs

*Property of STI
Page 4 of 13

Theory of Computation (With Automata Theory)

NFA and DFA Equivalence


Page 7 of 21

The final state of NFA N1 is q2. This means that the NFA is in the final state if at
least one copy of the NFA is at state q2. So the NFA is in the final state if it is in
any of the following states:
1.
2.
3.
4.

q2
q 0, q 2
q 1, q 2
q 0, q 1, q 2

In other words, the NFA will be in the final state if q2 is included in the subset.
Therefore, the final states for the equivalent DFA are q2, q02, q12, and q012.
Take note that not all of the states of the equivalent DFA are reachable from the
start state q0. These states are considered useless. It is thus possible that some
of these states will eventually be removed.
The states that will be removed will be determined upon the construction of the
transition function of the equivalent DFA.

NFA and DFA Equivalence


Page 8 of 21

Construction of the transition function for the equivalent DFA for NFA N1
The transition table for the equivalent DFA will be in the following form:

The procedure to be followed is that for each state that is reachable from state q0,
analyze to which state the equivalent DFA will go to given the arrival of each input
symbol.
Starting at state q0 (this is the DFA state that represents the situation where the
NFA is at state q0 only):
If input = 0, the NFA will go to state q1. So the NFA will be at state q1
only. This is represented by state q1 of the equivalent DFA.
If input = 1, the NFA will go to a dead state since all processing will stop.
This is represented by state qdead of the equivalent DFA.
Updating the transition function to reflect these gives

Equivalence of NFAs and DFAs

*Property of STI
Page 5 of 13

Theory of Computation (With Automata Theory)

The next states to be analyzed are states q1 and qdead since these are the states
that can be reached from q0 as of this time.
NFA and DFA Equivalence
Page 9 of 21

From state q1 (this is the DFA state that represents the situation where the NFA is
at state q1 only).
If input = 0, the NFA will stay at state q1. So the NFA will be at state q1
only. This is represented by state q1 of the equivalent DFA.
If input = 1, the NFA will have two copies. One copy will stay at state q1
while the other copy will go to state q2. So the NFA will be at states q1
and q2. This is represented by state q12 of the equivalent DFA.
From state qdead (this is the DFA state that represents the situation where all copies
of the NFA have stopped processing).
The NFA will stay at its dead state regardless of the input symbol
received. So the DFA will also stay at qdead regardless of the input
symbol arriving.

Updating the transition function to reflect these gives

The only state reachable from q0 that has not been analyzed so far is state q12 (it is
reachable from state q0 through state q1). Therefore, it will be analyzed next.

Equivalence of NFAs and DFAs

*Property of STI
Page 6 of 13

Theory of Computation (With Automata Theory)

NFA and DFA Equivalence


Page 10 of 21

From state q12 (this is the DFA state that represents the situation where the NFA is
at states q1 and q2)
If input = 0, the copy of the NFA which is at state q1 stays at q1. The
other copy which is at state q2 will stop processing. So the NFA will be at
state q1 only. This is represented by state q1 of the equivalent DFA.
If input = 1, the copy of the NFA which is at state q1 will go to states q1
and q2. While the other copy which is at q2 will stop processing. So the
NFA will be at states q1 and q2. This is represented by state q12 of the
equivalent DFA.

Updating the transition function to reflect these gives

NFA and DFA Equivalence


Page 11 of 21

So far the only states of the equivalent DFA that came out during the analysis are
q1, q12, and qdead. All these three states have been analyzed already (they have
complete entries in the transition function). It can therefore be said that these
states are the only states that are reachable from the start state q0.
The transition function will then be

Observe that the DFA states q2, q01, q02, and q012 are not reachable from state q0.
These are the useless states. For example, state q2 of the equivalent DFA is
unreachable from state q0 because there is no situation wherein the NFA N1 will be
in state q2 only. Similarly, state q012 of the equivalent DFA is unreachable from
state q0 because there is no situation wherein the NFA N1 will be at states q0, q1,
and q2 at the same time.
These states can therefore be removed from the transition table.

Equivalence of NFAs and DFAs

*Property of STI
Page 7 of 13

Theory of Computation (With Automata Theory)

NFA and DFA Equivalence


Page 12 of 21

The final transition function will then be

The state diagram of the equivalent DFA is


0

q0

q1

NFA and DFA Equivalence


Page 13 of 21

q12

qdead

0, 1

As another example, consider now the following NFA N2.


0, 1

q0

0, 1

q1

q2

Take note that N2 accepts strings over the alphabet = {0, 1} that contain the
substring 11.
Since this NFA has three states, the possible states N2 can assume are q0 only, q1
only, q2 only, q0 and q1, q0 and q2, q1 and q2, q1 and q2 and q3, and the dead state.
This is similar to the previous example.

Equivalence of NFAs and DFAs

*Property of STI
Page 8 of 13

Theory of Computation (With Automata Theory)

NFA and DFA Equivalence


Page 14 of 21

So the states of the equivalent DFA will be:

The next step now is to determine the start state and the final states of the
equivalent DFA.
The initial state of the NFA N2 is q0 so the initial state of the equivalent DFA will
also be q0.
NFA and DFA Equivalence
Page 15 of 21

The final state of NFA N2 is q2. This means that the NFA is in the final state if one
copy of the NFA is at state q2. So the NFA is in the final state if it is in any of the
following states:
1.
2.
3.
4.

q2
q 0, q 2
q 1, q 2
q 0, q 1, q 2

In other words, the NFA will be in the final state if q2 is included in the subset.
Therefore, the final states for the equivalent DFA are q2, q02, q12, and q012.

The transition table for the equivalent DFA will be in the following form:

For each state that is reachable from state q0, we next analyze to which state the
equivalent DFA will go to given the arrival of each possible input symbol.
Starting at state q0 (this is the DFA state that represents the situation where the
NFA is at state q0 only):
If input = 0, the NFA will go to state q0. So the NFA will be at state q0
only. This is represented by state q0 of the equivalent DFA.
If input = 1, the NFA will have two copies. One copy will stay at state q0

Equivalence of NFAs and DFAs

*Property of STI
Page 9 of 13

Theory of Computation (With Automata Theory)

while the other copy will go to state q1. So the NFA will be at states q0
and q1. This is represented by state q01 of the equivalent DFA.

Updating the transition function to reflect these gives

The next state to be analyzed is state q01 since this is the only state that is
reachable from q0 that has not been analyzed so far.
NFA and DFA Equivalence
Page 16 of 21

From state q01 (this is the DFA state that represents the situation where the NFA is
at states q0 and q1)
If input = 0, the copy of the NFA which is at state q0 will stay at q0. The
other copy which is at q1 will stop processing. The NFA will then be at
state q0 only. This is represented by state q0 of the equivalent DFA.
If input = 1, the copy of the NFA which is at state q0 will go to states q0
and q1. The other copy, which is at state q1, will go to state q2. The NFA
will then be at states q0, q1, and q2. This is represented by state q012 of
the equivalent DFA.

Updating the transition function to reflect these gives

The only state reachable from q0 that has not been analyzed so far is state q012 (it
is reachable from state q0 through state q01). Therefore, it will be analyzed next.

Equivalence of NFAs and DFAs

*Property of STI
Page 10 of 13

Theory of Computation (With Automata Theory)

NFA and DFA Equivalence


Page 17 of 26

From state q012 (this is the DFA state that represents the situation where the NFA is
at states q0,q1, and q2)
If input = 0, the copy of the NFA which is at state q0 will stay at state q0.
The second copy which is at state q1 will stop processing. The third copy
which is at state q2 will stay at state q2. The NFA will then be at states q0
and q2. This is represented by state q02 of the equivalent DFA.
If input = 1, the copy of the NFA which is at state q0 will go to states q0
and q1. The second copy which is at state q1 will go to state q2. The third
copy of the NFA which is at state q2 will stay at state q2. The NFA will
then be at states q0, q1, and q2. This is represented by state q012 of the
equivalent DFA.

Updating the transition function to reflect these gives

The only state reachable from q0 that has not been analyzed so far is state q02 (it is
reachable from state q0 through state q01 and then through q012). Therefore, it will
be analyzed next.
NFA and DFA Equivalence
Page 18 of 21

From state q02 (this is the DFA state that represents the situation where the NFA is
at states q0 and q2)
If input = 0, the copy of the NFA which is at state q0 will stay at state q0.
The other copy which is at state q2 will stay at state q2. The NFA will then
be at states q0 and q2. This is represented by state q02 of the equivalent
DFA.
If input = 1, the copy of the NFA which is at state q0 will go to states q0
and q1. The other copy which is at state q2 will stay state q2. The NFA
will then be at states q0, q1, and q2. This is represented by state q012 of
the equivalent DFA.

Updating the transition function to reflect these gives

Equivalence of NFAs and DFAs

*Property of STI
Page 11 of 13

Theory of Computation (With Automata Theory)

NFA and DFA Equivalence


Page 19 of 21

So far the only states of the equivalent DFA that came out during the analysis are
q01, q02, and q012. All these three states had been analyzed already (they have
complete entries in the transition function. It can therefore be said that these states
are the only states that are reachable from the start state q0.
The transition function will be

Observe that states q1, q2, q12, and qdead are not reachable from state q0. These
can therefore be removed from the transition table.

Equivalence of NFAs and DFAs

*Property of STI
Page 12 of 13

Theory of Computation (With Automata Theory)

NFA and DFA Equivalence


Page 20 of 21

The final transition table will then be

The state diagram of the equivalent DFA is


0

q0

q01

q012

0
1

NFA and DFA Equivalence


Page 21 of 21

q02

For the given NFA, have the students construct the equivalent DFA.
The given NFA accepts all strings that end with a 00.

q0

q01

q012

1
1

[NFA and DFA Equivalence, Pages 121 of 21]

Equivalence of NFAs and DFAs

*Property of STI
Page 13 of 13