Language of a DFA Example 1: DFA...

9
10/16/2009 1 THEOCOM R i Nt f Fi tE Review Notes for FirstExam 1 st SY 20092010 DFA Deterministic finite automata: Example transition function δ: δ(q a) = p q 0 q 1 q 2 1 0 0 0,1 1 Transition diagram Σ = {0, 1} Q = {q 0 , q 1 , q 2 } initial state: q 0 F = {q 0 , q 1 } q 0 q 1 q 2 q 2 q 2 q 1 δ(q, a) = p states Transition table inputs 0 1 q 0 q 1 q 2 Language of a DFA Example 1: DFA A a, b Transition diagram L(A)={ ε} q 0 q 1 a, b Language of a DFA Example 2: DFA A L(A) = {}= φ a,b Transition diagram q 0 Language of a DFA Example 3: DFA A q 0 q 2 a b a a q 3 q 1 b b L(A) = {a(ab)*aa} q 4 a, b a, b b

Transcript of Language of a DFA Example 1: DFA...

Page 1: Language of a DFA Example 1: DFA Amysite.dlsu.edu.ph/faculty/bulosr/THEOCOM/ReviewNotes-FirstExam... · 10/16/2009 2 Language of a DFA Example 4: DFA A a a q3 q1 b q0 b a, b L(A)

10/16/2009

1

THEOCOMR i N t f Fi t EReview Notes for First Exam

1st SY 2009‐2010

DFA

Deterministic finite automata: Example

transition function δ:δ(q a) = p

q0 q1 q21 0

0 0,11

Transition diagram

Σ = {0, 1}Q = {q0, q1, q2}initial state: q0F = {q0, q1}

q0 q1q2

q2q2

q1

δ(q, a) = p

stat

es

Transition table

inputs

0 1q0q1q2

Language of a DFA   Example 1: DFA A

a, b

Transition diagram

L(A) = { ε}q0 q1

a, b

Language of a DFA Example 2: DFA A

L(A) = {}= φa,b

Transition diagram

q0

Language of a DFA Example 3: DFA A

q0 q2a b a

aq3q1

bb

L(A) = {a(ab)*aa}

q4

a, b a, bb

Page 2: Language of a DFA Example 1: DFA Amysite.dlsu.edu.ph/faculty/bulosr/THEOCOM/ReviewNotes-FirstExam... · 10/16/2009 2 Language of a DFA Example 4: DFA A a a q3 q1 b q0 b a, b L(A)

10/16/2009

2

Language of a DFA Example 4: DFA A

a aq3

q1

b

q0

b a, b

L(A) = {(ab + bb)*}

q2

ab

b

Language of a DFA  Example 5: DFA A

q0 q1 q2a b q3

a a

b

L(A) = {a( baa + bba )*bb }

q4

a,b bb a

Language of a DFA Example 6: DFA A

q0 q1 q2a

b a, b

a

b

L(A) = {b* + b*a(ba)* }

q3

b ab

Examples• Construct a DFA that accepts the language 

L = {010, 1} Σ = {0, 1}

q0

q1

q01 q010

qdie 0, 1

01 0

0, 11

0 10, 1

11

0

• Construct a DFA over alphabet {0, 1} that accepts all strings that end in 101

• Sketch of answer:

qε q1 q10 q101

1

0

0 1

1

1

q0 q1

0 0

1

1

0 1

1

Exercises: Answers

L(A)={0*(1(0)*1(0)*)*}

L(A)={0*11*(00*11*)*}={(0+1)*1}q0 q1

q0 q1 q2

0

1 0

0 0,11

What are the languages of these DFAs ?

L(A)={0*+ (0*1(1*))}

( ) { ( ) } {( ) }

Page 3: Language of a DFA Example 1: DFA Amysite.dlsu.edu.ph/faculty/bulosr/THEOCOM/ReviewNotes-FirstExam... · 10/16/2009 2 Language of a DFA Example 4: DFA A a a q3 q1 b q0 b a, b L(A)

10/16/2009

3

NFA (without ε – transitions)ε NFA

δ for NFA: DefinitionTransition diagram: NFA

a q1ab

b

Q = {q0, q1 , q2 , q3 }

Σ = { a, b }

F = {q3 }

