18
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

CSC 3130: Formal languages and automata theory

  • Upload
    eilis

  • View
    53

  • Download
    5

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: 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

Page 2: CSC 3130: Formal languages and automata theory

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:

Page 3: CSC 3130: Formal languages and automata theory

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

Page 4: CSC 3130: Formal languages and automata theory

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

Page 5: CSC 3130: Formal languages and automata theory

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

Page 6: CSC 3130: Formal languages and automata theory

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

Page 7: CSC 3130: Formal languages and automata theory

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

Page 8: CSC 3130: Formal languages and automata theory

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

Page 9: CSC 3130: Formal languages and automata theory

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

Page 10: CSC 3130: Formal languages and automata theory

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

Page 11: CSC 3130: Formal languages and automata theory

Example

• Construct an NFA over alphabet {0, 1} that accepts those strings that contain the pattern 001

Page 12: CSC 3130: Formal languages and automata theory

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

Page 13: CSC 3130: Formal languages and automata theory

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

Page 14: CSC 3130: Formal languages and automata theory

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

Page 15: CSC 3130: Formal languages and automata theory

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}

Page 16: CSC 3130: Formal languages and automata theory

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

Page 17: CSC 3130: Formal languages and automata theory

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

Page 18: CSC 3130: Formal languages and automata theory

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)