Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure;...

  • View
    196

  • Download
    2

  • Category

    Science

Preview:

Citation preview

Theory of Computation

Nondeterminism & Nondeterministic

Finite Automata (NFA), Epsilon Closure,

NFA with & w/o Epsilon Transitions,

NFA vs. DFA, Subset Construction

Vladimir Kulyukin

Department of Computer Science

Utah State University

http://www.vkedco.blogspot.com/

Outline

• Nondeterminism & Nondeterministic

Finite Automata (NFA)

• Epsilon Closure

• Equivalence of NFAs with & without

Epsilon Transitions

• NFAs vs. DFAs

• Subset Construction: Equivalence of NFAs

& DFAs

Nondeterminism

&

Nondeterministic Finite Automata (NFA)

Nondeterminism

• Given an input, there can be more than

one legal sequence of steps to process

the input

• The input is accepted if at least one legal

sequence of moves ends up in an

accepting state

• The input is accepted if a final state is

reached on it

Nondeterministic Example

q0 q1 q2

a,b

a a

a,b

a b

q0 {q0, q1} {q0}

q1 {q2} { }

q2 {q2} {q2}

On the input aa this NFA

can reach q0, q1, and q2

Nondeterministic Example

q0 q1 q2

a,b

a a

a,b

2100

* ,,, qqqaaq

Power Sets

. of size theis whereelements, 2 has general,In

.3,2,1,3,2,3,1,2,1,3,2,1,

Then .3,2,1Let

.,,,,Then .,Let

:Examples

.

is ofset power The set. a be Let

SnSP

SP

S

babaSPbaS

SR|R P(S)

SS

n

NFA: Definition

states. accepting ofset theis

state;start theis

;:

symbols; ofset finite a i.e. alphabet,an is

states; ofset finite a is

where,,,,, tuple-5 a is NFA An

0

0

QF

Qq

QPQ

Q

FqQMM

Instantaneous Descriptions (IDs)

. with ends and with startsthat

relations moreor zero of sequence a is thereif

only and if i.e. IDs,on relation a is

., ifonly and if ,

,or any and stringany For

input. theofpart unread theis and

statecurrent theis where,,pair a is

NFA an for IDn descriptio ousinstantaneAn

**

*

*

JI

JI

aqrr,xaxq

aax

x

Qqxq

NFAs & Their Languages

. then NFA,an is If

.on NFA by the reached becan that

states all ofset the;,,|,

0

**

F,xq|δΣx MLM

x

rxqrxq

**

Epsilon Closure

Spontaneous Transitions

• Unlike DFAs, NFAs are allowed to make

transitions without reading any input

symbols

• When such spontaneous transitions (aka

ε–transitions) occur, the NFA is said to

read ε

• Note: when a spontaneous transition

occurs, the NFA may change a state

without reading any input

Example: Spontaneous Transition from q0

q0

q1

q2

ε

ε

a

b

.**

baML

Epsilon Closures

• When we deal with NFAs with ε-

transitions, we have to keep track of

where we can get to not only on symbols

but also on ε

• ε-closure(q) = {r | there is a path from

q to r consisting of only ε-transitions}

Epsilon Closure Example

q0 q1 q2

0 1

2

0 1 2 ε

q0 {q0} { } { } {q1}

q1 { } {q1} { } {q2}

q2 { } { } {q2} { }

ε ε

Epsilon Closure Example

.,,

closure

closure

0,0,0,closure

0,,,closure

0,,closure0, .4

.,,, .3

.,closure .2

.,,closure .1

210

0

0

210

210

0

*

0

2100

*

211

2100

qqq

q

q

qqq

qqq

qq

qqqq

qqq

qqqq

Equivalence of NFAs with ε-transitions

and NFAs w/o ε-transitions

• Theorem: If L is accepted by an NFA with

ε-transitions, L is accepted by an NFA

without ε-transitions.

• Proof: Use ε-closure to get rid of ε-

transitions.

Epsilon Closure Example

q0 q1 q2

0 1

2

0 1 2 ε

q0 {q0} { } { } {q1}

q1 { } {q1} { } {q2}

q2 { } { } {q2} { }

ε ε

http://www.vkedco.blogspot.com/

Epsilon Closure Example

.,, are states final The

.0,closure2,

.1,closure1,

.0,closure0,

.2,closure2,

.,1,closure1,

.0,closure0,

.2,closure2,

.,1,closure1,

.,,0,closure0,

. from obtain We

s.transition that w/ofunction ion transitnew

thedenote Let s.transition of ridget We

210

222

'

22

'

22

'

211

'

2111

'

11

'

200

'

2100

'

21000

'

'

'

qqq

qqq

qq

qq

qqq

qqqq

qq

qqq

qqqq

qqqqq

Epsilon Closure Example

q0 q1 q2 0,1 1,2

0,1,2

0 1 2

Practical Implication

If an NFA has ε-transitions, we can always

