0% found this document useful (0 votes)
3 views84 pages

2 NFA

The document provides an overview of Non-Deterministic Finite Automata (NFA), explaining its structure, acceptance criteria, and rejection criteria. It illustrates how NFAs can accept or reject input strings based on their transition diagrams and includes examples of accepted and rejected strings. Additionally, it highlights the differences between NFAs and Deterministic Finite Automata (DFA) and discusses the concept of lambda transitions.

Uploaded by

Alena Croft
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views84 pages

2 NFA

The document provides an overview of Non-Deterministic Finite Automata (NFA), explaining its structure, acceptance criteria, and rejection criteria. It illustrates how NFAs can accept or reject input strings based on their transition diagrams and includes examples of accepted and rejected strings. Additionally, it highlights the differences between NFAs and Deterministic Finite Automata (DFA) and discusses the concept of lambda transitions.

Uploaded by

Alena Croft
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

COMP 335: Introduction to

Theoretical Computer Science

Non Deterministic
Finite Automata (NFA)

The content on this video was originally provided by Dr. Shiri,


and modified by Drs. Pankratov and Narayanan.
Previous video
• Deterministic Finite Automata (DFA) 𝑀 = (𝑄, Σ, 𝛿, 𝑞) , 𝐹)

• L(M), the language accepted by a DFA M

• Regular languages
for a language to be accepted, accept all the possible accepts, reject the rest

Non-Deterministic Finite Automata (NFA/FA)

Alphabet = {𝑎}

q1 a q2
a
q0
a
q3
here you try all of them, as long as 1 branch accepts, it accepts.
Non deterministic Finite Accepter (NFA)

Alphabet = {𝑎}

Two choices q1 a q2
a
q0
a
q3
Non deterministic Finite Automata (NFA)

Alphabet = {𝑎}

Two choices q1 a q2 No transition


a
q0
a
q3 No transition
First Choice
a a

q1 a q2
a
q0
a
q3
First Choice

a a

q1 a q2
a
q0
a
q3
First Choice

a a

q1 a q2
a
q0
a
q3
First Choice
Input finished

a a

q1 a q2 “accept”
a
q0 for 2 things:
a 1) it consumed the entire input
2) it is in the accepted state

q3
Second Choice

a a

q1 a q2
a
q0
a
q3
Second Choice

a a

q1 a q2
a
q0
a
q3
Second Choice

a a

q1 a q2
a
q0
a
No transition:
q3
the automaton “hangs”
or it crashes
Second Choice

a a

q1 a q2
a
q0 Input cannot be consumed
a
q3 “reject”
Acceptance Criteria
An NFA M accepts an input string w if there is some way for M to
process the entire input string w and reach a final state.

Equivalently, there is a walk in the transition diagram of M from 𝑞) to a


final state labelled with the string w.
Example
𝑎𝑎 is accepted by this NFA:
“accept”
q1 a q2
a q1 a q2
a
q0
a
q0
q3 a
q3 “reject”

because there is a computation that accepts the string 𝑎𝑎.


Rejection example

a
q1 a q2
a
q0
a
q3
First Choice

q1 a q2
a
q0
a
q3
First Choice

a “reject”
q1 a q2
a
q0
a
q3
Second Choice

a
q1 a q2
a
q0
a
q3
Second Choice

a
q1 a q2
a
q0
a
q3
Second Choice

a
q1 a q2
a
q0
a
q3 “reject”
basically if every possible computations rejects, then it is
rejected, also if more than what can be accepted, reject.
Rejection Criteria Unlike Accept criteria if there exists a computation, that
can reaches the final state, then it is accepted.

An NFA M rejects an input string w if there is no computation of 𝑀 that


accepts 𝑤. In other words, every possible computation

EITHER
(1) consumes the entire input and ends up in a non-accepting state
OR
(2) cannot consume the entire input (gets stuck midway/hangs)
Example

The input 𝑎 is rejected by the NFA since:


“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 “reject” q3

all possible computations lead to rejection


Rejection example

a a a
q1 a q2
a
q0
a
q3
First Choice

