Anda di halaman 1dari 19

Nondeterminism

Example from last time


Construct a DFA over alphabet {0, 1} that
accepts those strings that end in 101
Sketch of answer:

q
1

1
0

q
q

Would be easier if
Suppose we could guess when the string we are
reading has only 3 symbols left
Then we could simply look for the sequence 101
and accept if we see it

3 symbols left

qdie

This is not a
DFA!

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
q

Each state can have zero, one, or more


transitions out labeled by the same symbol

Semantics of guessing
0, 1
q

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
q

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
q

State q3 has no transition going out


Upon reading anything in q3, we die

Meaning of automaton
Guess if you are 3
symbols away from
end of input

0, 1
q

If so, guess you


will see the
pattern 101

Check that you are


at the end of input

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
q

0, 1
0

Example
Construct an NFA that accepts all strings with an
even number of 0s or an odd number of 1s
1

q0

r0

1
0

0
s0

r1

even number of 0s

0
1
1

s1

odd number of 1s

-transitions can be taken for free (without reading in

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
q0

, 1
0

q1

states

alphabet = {0, 1}
states Q = {q0, q1, q2}
initial state q0
q0
accepting states F = {q2} q1
q2

q2

table of
transition function
inputs

1
{q1} {q1}

{q2}
{q0, q1}

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
q0

, 1
0

q1

q2

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


not

Examples

q0

, 1

q1

q2

0
2

00

q0

, 1

q1

q2

q2

q2

0
2

or

q0

, 1

or

, 1
2

q1

q0

q1
4

Examples
2

001

q0

, 14

101

11

q0

q0

q1
0

, 1

q2

q2

q1
0

q0

, 1

q1

, 1

q2

STOP

0
1

q1
0

q2
STOP

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, the


it is also accepted by some DFA.

Anda mungkin juga menyukai