get rid of them before constructing an

equivalent DFA. So we can, without loss

of generality, assume that NFAs do not

contain ε-transitions.

NFA vs. DFA

Equivalence of NFAs and DFAs

• Basic insight: A DFA can keep track of the

states that the equivalent NFA may be in after

reading each symbol of the input

• Since the NFA may be in more than one state

after reading a symbol, each state of the DFA

must correspond to a subset of the NFA’s

states

• The construction of an equivalent DFA from an

NFA is called subset construction

Subset Construction Example

NFA

q0 q1 q2

0,1

1 0,1

DFA? a NFA to isconvert th wedo How

.1 is of symbollast next to the|1,0*

xxML

State q0; Input 0

If the NFA is in q0 and reads 0, it returns back to

q0. So, on 0, the DFA moves from {q0} back to

{q0}:

{q0} 0

State q0; Input 1

If the NFA is in q0 and reads 1, it goes to q0 or

q1, i.e., {q0, q1}. So, on 1, the DFA moves from

{q0} to {q0, q1}:

{q0} 0 {q0, q1} 1

State {q0, q1}; Input 0

Suppose that the NFA is in one of {q0, q1} and reads 0. If it is in q0, it goes to q0. If it is in q1, it goes to q2. So, on 0, the DFA goes from {q0, q1} to {q0, q2}:

{q0} 0 {q0, q1} 1

{q0, q2} 0

http://www.vkedco.blogspot.com/

State {q0, q1}; Input 1

Suppose that the NFA is in one of {q0, q1} and reads 1. If it is in q0, it goes to q0 or q1, i.e., {q0, q1}. If it is in q1, it goes to q2. So, on 1, the DFA goes from {q0, q1} to {q0, q1, q2}:

{q0} 0 {q0, q1} 1

{q0, q2} 0 {q0, q1, q2}

1

State {q0, q2}; Input 0

Suppose that the NFA is in one of {q0, q2} and reads 0. If it is in q0, it goes to q0. If it is in q2, it goes nowhere. So, on 0, the DFA from {q0, q2} goes to {q0}:

{q0} 0 {q0, q1} 1

{q0, q2} 0 {q0, q1, q2}

1 0

State {q0, q2}; Input 1

Suppose that the NFA is in one of {q0, q2} and reads 1. If it is in q0, it goes to q0 or q1, i.e., {q0, q1}. If it is in q2, it goes nowhere. So, on 1, the DFA goes from {q0, q2} to {q0, q1}:

{q0} 0 {q0, q1} 1

{q0, q2} 0 {q0, q1, q2}

1 0

1

State {q0, q1, q2}; Input 0

Suppose that the NFA is in one of {q0, q1, q2} and reads 0. If it is in q0, it will go to q0. If it is in q1, it will go to q2. If it is in q2, it will go nowhere. So, on 0, the DFA goes from {q0, q1, q2} to {q0, q2}:

{q0} 0 {q0, q1} 1

{q0, q2}

0

{q0, q1, q2}

1 0

1

0

State {q0, q1, q2}; Input 1 Suppose that the NFA is in one of {q0, q1, q2} and reads 1. If it is in q0, it will go to q0 or q1, i.e., {q0, q1}. If it is in q1, it goes to q2. If it is in q2, it goes nowhere. So, on 1, the DFA goes from {q0, q1, q2} back to {q0, q1, q2}:

{q0} 0 {q0, q1} 1

{q0, q2}

0

{q0, q1, q2}

1 0

1

0 1

DFA’s Final States

What are the final states in the new DFA? Any subset that has a non-empty intersection with the NFA’s set of final states. So, the DFA has two final states: {q0, q2} and {q0, q1, q2}.

{q0} 0 {q0, q1} 1

{q0, q2}

0

{q0, q1, q2}

1 0

1

0 1

Subset Construction Theorem

. allfor ,,, on,constructiBy

.| .4

; .3

;,,,, .2

; 1.

where,,,,DFA new a

Construct .,,,,Let :Proof

.such that DFA a is Then there

ansitions.epsilon tr NFA w/o a be Let :Theorem

***

*

xxqxq

FRQRF

qq

aQRaraR

QPQ

FqQD

FqQN

DLNLD

N

NNDD

NDD

ND

D

Rr

ND

ND

DDDD

NNNN

NFA vs. DFA: Relative Advantages

& Disadvantages

• NFAs are simpler to write, because, in

general, they have fewer states and allow

for spontaneous transitions with epsilon-

moves

• However, they are not more powerful

than DFAs, i.e. they accept the same

regular languages as DFAs

• For every NFA, one can construct a DFA

that accepts the same language

References

• A. Brooks Weber. Formal Language: A

Practical Introduction. Franklin, Beedle &

Associates.

• M. Davis, R. Sigal, E. Weyuker.

Computability, Complexity, & Languages:

Fundamentals of Theorectical Computer

Science. Academic Press.

Recommended