You are on page 1of 18

Theory of Computation (With Automata Theory)

TOPIC TITLE: Nondeterministic Finite Automata


Specific Objectives:
At the end of the topic session, the students are expected to:
Cognitive:
1. Define nondeterminism.
2. Enumerate
and
explain
the
differences
between
nondeterministic finite automata (NFA) and deterministic finite
automata (DFA).
3. Explain how an NFA performs its computation.
4. Discuss how -transitions are handled.
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

Nondeterministic Finite Automata

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 18

Theory of Computation (With Automata Theory)

Nondeterminism
Page 1 of 33

Nondeterministic Finite Automata


Nondeterminism
Introduction
The main characteristic of a DFA is that for each input symbol, the automaton can
move to only one state from any given current state.
Consider a DFA that accepts strings over the alphabet = {0, 1} that starts with a 0
and ends with a 1 as shown below:
0

q0

q1

q2

q3

0, 1

At state q0, for example, if the input is 0, the DFA moves to only one state which is
q1. If the input is 1, the DFA moves again to only one state which is q3. The same
can be said about all the other states.
Since the DFA can move to only one state from any given current state, then this
implies that a DFA can only be in one state at any one time.
Nondeterminism
Page 2 of 33

Consider now the following finite automaton:


0, 1

q0

q1

q2

Without going through detailed computations, it can be observed that this machine
also accepts strings that start with a 0 and end with 1.
A careful examination of this machine shows that if it is at state q1 and the input is
equal to 1, the machine goes to state q2 and stays at q1 at the same time. This is
because state q2 has two outgoing edges labeled with the symbol 1. States in a
DFA always have only one outgoing edge for each input symbol.
The given machine is not considered a DFA because there is a situation in which a
single input symbol causes the automaton to move to more than one state.

Nondeterministic Finite Automata

*Property of STI
Page 2 of 18

Theory of Computation (With Automata Theory)

Nondeterministic
Page 3 of 33

The given graph is the state diagram for a nondeterministic finite automaton
(NFA).
Because an NFA can move to more than one state given a single input symbol, an
NFA is said to have choices.
Take the given NFA as an example. If the NFA is currently in state q1 and an input
symbol 1 arrives, the NFA has the option of either staying in state q1 or going to
state q2. In DFAs, there is always no choice. There is always one state to go to
given a certain input symbol.

Nondeterminism
Page 4 of 33

Since the NFA has the option of either staying in state q1 or going to state q2, the
NFA will consider both possibilities.
How will this be done?
The NFA will make a copy of itself. There are now two copies of the same NFA.
One copy will stay at state q1 and continue the computation from there. The
second copy will go to state q2 and also continue the computation from that state.
From this point on, there will be two computations occurring simultaneously.
In other words, the NFA has the ability to guess the outcome of the computation.
In the example given, the NFA tries to see if a string will be accepted by continuing
the computation at state q1. At the same time, it also tries to see if the same input
string will be accepted if it continues the computation at state q2.
Effectively, the NFA is said to be in two states, q1 and q2, at the same instance.
This is another characteristic of NFAs. It is possible for an NFA to be in several
states at any given time.
This is in contrast with a DFA since DFAs can only be in one state at a time. In
DFAs, there can only be one computation occurring at any time.

Nondeterministic Finite Automata

*Property of STI
Page 3 of 18

Theory of Computation (With Automata Theory)

Nondeterminism
Page 5 of 33

Notice also that in an NFA, it is possible that a state may not have a transition edge
for one or more of the input symbols of the alphabet.
In the given example, take note that for state q0, there is a transition edge for input
symbol 0 but none for 1. If there happens to be a copy of the NFA that is currently
at state q0 and an input symbol 1 arrives, this copy has nowhere to go because
there is no outgoing or transition edge for an input equal to 1. The computation
being done by this copy of the NFA stops and it ceases to exist. This copy of the
NFA simply dies.
Similarly, state q2 of the given NFA has no outgoing edge for both input symbols of
the alphabet. So if there happens to a copy of the NFA that is at state q2 and 1 or
a 0 arrives, this copy also stops processing and dies.

Nondeterminism
Page 6 of 33

Here is an example to illustrate how an NFA processes strings.


Given the same NFA that accepts strings that start with a 0 and end with a 1:
0, 1

q0

q1

q2

Let us determine if the string 01011 will be accepted.


1.