Initial state:q0q0 q3

q2

ba

a

{q0, q1, q3} {q2}∅ {q3}{q3} ∅∅ {q1}

a bq0

q1

q2

q3

inputs

states

transition function δ:

δ(q0 , ab ) = {q1 , q2 , q3 }

Example NFATransition diagram: NFA

a q1a

ab

bQ = {q0, q1 , q2 , q3 }

Σ = { a, b }F = {q3 }

Initial state:q0

{q0, q1, q3} {q2}∅ {q3}{q3} ∅

∅ {q1}

a bq0

q1

q2

q3

inputs

states

transition function δ:

q0 q3

q2

ba

L(A)= a*( ab + a + ba )(bb)*

NFA Example

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

start q2

a

q3

t

q1

e

q4

Σ

q0

Σ

q6

e

q5

a

s

s y

• Construct NFA for the language over the alphabet {a, b, …, z} such that every string doesn’t contain “fool”.

‘f’ ‘o’ ‘l’

not ‘f’ ‘f’

‘f’

‘f’ dead statedead state

start q0 q1

fq2

‘o’

oq2 q3

l

not ‘f’,‘o’not ‘f’,‘l’

not ‘f’,‘o’

Example : εNFAQ = {q0, q1 , q2 , q3 , q4 , q5 }

Σ = { a, b }

F = ∅Initial state:q0

∅{q1}a b

q0

q1

q2

inputs

states

transition function δ:

ε{q4 }

{q2} ∅ ∅{q3 , q4} ∅ ∅

q3

q4

q5

{q5} ∅ {q4 }{q5} ∅

∅ ∅ ∅

aq0

Transition diagram: εNFA

q2q1

b

q3

q4 q5

ε ε

εε ε

a

Page 4: Language of a DFA Example 1: DFA Amysite.dlsu.edu.ph/faculty/bulosr/THEOCOM/ReviewNotes-FirstExam... · 10/16/2009 2 Language of a DFA Example 4: DFA A a a q3 q1 b q0 b a, b L(A)

10/16/2009

4

δ for εNFAaq0 q2q1

b

q3

q4 q5

ε ε

εε a

q4 q5a

δ(q0 , ab ) = {q3,q4}

ε( {q0 } ) = {q0 , q3 , q4 }

L(A) = {ε , a , ab }

Exercise0 21

b4

5

ε

ε

b

a

a

3

ε

ba

ε( {0} ) = {0,1,2}δ(0, ab)? = {3,5}L(A) = (b + a + )   (a + b )

Example of εNFA

q0 q1 q2ε,b

a

a

εΣ = {a, b}

• Which of the following  strings is accepted by this εNFA?

yesaab bb no

yes

aab

bab

ab

bb

a

εyes

yes

yes

Example• Construct an NFA that accepts all strings with an even number of 0s or an odd number of 1s

r r

1 10 even number of 0sr0 r10

even number of 0s

s0 s1

0 01

1odd number of 1s

q0

ε

ε

ε-transitions can be taken for free (without reading input)

Regular Expressions

Examples of RE where 

01* 0 followed by any number of 1s= 0(1*) = {0, 01, 011, 0111, …}

(01*)(01) = {001, 0101, 01101, 011101, …} 0 followed by any number of 1s and then 01

0+1 = {0, 1} strings of length 1

(0+1)*010 any string that ends in 010

(0+1)*01(0+1)*

(0+1)* = {ε, 0, 1, 00, 01, 10, 11, …} any string

Σ = {0, 1}

(0+1)*01(0+1)* any string that contains the pattern 01

((0+1)(0+1))*+((0+1)(0+1)(0+1))* all strings whose length is even or divisible by 3= strings of length 0, 2, 3, 4, 6, 8, 9, 10, 12, ...

((0+1)(0+1)+(0+1)(0+1)(0+1))* all strings except 0 and 1

(1+01+001)*(ε+0+00) String that does not contain 000

Page 5: Language of a DFA Example 1: DFA Amysite.dlsu.edu.ph/faculty/bulosr/THEOCOM/ReviewNotes-FirstExam... · 10/16/2009 2 Language of a DFA Example 4: DFA A a a q3 q1 b q0 b a, b L(A)

10/16/2009

5

Road map

ε NFA

