37
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/

Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

Embed Size (px)

Citation preview

Page 1: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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/

Page 2: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

Outline

• Nondeterminism & Nondeterministic

Finite Automata (NFA)

• Epsilon Closure

• Equivalence of NFAs with & without

Epsilon Transitions

• NFAs vs. DFAs

• Subset Construction: Equivalence of NFAs

& DFAs

Page 3: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

Nondeterminism

&

Nondeterministic Finite Automata (NFA)

Page 4: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 5: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 6: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

Nondeterministic Example

q0 q1 q2

a,b

a a

a,b

2100

* ,,, qqqaaq

Page 7: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 8: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 9: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 10: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

**

Page 11: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

Epsilon Closure

Page 12: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 13: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

Example: Spontaneous Transition from q0

q0

q1

q2

ε

ε

a

b

.**

baML

Page 14: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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}

Page 15: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

Epsilon Closure Example

q0 q1 q2

0 1

2

0 1 2 ε

q0 {q0} { } { } {q1}

q1 { } {q1} { } {q2}

q2 { } { } {q2} { }

ε ε

Page 16: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 17: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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.

Page 18: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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/

Page 19: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 20: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

Epsilon Closure Example

q0 q1 q2 0,1 1,2

0,1,2

0 1 2

Page 21: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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.

Page 22: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

NFA vs. DFA

Page 23: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 24: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

Subset Construction Example

Page 25: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 26: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 27: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 28: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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/

Page 29: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 30: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 31: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 32: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 33: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 34: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 35: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 36: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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

Page 37: Theory of Computation: Nondeterminism & Nondeterministic Finite Automata (NFA); Epsilon Closure; NFA with & w/o Epsilon Transitions; NFA vs. DFA, Subset Construction

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.