Initially, the NFA will be at state q0. When the first input symbol (0)
arrives, the NFA goes to state q1 as shown below:

input

state
q0

start

0
q1

Nondeterministic Finite Automata

*Property of STI
Page 4 of 18

Theory of Computation (With Automata Theory)

Nondeterminism
Page 7 of 33

2.

Assume now that the second input symbol (1) arrives. The NFA (which is
currently in state q1) has two options, either it stays at q1 or goes to q2.
The machine will try both possibilities at the same time. It will then stay at
state q1 and go to state q2 simultaneously. This is shown below:

input

state
q0 (start)

0
q1
1
q1

q2

In other words, the NFA made another copy of itself. One copy to
continue the processing at state q1 and the other at state q2. There are
now two computations occurring at the same time. The NFA is said to be
in both states q1 and q2.
Nondeterminism
Page 8 of 33

3.

Assume now that the third input symbol (0) arrives. There are currently
two copies of the NFA, one is at state q1 while the other is at state q2.
The copy of the NFA which is at state q1 stays at q1. The copy which is at
state q2 stops processing because it has nowhere to go. In other words,
this copy of the NFA dies.

input

state
q0 (start)

0
q1
1
q1

q2 (stopped)

0
q1
Hence, at this point in time, there will only be one remaining copy of the
NFAthe one that is still at state q1.
If it so happens that all copies of the NFA die at a certain input symbol,
then the entire NFA ceases to exist and the string is rejected.

Nondeterministic Finite Automata

*Property of STI
Page 5 of 18

Theory of Computation (With Automata Theory)

Nondeterminism
Page 9 of 33

4.

Assume now that the fourth input symbol (1) arrives. There is only one
copy of the NFA and it is currently in state q1. The NFA will again have
two options, either stay at q1 or go to q2. The NFA will again consider
both possibilities. So it will create another copy of itself. One copy will
continue the computation at state q1 while the other at state q2.
input

state
q0 (start)

0
q1
1
q1

q2 (stopped)

0
q1
1
q1

q2

The NFA is again said to be in states q1 and q2 simultaneously.


Nondeterminism
Page 10 of 33

5.

Assume now that the fifth and last input symbol (1) arrives. There are
currently two copies of the NFA, one is at state q1 and the other at q2.
The copy of the NFA which is at state q1 has the option of staying at q1 or
going to q2. As usual, the NFA will consider both so it creates another
copy of itselfone to continue the computation at state q1 and the other
at state q2.
The other copy which is at state q2 stops processing and ceases to exist.

input

state
q0 (start)

0
q1
1
q1

q2 (stopped)

0
q1
1
q1

q2 (stopped)

q1

q2

Nondeterministic Finite Automata

*Property of STI
Page 6 of 18

Theory of Computation (With Automata Theory)

Nondeterminism
Page 11 of 33

After the last input, there are two copies of the NFA. One copy is currently at state
q1 and another at state q2 (which is a final state). So the NFA is currently in both
states q1 and q2. Since at least one of the copies is at a final state, the NFA
accepts the input string 01011.
If it so happens that there is no copy of the NFA that is in a final state, then the
string will be rejected.

Here is another example. Given the following NFA, determine if the string 110110
will be accepted.
0, 1

q0

Nondeterminism
Page 12 of 33

1.

q1

0, 1

0, 1

q2

q3

Initially, the NFA will be at state q0. When the first input symbol (1)
arrives, the NFA has two optionseither stay at q0 or go to q1. The NFA
will now create a copy of itself. One copy continues the processing at
state q0 while the other copy continues at state q1.

state

input

q0

(start)

1
q0

q1

There will now be two copies of the NFA. One copy is at state q0 while
the other is at state q1. So the NFA is said to be in states q0 and q1.

Nondeterministic Finite Automata

*Property of STI
Page 7 of 18

Theory of Computation (With Automata Theory)

Nondeterminism
Page 13 of 33

2.

There are currently two copies of the NFA. One is at state q0 while the
other is at q1. When the second input symbol (1) arrives, the copy of the
NFA which is at state q0 has two optionseither stay at q0 or go to q1.
The machine will try both possibilities so it creates a copy of itself. One
copy continues the processing at q0 while the other at state q1.
On the other hand, the copy which is at state q1 will go to state q2.

state

input

q0

(start)

1
q0

q1

1
q0

q1

q2

There will now be three copies of the NFA. One is at state q0, the second
one is at state q1, and the third is at state q2. The NFA is then said to be
in states q0, q1, and q2.
Nondeterminism
Page 14 of 33

