CSC 3130: Automata theory and formal languages

19
CSC 3130: Automata theory and formal languages Andrej Bogdanov http://www.cse.cuhk.edu.hk/ ~andrejb/csc3130 The Chinese University of Hong Kong Nondeterminism Fall 2008

description

Fall 2008. The Chinese University of Hong Kong. CSC 3130: Automata theory and formal languages. Nondeterminism. Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130. Example from last time. Construct a DFA over alphabet {0, 1} that accepts those strings that end in 101 - PowerPoint PPT Presentation

Transcript of CSC 3130: Automata theory and formal languages

Page 1: CSC 3130: Automata theory and formal languages

CSC 3130: Automata theory and formal languages

Andrej Bogdanov

http://www.cse.cuhk.edu.hk/~andrejb/csc3130

The Chinese University of Hong Kong

Nondeterminism

Fall 2008

Page 2: CSC 3130: Automata theory and formal languages

Example from last time

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:

Page 3: CSC 3130: Automata theory and formal languages

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!

Page 4: CSC 3130: Automata theory and formal languages

Nondeterminism

• Nondeterminism is the ability to make guesses, which we can later verify

• Informal nondeterministic algorithm for language of strings that end in 101:

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

Page 5: CSC 3130: Automata theory and formal languages

Nondeterministic 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

1 0 1

0, 1

q q q q

Page 6: CSC 3130: Automata theory and formal languages

Semantics of guessing

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

Page 7: CSC 3130: Automata theory and formal languages

Semantics of guessing

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

Page 8: CSC 3130: Automata theory and formal languages

Semantics of guessing

1 0 1

0, 1

q q q q

• State q3 has no transition going out

• Upon reading anything in q3, we die

Page 9: CSC 3130: Automata theory and formal languages

Meaning of automaton

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

Page 10: CSC 3130: Automata theory and formal languages

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).

• Only difference from DFA is that output of is a set of states

Page 11: CSC 3130: Automata theory and formal languages

Example

1 0 1

0, 1

q q q q

alphabet = {0, 1}start state Q = {q0, q1, q2, q3}initial state q0

accepting states F = {q3}st

ate

s

inputs

0 1q0

q1

q2

{q0, q1}

transition function

q3

{q0}{q2}

{q3}

Page 12: CSC 3130: Automata theory and formal languages

Language of an NFA

The language of an NFA is the set of all strings forwhich there is some path that, starting from the initial state, leads to an accepting state as the string is read left to right.

1 0 1

0, 1

q q q q

• Example

– 1101 is accepted, but 0110 is not

Page 13: CSC 3130: Automata theory and formal languages

NFAs are as powerful as DFAs

• Obviously, an NFA can do everything a DFA can do

• But can it do more?

Page 14: CSC 3130: Automata theory and formal languages

NFAs are as powerful as DFAs

• Obviously, an NFA can do everything a DFA can do

• But can it do more?

• Theorem

A language L is accepted by some DFA if andonly if it is accepted by some NFA.

NO!

Page 15: CSC 3130: Automata theory and formal languages

Proof of theorem

• To prove the theorem, we have to show that for every NFA there is a DFA that accepts the same language

• We will give a general method for simulating any NFA by a DFA

• Let’s do an example first

Page 16: CSC 3130: Automata theory and formal languages

Simulation example

1 0

0, 1

q q qNFA:

DFA: 1q q0 or q1

1

q0 or q2

1

0 0

0

Page 17: CSC 3130: Automata theory and formal languages

General method

NFA DFAstates q0, q1, …,

qn

q0}, {q1}, {q0,q1}, …, {q0,…,qn}

one for each subset of states in the NFA

initial state

q0 q0}

transitions

’({qi1,…,qik}, a) =

(qi1, a) ∪…∪ (qik, a)

accepting states

F Q F’ = {S: S contains some state in F}

Page 18: CSC 3130: Automata theory and formal languages

Proof of correctness

• Lemma

• Proof by induction on n

• At the end, the DFA accepts iff it is in a state that contains some accepting state of NFA

• By lemma, this is true iff the NFA can reach an accepting state

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

Page 19: CSC 3130: Automata theory and formal languages

Exercises

• Construct NFAs for the following languages over the alphabet {a, b, …, z}:– All strings that contain eat or sea or easy– All strings that contain both sea and tea– All strings that do not contain fool