You are on page 1of 22

THEORY OF AUTOMATA

Lecture#04
Non-deterministic Finite Automata, NFA & FA Equivalence, NFA
& Kleene’s Theorem
Lecture Contents
 Deterministic Finite Automata (DFA)
 Non-deterministic Finite Automata (NFA)
 Building NFA (Exercises)
 DFA vs. NFA
 Equivalence of DFA & NFA
Deterministic Finite Automata
Finite Automata (FA) is also known as Deterministic Finite Automata (DFA)
because its state is always deterministic
For an FA, on a particular input we exactly only in which state that FA is (it is
deterministic)
Non-deterministic Finite Automata
A Non-deterministic Finite Automaton (NFA) does the same job as a DFA but it is
“non-deterministic” by nature
NFA does the same job as DFA i.e. NFA for a particular language L accepts all the
words belonging to L but in non-deterministic manner
Non-determinism implies that NFA can be in more than one state at the same
time
There can be more than multiple one transitions for input of a letter from a
particular state
NFA can choose to show no transition for input of a letter from some states i.e.
such transitions show that on that letter’s input machine will crash rejecting the
input
DFA vs. NFA

DFA NFA

1
1,0
1
1,0
0,1

0
Non-deterministic Finite Automata Definition
A Non-deterministic Finite Automaton (NFA) consists of:
Q (a finite set of states with one start state and zero or more accept states)
∑ (an alphabet)
q0 (start state)
F (set of accept states)
δ (a transition function that shows moving from existing states to new states for
some input letter)

NFA is also defined as 5-tuple {Q, ∑ , q0, F, δ }


NFA: How to build?
1. Building a NFA has same process as building a DFA except that NFA allows
multiple transitions for a letter from any state (so control can be at multiple
places for any input)
2. NFA even allows no transition for a letter from any state
3. If NFA shows no transition of a letter from all current states, machine crashes
rejecting the word
Accepting Words in NFA
How can I know that NFA accepts a word w?
1. Start with initial state of NFA
2. For a letter move to all states transitions lead you to
3. Repeat step 2 until all letters in w end
4. If at least one of current states is an accept state, w is accepted
5. Otherwise, w is rejected
NFA … Example
Create NFA for “All words ending on letter b” over Σ = {a, b}.
NFA … Example
Create NFA for “All words ending on letter b” over Σ = {a, b}.
For given NFA show all the
transitions on inputs i. aab ii. baa
Build NFA Exercises
Build NFAs for following languages over Σ = {a, b}.
Language of all words starting and ending with different letters
Language of all words containing substring abb
Language of all words containing substring abb or baa
Language of all words not containing substring abb
EVEN-EVEN / ODD-ODD / EVEN-ODD / ODD-EVEN
Language of all words with count of ‘a’ as multiple of three
Language of all words with count of ‘a’ as not multiple of three
Language of all words with a as 2nd letter
DFA vs NFA
DFA NFA
For every letter of alphabet there is exactly one There may be zero, one or more transitions for a letter
transition from a state of alphabet from a state
DFA does not allow null-string transitions NFA allows null-string transitions
For some input, DFA guarantees control at one place NFA allows control to be at multiple places at a time
at a time
A word is accepted if input ends on accept state NFA accepts the word if word ends on at least one
accept state out of all current states
A word is rejected if input ends on reject state NFA rejects the word if input ends when all current
states are either non-accept or NFA has crashed
Not all NFAs are DFAs All DFAs are NFAs
Easier to run input on Harder to run input on
Single execution machine model Parallel execution machine model
DFA and NFA Equivalence … Proof
Theorem:
A language L is accepted by a DFA if and only if it is accepted by an NFA
Proof:
We have to prove that for every DFA there is an equivalent NFA and vise versa

Part 1: There is an NFA for every DFA


We don’t need to prove it as every DFA is a special type of NFA in which there is
exactly one transition of every letter of alphabet for every state (the definition of
NFA)
Part 2: There is a DFA for every NFA
To prove it, we have to understand that every transition in NFA (say N) can map to
all or some states of i.e. every transition maps to a subset of states in N
DFA and NFA Equivalence … Proof continued
Theorem:
A language L is accepted by a DFA if and only if it is accepted by an NFA
Proof continued:
Suppose that there is a NFA N with S states. Let’s construct the DFA D equivalent to N.
Number of states in D is equal to 2S (i.e. no. of subsets of S in N).
Construct the DFA from N as per following rules:
Construct a single initial state in D that is equivalent to starting state in N
Every transition in N for some letter of Σ (it can be a single path transition or a
multiple path transition) is equivalent to a single transition in D
If some state in N has no transition for some letter of Σ, there is a special transition in D
showing the transition towards a non-accepting state
A transition in N that leads to its one or more accepting state, will be always equivalent
to a transition in D towards an accepting state
DFA and NFA Equivalence … Proof continued
Theorem:
A language L is accepted by a DFA if and only if it is accepted by an NFA
Proof continued:
At end, discard all the states in D that are not reachable through the process mentioned
in above steps
So, we have constructed a DFA (D) equivalent to N.
DFA and NFA Equivalence … Example
Create a DFA equivalent to given NFA:

Solution:
Above NFA accepts all words ending on ‘bb’ over Σ = {a, b}.
We first create a table of in given NFA and its equivalent DFA
DFA and NFA Equivalence … Example
Table:
State in given NFA Corresponding state in
DFA
{q0} Q0
{q1} Q1
{q2} Q2
{q0, q1} Q3
{q0, q2} Q4
{q1, q2} Q5
{q0, q1, q2} Q6
Ø Q7
DFA and NFA Equivalence … Example
Transition Table:
Current State in DFA New State for New State for
input = a input = b
Q0={q0} //(initial state) Q0={q0} Q3={q0, q1}
Q1={q1} Q7=Ø Q2={q2}
Q2={q2} // Accept State Q7=Ø Q7=Ø
Q3={q0, q1} Q0={q0} Q6={q0, q1, q2}
Q4={q0, q2} // Accept State Q0={q0} Q3={q0, q1}
Q5={q1, q2} // Accept State Q7=Ø Q2={q2}
Q6={q0, q1, q2} //Accept State Q0={q0} Q6={q0, q1, q2}
Q7=Ø Q7=Ø Q7=Ø
DFA and NFA Equivalence … Example
Equivalent DFA as per transition table:
DFA and NFA Equivalence … Example
Removing states not reachable from Q0 we get:
21

NFA Applications
Text indexing
inverted indexing
For each unique word in the database, store all locations that contain it
using an NFA or a DFA
Find pattern P in text T
Example: Google querying
Extensions of this idea:
PATRICIA tree, suffix tree
NFA & Kleene’s Theorem
NFA corresponding to union of two FAs
NFA corresponding to complement of an FA
NFA corresponding to concatenation of two FAs
NFA corresponding to intersection of two FAs
NFA corresponding to Kleene’s Star of an FA

You might also like