3.

There are currently three copies of the NFA. One is at state q0, another
at state q1, and the third copy is at state q2. When the third input symbol
(0) arrives, the copy of the NFA which is at state q0 stays at q0, the copy
which is at state q1 goes to q2, and the copy which is at q2 goes to state
q 3.
state

input

q0

(start)

1
q0

q1

1
q0

q1

q2

q0

q2

q3

The NFA is now said to be in states q0, q2, and q3.

Nondeterministic Finite Automata

*Property of STI
Page 8 of 18

Theory of Computation (With Automata Theory)

Determinism
Page 15 of 33

4.

There are currently three copies of the NFA. One is at state q0, another
at state q2, while the third copy is at state q3. When the fourth input
symbol (1) arrives, the copy of the NFA which is at state q0 has two
optionsstay at q0 or go to q1. The machine will try both possibilities so it
creates another copy of itselfone copy to continue the processing at
state q0 and another to continue at state q1.
The second copy which is at state q2 will go to q3.
The third copy which is at state q3 stops computing because it has
nowhere to go.
state

input

q0

(start)

1
q0

q1

1
q0

q1

q2

q0

q2

q3 (stopped)

1
q0

q1

q3

The NFA is now said to be at states q0, q1, and q3.


Nondeterminism
Page 16 of 33

5.

There are currently three copies of the NFA. One is at state q0, another
at state q1, while the third copy is at state q3. When the fifth input symbol
(1) arrives, the copy of the NFA which is at state q0 has two optionsstay
at q0 or go to q1. The machine will try both possibilities so it creates
another copy of itselfone copy to continue the processing at state q0
and another to continue at state q1.
The second copy which is at state q1 will go to q2.
The third copy which is at q3 stops processing and ceases to exist.
state

input

q0

(start)

1
q0

q1

1
q0

q1

q2

q0

q2

q3 (stopped)

1
q0

q1

q3 (stopped)

1
q0

q1

q2

The NFA is now said to be at states q0, q1, and q2.

Nondeterministic Finite Automata

*Property of STI
Page 9 of 18

Theory of Computation (With Automata Theory)

Nondeterminism
Page 17 of 26

6.

There are currently three copies of the NFA. One is at state q0, another
at state q1, while the third copy is at state q2. When the sixth and final
input symbol (0) arrives, the copy of the NFA which is at state q0 stays at
q0. The second copy which is at state q1 goes to q2. The third copy
which is at state q2 goes to q3.
state

input

q0

(start)

1
q0

q1

1
q0

q1

q2

q0

q2

q3 (stopped)

1
q0

q1

q3 (stopped)

1
q0

q1

q2

q0

q2

q3

Nondeterminism
Page 18 of 33

The NFA is now at three states, q0, q2, and q3. Since one of these states is a
final state (q3), then the NFA accepts the input string 110110.

Take note that the NFA in this example will accept all strings whose third to the last
input symbol is a 1.
-Transitions
One other difference between an NFA and a DFA is that an NFA may have transitions (epsilon transitions). An -transition is shown below:

qi

qj

An -transition, also called silent transition, from state qi to state qj is depicted as


a directed edge from qi to qj with a label signifying no input symbol.

Nondeterministic Finite Automata

*Property of STI
Page 10 of 18

Theory of Computation (With Automata Theory)

Nondeterminism
Page 19 of 33

An -transition from state qi to state qj means that once an NFA goes to state qi, it
has the option of staying at qi or it can go right away to state qj even without any
additional input arriving.
So once an input symbol causes the NFA to go to state qi, the NFA will
automatically create a copy of itself that will also go to qj. So the NFA will be in
states qi and qj at the same time.
Example:
Given the following NFA, determine if the string 010010 will be accepted.
0

q0

q1

q2

Nondeterminism
Page 20 of 33

1.

Initially, the NFA will be at state q0. When the first input symbol (0)
arrives, the NFA goes to q1. At q1, it has the option of immediately going
to q2 without any new input symbol arriving. This is because of the transition from state q1 to state q2. The NFA will consider both
possibilities so it creates another copy of itself. One copy will continue
the computation at state q1 while the other copy will continue at state q2.
input

state
q0 (start)

0
q1

q2

The NFA is said to be in states q1 and q2.

Nondeterministic Finite Automata

*Property of STI
Page 11 of 18

