55
Fall 2004 COMP 335 1 Another NFA Example 0 q 1 q 2 q 0 1 1 , 0

Another NFA Example

Embed Size (px)

DESCRIPTION

Another NFA Example. Language accepted. (redundant state). Remarks:. The symbol never appears on the input tape. Simple automata:. NFAs are interesting because we can express languages easier than DFAs. NFA. DFA. Formal Definition of NFAs. Set of states, i.e. - PowerPoint PPT Presentation

Citation preview

Page 1: Another NFA Example

Fall 2004 COMP 335 1

Another NFA Example

0q 1q 2q0

11,0

Page 2: Another NFA Example

Fall 2004 COMP 335 2

{ }{ } *10=

...,101010,1010,10,λ=)(ML

0q 1q 2q0

11,0

Language accepted

(redundant state)

Page 3: Another NFA Example

Fall 2004 COMP 335 3

Remarks:

•The symbol never appears on the input tape

0q2M

0q1M

{}=)M(L 1 }λ{=)M(L 2

•Simple automata:

Page 4: Another NFA Example

Fall 2004 COMP 335 4

0q

2q

1qa

a

a

0q 1qa

}{=)( 1 aML

2M1M

}{=)( 2 aML

NFA DFA

•NFAs are interesting because we can express languages easier than DFAs

Page 5: Another NFA Example

Fall 2004 COMP 335 5

Formal Definition of NFAs FqQM ,,,, 0

:Q

::0q

:F

Set of states, i.e. 210 ,, qqq

: Input aphabet, i.e. ba,

Transition function

Initial state

Final states

Page 6: Another NFA Example

Fall 2004 COMP 335 6

10 1, qq

0

11,0

Transition Function

0q 1q 2q

Page 7: Another NFA Example

Fall 2004 COMP 335 7

0q

0

11,0

},{)0,( 201 qqq

1q 2q

Page 8: Another NFA Example

Fall 2004 COMP 335 8

0q

0

11,0

1q 2q

},{),( 200 qqq

Page 9: Another NFA Example

Fall 2004 COMP 335 9

0q

0

11,0

1q 2q

)1,( 2q

Page 10: Another NFA Example

Fall 2004 COMP 335 10

Extended Transition Function

*

0q

5q4q

3q2q1qa

aa

b

10 ,* qaq

Page 11: Another NFA Example

Fall 2004 COMP 335 11

540 ,,* qqaaq

0q

5q4q

3q2q1qa

aa

b

Page 12: Another NFA Example

Fall 2004 COMP 335 12

0320 ,,,* qqqabq

0q

5q4q

3q2q1qa

aa

b

Page 13: Another NFA Example

Fall 2004 COMP 335 13

Formally

wqq ij ,* : there is a walk from to with label

iq jqw

iq jq

w

kw 211 2 k

iq jq

Page 14: Another NFA Example

Fall 2004 COMP 335 14

The Language of an NFA

0q

5q4q

3q2q1qa

aa

b

540 ,,* qqaaq

M

)(MLaa

50 ,qqF

F

Page 15: Another NFA Example

Fall 2004 COMP 335 15

0q

5q4q

3q2q1qa

aa

b

0320 ,,,* qqqabq MLab

50 ,qqF

F

Page 16: Another NFA Example

Fall 2004 COMP 335 16

0q

5q4q

3q2q1qa

aa

b

50 ,qqF

540 ,,* qqabaaq )(MLabaaF

Page 17: Another NFA Example

Fall 2004 COMP 335 17

0q

5q4q

3q2q1qa

aa

b

50 ,qqF

10 ,* qabaq MLabaF

Page 18: Another NFA Example

Fall 2004 COMP 335 18

0q

5q4q

3q2q1qa

aa

b

}{*}{* aaababML

Page 19: Another NFA Example

Fall 2004 COMP 335 19

FormallyThe language accepted by NFA is:

where

and there is some

M

,...,, 321 wwwML

},,...,,{),(* 0 kjim qqqwq

Fqk (final state)

Page 20: Another NFA Example

Fall 2004 COMP 335 20

0q kq

w

w

w

),(* 0 wq MLw

Fqk

iq

jq

Page 21: Another NFA Example

Fall 2004 COMP 335 21

NFA accept Regular Languages

Page 22: Another NFA Example

Fall 2004 COMP 335 22

Equivalence of FA

Definition:

An FA is equivalent to FA

if that is if both accept the same language.

1M 2M

21 MLML

Page 23: Another NFA Example

Fall 2004 COMP 335 23

Example of equivalent FA

0q 1q

0

1

0q 1q 2q

0

11

0

1,0

NFA

DFA

*}10{1 ML

*}10{2 ML

1M

2M

Page 24: Another NFA Example

Fall 2004 COMP 335 24

We will prove:

Languages acceptedby NFA

RegularLanguages

That is, NFA and DFA have the same computation power

Languages acceptedby DFA

Page 25: Another NFA Example

Fall 2004 COMP 335 25

Languages acceptedby NFA

RegularLanguages

Step 1

Proof: Every DFA is trivially an NFA

Any language accepted by a DFAis also accepted by an NFA

L

Page 26: Another NFA Example

Fall 2004 COMP 335 26

Languages acceptedby NFA

RegularLanguages

Step 2

Proof: Any NFA can be converted into anequivalent DFA

Any language accepted by an NFAis also accepted by a DFA

Page 27: Another NFA Example

Fall 2004 COMP 335 27

Convert NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q

M

M

Page 28: Another NFA Example

Fall 2004 COMP 335 28

Convert NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

M

M

Page 29: Another NFA Example

Fall 2004 COMP 335 29

Convert NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

M

M

Page 30: Another NFA Example

Fall 2004 COMP 335 30

Convert NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

a

M

M

Page 31: Another NFA Example

Fall 2004 COMP 335 31

Convert NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

M

M

Page 32: Another NFA Example

Fall 2004 COMP 335 32

Convert NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

M

M

Page 33: Another NFA Example

Fall 2004 COMP 335 33

Convert NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

M

M

)(MLML

Page 34: Another NFA Example

Fall 2004 COMP 335 34

NFA to DFA: Remarks

We are given an NFA

We want to convert it into an equivalent DFA

That is,

M

M

)(MLML

Page 35: Another NFA Example

Fall 2004 COMP 335 35

If the NFA has states

Then the DFA has states in the powerset

,...,, 210 qqq

,....,,,, 2110 qqqq

Page 36: Another NFA Example

Fall 2004 COMP 335 36

Procedure NFA to DFA

1. Initial state of NFA:

Initial state of DFA:

0q

0q

Page 37: Another NFA Example

Fall 2004 COMP 335 37

Example

a

b

a

0q 1q 2q

NFA

DFA

0q

M

M

Page 38: Another NFA Example

Fall 2004 COMP 335 38

Procedure NFA to DFA

2. For every DFA’s state

Compute in the NFA

Add the following transition to the DFA

},...,,{ mji qqq

...

,,*

,,*

aq

aq

j

i

},...,,{ mji qqq

},...,,{},,...,,{ mjimji qqqaqqq

Page 39: Another NFA Example

Fall 2004 COMP 335 39

Example

a

b

a

0q 1q 2q

NFA

0q 21,qqa

DFA

},{),(* 210 qqaq

210 ,, qqaq

M

M

Page 40: Another NFA Example

Fall 2004 COMP 335 40

Procedure NFA to DFA

Repeat step 2 for all symbols in the alphabet ∑, until no more transitions can be added.

Page 41: Another NFA Example

Fall 2004 COMP 335 41

Example

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

M

M

Page 42: Another NFA Example

Fall 2004 COMP 335 42

Procedure NFA to DFA

3. For any DFA state:

If some is a final state in the NFA

Then, is a final state in the DFA

},...,,{ mji qqq

jq

},...,,{ mji qqq

Page 43: Another NFA Example

Fall 2004 COMP 335 43

Example

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

Fq 1

Fqq 21,

M

M

Page 44: Another NFA Example

Fall 2004 COMP 335 44

Theorem Take NFA M

Apply the procedure to obtain DFA M

Then, and are equivalent:M M

MLML

Page 45: Another NFA Example

Fall 2004 COMP 335 45

Proof

MLML

MLML MLML AND

Page 46: Another NFA Example

Fall 2004 COMP 335 46

MLML First we show:

)(MLwTake arbitrary string :

We will prove: )(MLw

Page 47: Another NFA Example

Fall 2004 COMP 335 47

)(MLw

0q fq

w

kw 211 2 k

0q fq:M

:M

Page 48: Another NFA Example

Fall 2004 COMP 335 48

0q fq

kw 211 2 k

:M

}{ 0q

1 2 k:M

},{ fq

)(MLw

)(MLw

We will show that if

Page 49: Another NFA Example

Fall 2004 COMP 335 49

0q mq

naaav 211a 2a na:M

}{ 0q

1a 2a na:M },{ iq

iq jq lq

},{ jq },{ lq },{ mq

More generally, we will show that if in :M

(arbitrary string)

Page 50: Another NFA Example

Fall 2004 COMP 335 50

0q 1a:M

}{ 0q

1a:M },{ iq

iq

Proof by induction on

The basis case: 1av

|| v

Page 51: Another NFA Example

Fall 2004 COMP 335 51

Induction hypothesis: kv ||1

0q dq1a 2a ka:M iq jq cq

}{ 0q

1a 2a ka:M },{ iq },{ jq },{ cq },{ dq

kaaav 21

Page 52: Another NFA Example

Fall 2004 COMP 335 52

Induction Step: 1|| kv

0q dq1a 2a ka:M iq jq cq

}{ 0q

1a 2a ka:M },{ iq },{ jq },{ cq },{ dq

1121

kk

v

k avaaaav

v

v

Page 53: Another NFA Example

Fall 2004 COMP 335 53

Induction Step: 1|| kv

0q dq1a 2a ka:M iq jq cq

}{ 0q

1a 2a ka:M },{ iq },{ jq },{ cq },{ dq

1121

kk

v

k avaaaav

v

v

eq1ka

1ka

},{ eq

Page 54: Another NFA Example

Fall 2004 COMP 335 54

0q fq

kw 211 2 k

:M

}{ 0q

1 2 k:M

},{ fq

)(MLw

)(MLw

Therefore if

Page 55: Another NFA Example

Fall 2004 COMP 335 55

We have shown: MLML

We also need to show: MLML

(proof is similar)