Title: CSC 3130: Automata theory and formal languages
1Fall 2009
The Chinese University of Hong Kong
CSC 3130 Automata theory and formal languages
Nondeterminism
Andrej Bogdanov http//www.cse.cuhk.edu.hk/andrej
b/csc3130
2Example from last time
- Construct a DFA over alphabet 0, 1 that accepts
those strings that end in 101 - Sketch of answer
0
0
q000
1
0
q00
1
q001
q0
1
0
q01
qe
1
q101
q10
0
1
q1
1
q11
1
q111
1
3Would 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
101and accept if we see it
1
0
1
3 symbols left
qdie
This is not a DFA!
4Nondeterminism
- Nondeterminism is the ability to make guesses,
which we can later verify - Informal nondeterministic algorithm for strings
that end in 101
- Guess if you are approaching end of input
- If guess is yes, look for 101 and accept if you
see it - If guess is no, read one more symbol and go to
step 1
5Nondeterministic finite automaton
- This is a kind of automaton that allows you to
make guesses - Each state can have zero, one, or more
transitions out labeled by the same symbol
0, 1
1
0
1
q0
q1
q2
q3
6Semantics of guessing
0, 1
1
0
1
q0
q1
q2
q3
- State q0 has two transitions labeled 1
- Upon reading 1, we have the choice of staying in
q0 or moving to q1
7Semantics of guessing
0, 1
1
0
1
q0
q1
q2
q3
- State q1 has no transition labeled 1
- Upon reading 1 in q1, we die upon reading 0, we
continue to q2
8Semantics of guessing
0, 1
1
0
1
q0
q1
q2
q3
- State q3 has no transition going out
- Upon reading anything in q3, we die
9Meaning of automaton
Guess if you are 3 symbols away from end of input
0, 1
1
0
1
q0
q1
q2
q3
If so, guess you will see the pattern 101
Check that you are at the end of input
10Example
- Construct an NFA over alphabet 0, 1 that
accepts those strings that contain the pattern 001
11Example
- 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
q0
q1
q2
q3
12Example
- Construct an NFA that accepts all strings with an
even number of 0s or an odd number of 1s
can be taken for free (without reading input)
13Formal definition
- A nondeterministic finite automaton (NFA) is a
5-tuple (Q, S, d, q0, F) where - Q is a finite set of states
- S is an alphabet
- d Q (S ? e) ? 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 d can go into several states
- It allows e-transitions
14Example
0
?, 1
?
q0
q1
q2
0
table of transition function d
alphabet S 0, 1 states Q q0, q1,
q2 initial state q0 accepting states F q2
inputs
0
1
?
q0
q1
Æ
q1
q1
q2
q0, q1
Æ
states
q2
Æ
Æ
Æ
15Language 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 e-transitions are taken
for free).
- Example
- e, 00, 001, 101 are accepted, but 11, 0110 are not
0
?, 1
?
q0
q1
q2
0
16Examples
0
?, 1
1
?
e
2
q0
q1
q2
0
2
3
0
?, 1
1
?
00
4
q0
q1
q2
0
2
0
?, 1
1
4
?
or
5
q0
q1
q2
0
3
0
?, 1
1
3
?
or
5
q0
q1
q2
0
2
4
17Examples
2
0
? , 1
4
1
?
5
001
q0
q1
q2
0
3
0
?, 1
1
3
?
4
q0
q1
q2
101
0
2
0
?, 1
1
?
q0
q1
q2
11
0
STOP
0
?, 1
1
?
2
q0
q1
q2
0
STOP
18NFAs are as powerful as DFAs
- Obviously, an NFA can do everything a DFA can do
- But can it do more?
19NFAs are as powerful as DFAs
- Obviously, an NFA can do everything a DFA can do
- But can it do more?
- Theorem
NO!
If a language L is accepted by some NFA, thenit
is also accepted by some DFA.