regular

NFAwithout ε

DFAregularexpression

DFA2‐stateGNFA

GNFA

regular expression → εNFA: General method

∅ q0

ε q0

symbol a q0 q1a

RS q0 q1εMR MS

εε

R + S

q0q1

εMR

MS

εε

εR*

q0 q1εMR

ε

ε

ε

regular expression → εNFAε

ε

Example : Convert R3 = (0 + 1)* to εNFA

M2

q0 q1

ε

εε

εq2 q3

0

q4 q51

q’0ε

q’1ε

regular expression → εNFA• R4 = (aa + b)*  Construct  the εNFA 

(aa+b)*

a aεε

ε

2

b

ε

ε

ε

ε

0

1 2 3 4

5 6

7

Exercise: R5 = ((a +b)a*)*   Construct the εNFAa1 2a : 3 4b : b

6 7 8a* : ε aε

a1 2a + b :

b3 4

5

a

ε

ε

a1(a + b)a* :

b365

4

2εε

ε

ε 7 8ε aε

a

b36

4

2εε

ε

ε 7 8ε aε

0

((a + b)a*)* :

ε

5ε1

Example1 : εNFA to NFA conversion

q2q1

ba

a

ε

q0

εq3

εNFA

a

q1

b

a

q3q0 q2a

a

b

a

a

NFA

Page 6: Language of a DFA Example 1: DFA Amysite.dlsu.edu.ph/faculty/bulosr/THEOCOM/ReviewNotes-FirstExam... · 10/16/2009 2 Language of a DFA Example 4: DFA A a a q3 q1 b q0 b a, b L(A)

10/16/2009

6

Exercise 1: εNFA to NFA conversion 

a0 1

ε

ε2 4b

a

3

Convert the εNFA to NFA

Exercise 1: εNFA to NFA conversion 

Answer

a1

2

a

ba

2

2

4

3

0a,b

a

a

aa,b

a

a a

Example 1 :  εNFA to NFA conversion

states

inputs

ε a b ε({q}) Sa Sb ε({Sa}) ε({Sb})

q0

q1

q2

q

{q1, q2}

∅ ∅{q1}

∅ ∅

{q3}

∅∅

{ } ∅ ∅

{q0, q1}

{q1}

{q2}

{q1, q3}

{q1, q2} ∅

{q1, q2} ∅

∅ {q3}

{q1, q2} ∅

{q1, q2} ∅

{q1, q2} ∅

∅ {q1, q3}

{q1, q2} ∅q3 {q1} ∅ ∅ {q1, q3} {q1, q2} ∅ {q1, q2} ∅

a

q1

b

a

q3q0 q2a

a

b

a

a

For every accept state qf, make accepting all states connected to it via εs.

F2= {q0,q1, q3}

Example2: of εNFA to NFA conversionq0 q1 q2

ε,ba

a

εεNFA:

NFA: q0 q1 q2a, b

a

a

aa

a, b

Transition table of NFA:

states

inputsa b

q0

q1

q2

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

F2 = {q0, q1, q2}