a a a
q1 a q2
a
q0
a
q3
First Choice

a a a
q1 a q2
a
q0 No transition:
a
the automaton hangs
q3 even if it hangs on final state, it is rejected
First Choice

a a a
q1 a q2 “reject”
a
q0
a Input cannot be consumed
q3
Second Choice

a a a
q1 a q2
a
q0
a
q3
Second Choice

a a a
q1 a q2
a
q0
a
q3
Second Choice

a a a
q1 a q2
a
q0
a
No transition:
q3
the automaton hangs
Second Choice

a a a
q1 a q2
a
q0 Input cannot be consumed
a
q3 “reject”
𝑎𝑎𝑎 is rejected by the NFA:

“reject”
q1 a q2 q1 a q2
a a
q0
a
q0
a
q3 q3 “reject”

All possible computations lead to rejection


The language accepted by the NFA below is 𝐿 = {𝑎𝑎}

q1 a q2
a
q0
a
q3
Another Feature of NFA: l - Transitions

there are actually 2 branches.


1) q0 , q1 , q3
2) q0 , q1 , q2 , q3

q0 a q1 l q2 a q3
a a

q0 a q1 l q2 a q3
a a

q0 a q1 l q2 a q3
(tape head does not move)

a a

q0 a q1 l q2 a q3
a a

q0 a q1 l q2 a q3
Input finished

a a

“accept”

q0 a q1 l q2 a q3

String 𝑎𝑎 is accepted
Rejection Example

a a a

q0 a q1 l q2 a q3
a a a

q0 a q1 l q2 a q3
(tape head doesn’t move)

a a a

q0 a q1 l q2 a q3
a a a

q0 a q1 l q2 a q3

No transition:
the automaton hangs
Input cannot be consumed

a a a
“reject”

q0 a q1 l q2 a q3

String 𝑎𝑎𝑎 is rejected


Language accepted: 𝐿 = {𝑎𝑎}

q0 a q1 l q2 a q3
Another NFA Example

q0 a q1 b q2 l q3

l
a b

q0 a q1 b q2 l q3

l
a b

q0 a q1 b q2 l q3

l
a b

q0 a q1 b q2 l q3

l
a b
“accept”

q0 a q1 b q2 l q3

l
Another String

a b a b

q0 a q1 b q2 l q3

l
a b a b

q0 a q1 b q2 l q3

l
a b a b

q0 a q1 b q2 l q3

l
a b a b

q0 a q1 b q2 l q3

l
a b a b

q0 a q1 b q2 l q3

l
a b a b

q0 a q1 b q2 l q3

l
a b a b

q0 a q1 b q2 l q3

l
a b a b
“accept”

q0 a q1 b q2 l q3

l
Language accepted
6
𝐿 = 𝑎𝑏, 𝑎𝑏𝑎𝑏, 𝑎𝑏𝑎𝑏𝑎𝑏, … = 𝑎𝑏

q0 a q1 b q2 l q3

l
Remark: The symbol 𝜆 does not appear on the input tape explicitly.
Notice that an NFA for a language is often easier and has
fewer states than a DFA for the same language.

NFA 𝑀8 DFA 𝑀9 a
q2
q0 a q1
a
q0 a q1

L( M1) = {a} L( M 2 ) = {a}


Some more examples. Try designing DFAs for
the same languages over the alphabet {a, b}.

M1 M2
q0 q0

L(M1) = {} L(M 2 ) = {λ}


Formally, an NFA 𝑀 = (𝑄, Σ, 𝛿, 𝑞) , 𝐹)

𝑄: set of states, e.g., {𝑞) , 𝑞8 , 𝑞9 }

Σ : input alphabet, e.g., {𝑎, 𝑏} this encodes possiblity of lambda transition

𝛿 : transition function 𝛿: 𝑄×(Σ ∪ {𝜆}) → 2C this is a total function

𝑞) : initial state (must be a member of 𝑄)

𝐹 : set of final/accepting state(s), i.e., subset of 𝑄


