Anda di halaman 1dari 30

Formal Languages

Non Deterministic Finite


Automata
Nondeterminism
• Nondeterminism is the ability to make
guesses, which we can later verify
• Informal nondeterministic algorithm
for strings that end in 101:
1. Guess if you are approaching end of input
2. If guess is yes, look for 101 and accept if you see it
3. If guess is no, read one more symbol and go to step 1
Nondeterministic finite
automaton
• This is a kind of automaton that allows
you to make guesses

0, 1

q0 1 q1 0 q2 1 q3

• Each state can have zero, one, or more


transitions out labeled by the same
symbol
Semantics of guessing
0, 1

q0 1 q1 0 q2 1 q3

• State q0 has two transitions labeled 1


• Upon reading 1, we have the choice
of staying in q0 or moving to q1
Semantics of guessing
0, 1

q0 1 q1 0 q2 1 q3

• State q1 has no transition labeled 1


• Upon reading 1 in q1, we die; upon
reading 0, we continue to q2
Semantics of guessing
0, 1

q0 1 q1 0 q2 1 q3

• State q3 has no transition going out


• Upon reading anything in q3, we die
Example
• Construct an NFA over alphabet {0,
1} that accepts those strings that
contain the pattern 001
Example
• Construct an NFA over alphabet {0, 1}
that accepts those strings that contain
the pattern 001 somewhere

• Answer
0, 1 0, 1

0 0 1 q
q0 q1 q2 q33
Example
• Construct an NFA that accepts all strings with an even
number of 0s or an odd number of 1s

1 1
0
r0 r1 even number of 0s
ε 0

q0
ε 0 0
1 odd number of 1s
s0 s1
1

ε -transitions can be taken for free (without reading input)


Formal definition
• A nondeterministic finite automaton (NFA) is a
5-tuple (Q, Σ , δ , q0, F) where
– Q is a finite set of states
– Σ is an alphabet
– δ : Q × (Σ ∪ {ε }) → subsets of Q is a transition function
– q0 ∈ Q is the initial state
– F ⊆ Q is a set of accepting states (or final states).

• Differences from DFA:


– transition function δ can go into several states
– It allows ε -transitions
Example
ε ,1 0 ε
q0 q1 q2
0

table of
transition function δ :
alphabet Σ = {0, 1}
inputs
states Q = {q0, q1, q2}
0 1 ε
initial state q0
accepting states F = {q2} q0 ∅ {q1} {q1}
states

q1 {q0, q1} ∅ {q2}


q2 ∅ ∅ ∅
Language of an NFA
The language of an NFA is the set of all
strings for which there is some path
that, starting from q0, leads to an
accepting state as the string is read left
to right (and ε -transitions are taken for
free).
• Example
ε ,1 0 ε
q0 q1 q2
0

– ε , 00, 001, 101 are accepted, but 11, 0110


are not
Extended Transition δ*
Function
• δ * ( q0 , a ) = { q1}

q4 q5
a a
q0 a q1 b q2 λ q3
λ 13
δ * ( q0 , aa ) = { q4 , q5 }

q4 q5
a a
q0 a q1 b q2 λ q3
λ 14
δ * ( q0 , ab ) = { q2 , q3 , q0 }

q4 q5
a a
q0 a q1 b q2 λ q3
λ 15
The Language of an NFA
M
•F = { q0 ,q5 } q4 q5
a a
q0 a q1 b q2 λ q3
λ

δ * ( q0 , aa ) = { q4 , q5 } aa ∈ L(M )
∈F 16
F = { q0 ,q5 }
q4 q5
a a
q0 a q1 b q2 λ q3
λ

δ * ( q0 , ab ) = { q2 , q3 , q0 } ab ∈ L( M )
∈F 17
F = { q0 ,q5 }
q4 q5
a a
q0 a q1 b q2 λ q3
λ

δ * ( q0 , aba ) = { q1} aba ∉ L( M )


∉F 18
• q4 q5
a a
q0 a q1 b q2 λ q3
λ

L( M ) = { λ } ∪ { ab} * {aa}
19
Formally
MNFA
• The language accepted by is:

L( M ) = { w1, w2 , w3 ,...}

• whereδ * (q0 , wm ) = {qi , q j ,..., qk ,}

qk
• and there is some ∈F (accepting state)

20
NFAs are as powerful as
DFAs
• Obviously, an NFA can do everything
a DFA can do
• But can it do more?
NFAs are as powerful as

DFAs
Obviously, an NFA can do everything a
DFA can do
• But can it do more?

NO!
• Theorem
If a language L is accepted by some NFA, then
it is also accepted by some DFA.
Equivalence of Machines

• Definition:

• Machine is equivalent to machine


M1 M2
• if
L( M 1 ) = L( M 2 )

23
Example of equivalent
machines M1
NFA 0
L•( M 1) = {10} *
q0 q1
1

FA M2 0,1
0
L( M 2 ) = {10} *
q0 q1 1 q2
1
0 24
NFA to DFA conversion
intuition
0, 1

NFA: q0 1 q1 0 q2

0 0

DFA: q0 1 q0 or q1 0 q0 or q2

1 1
NFA to DFA conversion
intuition
0, 1

NFA: q0 1 q1 0 q2

0 0

DFA: q0 1 {q0, q1} 0 {q0, q2}

1 1
General method
NFA DFA
states q0, q1, …, qn ∅, {q0}, {q1}, {q0,q1}, …, {q0,
…,qn}
one for each subset of states in the
NFA
initial q0 {q0}
state
transitions δ δ ’({qi1,…,qik }, a) =
δ (qi1, a) ∪…∪ δ (qik ,
a)
accepting F ⊆ Q F’ = {S: S contains some
states state in F}
Why the method works
After reading n symbols, the DFA is in
state
{qi1,…,qik } if and only if the NFA is in one
of the states qi1,…,qik
• At the end, the DFA accepts when it is
in a state that contains some
accepting state of NFA

• So the DFA accepts only when the


NFA accepts too
•NFAs are interesting because we can
express languages easier than FAs

FA
M2 a
q2
a
q0 a q1

L( M 2 ) = {a} 29
•NFAs are interesting because we can
express languages easier than FAs

NFA
M1 FA
M2 a
q2
q0 a q1
a
q0 a q1

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

Anda mungkin juga menyukai