Example 1: NFA to DFA Conversionδ( { 0 } , a ) = { 1 , 2 }δ( { 0 } , b ) = ∅δ( { 1 , 2 } , a ) = { 1 , 2 }δ( { 1 , 2 } , b ) = { 1 , 3 }δ( { 1 , 3 } , a ) = { 1 , 2 }δ( { 1 , 3 } , b ) = ∅δ(∅, a ) =∅δ( (∅, b ) = ∅

0

1

3

aa

b

2

a

a

aa

b

NFA: M2

Q={ {0} , { 1 , 2 }, ∅, { 1 , 3 } }F = {{0}, {1,2}, {1,3}}

aa

a

a

b

b

b

DFA: M

{0}

{1,2}

{1,3},b

Exercise 1: NFA to DFA Conversion

• Convert the NFA to DFA

Page 7: Language of a DFA Example 1: DFA Amysite.dlsu.edu.ph/faculty/bulosr/THEOCOM/ReviewNotes-FirstExam... · 10/16/2009 2 Language of a DFA Example 4: DFA A a a q3 q1 b q0 b a, b L(A)

10/16/2009

7

Exercise 1: NFA to DFA Conversion

• Answer to exercise 1

Example 3: DFA to RE ConversionFind a regular expression equivalent to the DFA below.

aq q

b

q0 q1

q2

a

abb

From DFA to GNFA: Conversion example

aq0 q1

b

a

abb

ε

q2

Add a new initial state S and a common final state A using ε−transitions.

GNFA Reduction example

aq1

b

a

b

ε

q0

q2

abb

Rip out (remove) state q0

GNFA Reduction example

aq1

b

a

abb

S

A

ε

ε

q2

Rip out (remove) state q0

S q1 q2

S ‐‐

q1 ‐‐

q2 ‐‐

a b

aa ab

ba bb

GNFA Reduction example

aq1

b

ab

S

A

ε

+ aa

+ ba

abbb

q2

Rip out (remove) state q0

S q1 q2

S ‐‐ a b

q1 ‐‐ aa ab

q2 ‐‐ ba bb

bb

Page 8: Language of a DFA Example 1: DFA Amysite.dlsu.edu.ph/faculty/bulosr/THEOCOM/ReviewNotes-FirstExam... · 10/16/2009 2 Language of a DFA Example 4: DFA A a a q3 q1 b q0 b a, b L(A)

10/16/2009

8

GNFA Reduction example

aq1

b

ab

S

ε

+ aa

+ ba

q2

Rip out (remove) state q1

abbb

GNFA Reduction example

ab

ab

S

A

ε

+ aa

+ ba

abbb

q2

Rip out (remove) state q1

S q2 A

S ‐‐

q2 ‐‐

A ‐‐ ‐‐ ‐‐

bb

a (b+aa)* ab a (b+aa)*

(a+ba) (b+aa)* ab (a+ba) (b+aa)*

GNFA Reduction example

b

S A

bb

a (b+aa)*

+ a (b+aa)* ab

ε+ (a+ba) (b+aa)* ab

+(a+ba) (b+aa)*

q2Rip out (remove) state q1

S q2 A

S ‐‐ a (b+aa)* ab a (b+aa)* 

q2 ‐‐ (a+ba) (b+aa)* ab (a+ba) (b+aa)* 

A ‐‐ ‐‐ ‐‐

bb + (a+ba) (b+aa) ab

GNFA Reduction example

b

S A

bb

a (b+aa)*

+ a (b+aa)* ab

ε+ (a+ba) (b+aa)* ab

+(a+ba) (b+aa)*

q2

Rip out (remove) state q2

bb + (a+ba) (b+aa) ab

GNFA Reduction example

b

S A

bb

a (b+aa)*

+ a (b+aa)* ab

ε+ (a+ba) (b+aa)* ab

+(a+ba) (b+aa)*

Rip out (remove) state q2

S A

S ‐‐

A ‐‐ ‐‐

bb + (a+ba) (b+aa) ab

(b+a (b+aa)* ab) (bb+(a+ba) (b+aa)*ab))* (ε+(a+ba) (b+aa)* )

GNFA Reduction exampleS A

a (b+aa)*

(b+a (b+aa)* ab) (bb+(a+ba) (b+aa)*ab))* (ε+(a+ba) (b+aa)* )

Rip out (remove) state q2

S A

S ‐‐ (b+a (b+aa)* ab) (bb+(a+ba) (b+aa)*ab))* (ε+(a+ba) (b+aa)* )

A ‐‐ ‐‐

Page 9: Language of a DFA Example 1: DFA Amysite.dlsu.edu.ph/faculty/bulosr/THEOCOM/ReviewNotes-FirstExam... · 10/16/2009 2 Language of a DFA Example 4: DFA A a a q3 q1 b q0 b a, b L(A)

10/16/2009

9

Example : DFA to RE ConversionFind a regular expression equivalent to the DFA below.

aq0 q1

b

a

abb

q2

abb

S A

a (b+aa)*  (b+a (b+aa)* ab) (bb+(a+ba) (b+aa)*ab))* (ε+(a+ba) (b+aa)* )+

Exercise 1: DFA to RE Conversion

q1 q2

0 110

q0ε ε q3

Eliminate q1: q2q0 q30*1

00*1+1ε

Eliminate q2:

q

q0 q30*1(00*1+1)*

Check: 0*1(00*1+1)* = q1 q2

0 110