C
Transition Function 𝛿: 𝑄×(Σ ∪ {𝜆}) → 2
Recall:
for DFA:
d (q0 , 1) = {q1} power set of Q.
delta : Q x E --> Q and the output is a
set

0
q0 q1 0, 1 q
2
1
l
d (q1,0) = {q0 , q2}

0
q0 q1 0, 1 q
2
1
l
d (q0 , l ) = {q0 , q2}

0
q0 q1 0, 1 q
2
1
l
d (q2 ,1) = Æ

0
q0 q1 0, 1 q
2
1
l
∗ ∗ C
Extended Transition Func. 𝛿 : 𝑄×Σ → 2
d * (q0 , a ) = {q1}

q4 q5
a a
q0 a q1 b q2 l q3
l
d * (q0 , aa ) = {q4 , q5}

q4 q5
a a
q0 a q1 b q2 l q3
l
d * (q0 , ab ) = {q2 , q3 , q0 }

q4 q5
a a
q0 a q1 b q2 l q3
l
Main Slide of Extended transition function

Formally
𝑞E ∈ 𝛿 ∗ 𝑞G , 𝑤 : there is a walk from 𝑞G to
𝑞E with label 𝑤

qi w qj

w = s1s 2 !s k
s1 s2 sk
qi qj
The Language of an NFA 𝑀
q4 q5
a a
q0 a q1 b q2 l q3

F = {q0 ,q5} l

d * (q0 , aa ) = {q4 , q5} aa Î L(M )


ÎF
F = {q0 ,q5}
q4 q5
a a
q0 a q1 b q2 l q3
l

d * (q0 , ab ) = {q2 , q3 , q0 } ab Î L(M )


ÎF
F = {q0 ,q5}
q4 q5
a a
q0 a q1 b q2 l q3
l

d * (q0 , abaa ) = {q4 , q5} abaa Î L(M )


ÎF
F = {q0 ,q5}
q4 q5
a a
q0 a q1 b q2 l q3
l

d * (q0 , aba ) = {q1} aba Ï L(M )


ÏF
q4 q5
a a
q0 a q1 b q2 l q3
l
{(ab)^i : i>= 0} U {(ab)^i aa : i>= 0} = {(ab)^i : i >= 0} * { labda , aa}
any numbers of ab
L(M ) = {ab}* È {ab}*{aa}
any numbers of ab followed by aa
Formally
The language accepted by NFA 𝑀 is:

𝐿 𝑀 = {𝑤 ∶ 𝛿 ∗ 𝑞) , 𝑤 ∩ 𝐹 ≠ ∅}

In words: possible to reach a final state starting at 𝑞) by following a


walk labelled 𝑤.
wÎ L(M ) d * (q0 , w)
qi
w

q0 qk qk Î F
w
w qj
What is L(M) for the NFA M below?

0
q0 q1 0, 1 q2
1
l accepted language: {lambda(10)^n) : n>=0}
even though lambda is here, it is not mendatory to take it.
Language accepted by M


𝐿 𝑀 = 𝜆, 10,1010,101010, … = 10
0
q0 q1 0, 1 q2
1
(redundant state)
l
Why study non-determinism?
• One way to think about a non-deterministic automaton is that 𝑀
“guesses” the right moves to take towards 𝐹.
• Alternatively, M makes all possible moves in parallel.

• Easier to design NFAs in many cases – NFAs can be a lot smaller than
DFAs for the same language.

• Easier to prove properties of languages accepted by NFAs than DFAs.


Design an NFA M on the alphabet {a, b} that
accepts all strings ending with ab:
Design an NFA M such that:
L(M) = 𝑤 ∶ 𝑤 contains the substring 001
Review questions
• How does a Non-Deterministic Finite Automaton (NFA) differ from a
DFA?

• When do we say a string w is accepted by an NFA M?

• What is the language accepted by a given NFA M?


You should now be able to…
• Given an NFA M, and a string w:
say whether M accepts w , or if M rejects w.

• Given an NFA M, describe L(M).

• Given a regular language L, design an NFA that accepts L.

You might also like