Theory of Computation (With Automata Theory)

Nondeterminism
Page 21 of 33

2.

There are currently two copies of the NFA. One copy is at state q1 while
other is at state q2. When the second input symbol (1) arrives, the copy
of the NFA which is at state q1 stops computing since it has nowhere to
go. It therefore ceases to exist. The copy at state q2 will go to state q0.
The NFA will then only be in one state which is q0.
input

state
q0 (start)

0
(stopped)

q1

q2

1
q0

Nondeterminism
Page 22 of 26

3.

There is currently only one copy of the NFA which is at state q0. When
the third input symbol (0) arrives, the NFA goes to q1. But once at q1, it
has the option of immediately going to q2 without any new input symbol
arriving. This is caused again by the -transition from state q1 to state q2.
The NFA will consider both possibilities so it creates another copy of
itself. One copy will continue the computation at state q1 while the other
copy will continue at state q2.
input

state
q0 (start)

0
(stopped)

q1

q2

1
q0
0
q1

q2

The NFA is now said to be in states q1 and q2.

Nondeterministic Finite Automata

*Property of STI
Page 12 of 18

Theory of Computation (With Automata Theory)

Nondeterminism
Page 23 of 33

4.

There are currently two copies of the NFA. One copy is at state q1 while
other is at state q2. When the fourth input symbol (0) arrives, the copy of
the NFA which is at state q1 stays at q1. Since there is an -transition
from q1 to q2, it will also immediately go to q2 by creating another copy of
itself.
The second copy of the NFA (the one at state q2) ceases to exist
because it has nowhere to go.
input

state
q0 (start)

0
(stopped)

q1

q2

1
q0
0
q1

q2 (stopped)

q1

q2

The NFA is now in states q1 and q2.


Nondeterminism
Page 24 of 33

5.

There are currently two copies of the NFA. One copy is at state q1 while
the other is at state q2. When the fifth input symbol (1) arrives, the copy
of the NFA which is at state q1 stops computing since it has nowhere to
go. The remaining copy of the NFA (the one at state q2) goes to q0.
There is now only one copy of the NFA.
input

state
q0 (start)

0
(stopped)

q1

q2

1
q0
0
q1

q2 (stopped)

0
(stopped) q1

q2

1
q0

The NFA is said to be in state q0 only.

Nondeterministic Finite Automata

*Property of STI
Page 13 of 18

Theory of Computation (With Automata Theory)

Nondeterminism
Page 25 of 33

6.

There is currently only one copy of the NFA which is at state q0. When
the sixth and last input symbol (0) arrives, the NFA goes to q1. Once at
q1, it has the option of immediately going to q2 without any new input
symbol arriving. This is caused again by the -transition from state q1 to
state q2. The NFA will consider both possibilities so it creates another
copy of itself. One copy will continue the computation at state q1 while
the other copy will continue at state q2.
The NFA will then be at state q1 and state q2. Since one of these states
is a final state (q1), the string 010010 is accepted.

Take note that this NFA accepts all strings that start and end with a 0. If
an input string starts with a 1, the NFA immediately dies (state q0 has no
outgoing edge for input symbol 1) and the string is rejected.

Nondeterminism
Page 26 of 33

This figure shows the complete flow with an input string of 010010.
input

state
q0 (start)

0
(stopped)

q1

q2

1
q0
0
q1

q2 (stopped)

0
(stopped) q1

q2

1
q0
0
q1

Nondeterministic Finite Automata

q2

*Property of STI
Page 14 of 18

Theory of Computation (With Automata Theory)

Nondeterminism
Page 27 of 33

To summarize the differences between an NFA and a DFA:


1.

NFAs can move to more than one state from any given current
state. They can therefore be in several states at the same time.
DFAs can only be in one state at a time.

2.

In a given state, NFAs may not necessarily have a transition edge


for each symbol in the alphabet. Each state in a DFA must have a
transition edge for each input symbol in the alphabet.

3.

NFAs can move to a state even without any input symbol arriving
(-transitions). This is not possible for DFAs.

[Nondeterminism, Pages 127 of 33]


Nondeterministic Finite
Automata
Page 28 of 33

Nondeterministic Finite Automata


