CSC 3130: Formal languages and automata theory
description
Transcript of CSC 3130: Formal languages and automata theory
CSC 3130: Formal languages and automata theory
Andrej Bogdanov
http://www.cse.cuhk.edu.hk/~andrejb/csc3130
The Chinese University of Hong Kong
Nondeterminism
Fall 2010
Example
0
1
…
…
……
q
q
q
q
q
q
q
q
q
q
q
0
1
0
1
0
1
1
1
1
1
0
• Construct a DFA over alphabet {0, 1} that accepts those strings that end in 101
• Sketch of answer:
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 101and accept if we see it
qdie
1
0 13 symbols left
This is not a DFA!
0
1
0
Nondeterminism
• Nondeterminism is the ability to make guesses, which we can later verify
• How a nondeterministic automaton for strings that end in 101 works:
1. Guess if you are approaching end of input2. If guess is yes, look for 101 and accept if you
see it3. 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
• Each state can have zero, one, or more outgoing transitions labeled by the same symbol
1 0 1
0, 1
q q q q
The ability to make choices
1 0 1
0, 1
q q q q
• State q0 has two transitions labeled 1
• Upon reading 1, we have the choice of staying in q0 or moving to q1
The ability to make choices
1 0 1
0, 1
q q q q
• State q1 has no transition labeled 1
• Upon reading 1 in q1, we die; upon reading 0, we continue to q2
The ability to make choices
1 0 1
0, 1
q q q q
• State q3 has no transition going out
• Upon reading 0 or 1 in q3, we die
Meaning of NFA
1 0 1
0, 1
q q q q
Guess if you are 3 symbols away from
end of input
If so, guess you will see the
pattern 101
Check that you are at the end of input
How to run an NFA
1 0 1
0
q q q q
input: 01101
,1
The NFA can have several active statesat the same time
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 0 1
0, 1
q q q q
0, 1
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
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
, 00, 001, 101 are accepted, but 11, 0110 are not
q0
, 1 0
0q1 q2
Example
alphabet = {0, 1}states Q = {q0, q1, q2}initial state q0
accepting states F = {q2}
state
s
inputs
0 1q0
q1
q2
{q1}
table oftransition function
{q0, q1}
q0
, 1 0
0q1 q2
{q1}{q2}
Examples
q0
, 1
0q1 q2
q0
, 1
0q1 q2
1
2
00
0
01
2
3
4
5
q0
, 1
0q1 q2
01
2 3
4
q0
, 1
0q1 q2
01
2
3
54
or
or
Examples
q0
, 1
0q1 q2
15
001
0
q0
, 1
0q1 q2
01
4
q0
, 1
0q1 q2
01
2
3
4
1012
3
11
q0
, 1
0q1 q2
01
2
STOP
STOP
Example of -transitions
• Construct an NFA that accepts all strings with an even number of 0s or an odd number of 1s
r0 r1
1 10
0even number of 0s
s0 s1
0 01
1odd number of 1s
q0
-transitionscan be taken for free (without reading input)