Formal Definition:
The formal definition of a NFA is similar to that of a DFA. Recall that the
definition of a DFA is obtained by simply listing the components of an
automaton, which are the set of states (Q), the alphabet (), the transition
function (), the start state (q0), and the set of final states (F).
The main difference between the two definitions is in the transition function.
The transition function of an NFA must consider the possibility of state
transitions even if there is no input symbol (-transitions).
The transition function must also consider the possibility that the NFA may
go to several states given the same input symbol. It may even not go to
any state at all (the copy dies) given an input symbol because there is no
transition edge for that particular input symbol.

Nondeterministic Finite Automata

*Property of STI
Page 15 of 18

Theory of Computation (With Automata Theory)

Nondeterministic Finite
Automata
Page 29 of 33

For example:
0, 1

q0

q1

q2

The formal definition of this NFA is a 5-tuple N1 = {Q, , , q0, F} where:


1. Q = {q0, q1, q2}
2. = {0, 1}
3. :

4. Start State = q0.


5. F = {q2}
Take note that the transition function has the symbol together with the
input symbols. The should not be taken as an actual input symbol. It is
written there to take into consideration -transitions. In this NFA, there are
no -transitions indicated by the absence of any entry below it.
Blank entries in the transition function indicate no transition will occur when
the NFA is in a particular state and a certain input symbol arrives. For
example, if the NFA is currently in state q0 and the input is equal to 1, the
NFA stops processing because it has nowhere to go. This is because
there is a blank entry at the intersection of state q0 and input symbol 1 in
the table. Mathematically:

(q0, 1) =
Finally, take note of the two entries (q1 and q2) at the intersection of state
q1 and input symbol 1 in the table. This means that if the NFA is in state
q1 and the incoming input is 1, the NFA will go to two states, q1 and q2.
Mathematically:

(q1, 1) = {q1, q2}

Nondeterministic Finite Automata

*Property of STI
Page 16 of 18

Theory of Computation (With Automata Theory)

Designing Deterministic Finite


Automata
Page 30 of 33

Another example:
0

q0

q1

q2

The formal definition of this NFA is a 5-tuple N2 = {Q, , , q0, F} where:


1. Q = {q0, q1, q2}
2. = {0, 1}
3. :

4. Start State = q0
5. F = {q1}
Nondeterministic Finite
Automata
Page 31 of 33

For each of the following NFA examples, have the students formally describe the
NFA by listing down the components of each (set of states, set of symbols, the
transition table, the start state, and the set of final states). Then, let them
experiment with various strings to determine the strings that will be accepted and
rejected by the NFA. Finally, have them come up with a generalized statement on
the strings accepted by the NFA.
It is not advised that the instructor let the students work on all the examples and
then later present to them all the solutions.
Instead, it is highly recommended that the students work on them one at a time.
Meaning, have them work on one example for 5 to 10 minutes. Then present to
them the solution for that example. After that, have them work on the second
example for another 5 to 10 minutes and then present to them the solution. In this
way, the students learn from their mistakes before proceeding to solve the next
exercise.
For NFA N1: This NFA accepts all strings that are multiples of 10 (such as 10,
1010, 101010) including the empty string.
For NFA N2: This NFA accepts all strings that contain the substring 11.

Nondeterministic Finite Automata

*Property of STI
Page 17 of 18

Theory of Computation (With Automata Theory)

Nondeterministic Finite
Automata
Page 32 of 33

For NFA N3: This NFA accepts all strings that are multiples of 010 (such as 010,
010010, 010010010) or strings that are multiples of 101 (such 101, 101101,
101101101), or strings that are multiples of 101 followed by multiples of 010 (such
as 101010, 101101010010). The NFA also accepts the empty string.
For NFA N4: This NFA accepts all strings that contain the substring 11 or 101.

Nondeterministic Finite
Automata
Page 33 of 33

The state diagram for the required DFA:

In an NFA, a state may have zero, one, or many outgoing edges for each input
symbol, and an NFA may or may not have an -transition. DFAs can only have
one outgoing edge for each input symbol and they cannot have -transitions.
By careful analysis of the definitions of both NFA and DFA, a DFA may be
considered as a special type of NFA with the following restrictions:
1.

DFAs can only have one outgoing edge for each input symbol and each
input symbol must have an outgoing edge.

2.

A DFA cannot have -transitions.

It can therefore be said that a DFA is a restricted type of NFA. In fact, all DFAs are
NFAs.
However, not all NFAs are DFAs.
In other words, nondeterminism is a generalization of determinism.

[Nondeterministic Finite Automata, Pages 2833 of 33]

Nondeterministic Finite Automata

*Property of STI
Page 18 of 18