Download - Theory of Automata - Solved Assignments - Semester Spring 2010

Transcript
Page 1: Theory of Automata - Solved Assignments - Semester Spring 2010

Theory of Automata

Solved Assignments

Semester Spring 2010

Assignment 1

Question No.1 Marks: 4

a) Give regular expressions of the following languages over Σ={0,1}:

1. All strings having no pair of consecutive zeros.

2. All strings having exactly two 1’s or three 1’s not more than it.

b) Show that the Regular expression ^ + 0(0+1)*+(0+1)*00(0+1)* is equivalent to ((0*1)*01*)*

Solution:

a)

1. RE for the language having all strings having no pair of consecutive zeros.

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

Other possibilities might be

(1 + 01)*(0 + λ) + 1* or (1 + 01)* + (1 + 01)*0 + 1*

2. RE for the language having all strings having exactly two 1’s or three 1’s not more than it.

0*10*10* + 0*10*10*10*

b) After generating the string from both regular expressions :

^ + 0(0+1)*+(0+1)*00(0+1)*= ^, 0, 00, 01,001,010,100,011,000,0011,1100………

((0*1)*01*)* = ^,0,00,01,10,010,100,011,101,110,000,……………..

Page 2: Theory of Automata - Solved Assignments - Semester Spring 2010

We can see that first RE cannot generate the string ‘10’ while this string can be generated by second RE. So they are

not expressing the same language. Hence it is concluded that given Regular expressions are NOT equivalent.

Question No. 2 Marks: 4

a) Give recursive definition for the language ODD, of strings defined over ∑={-,0,1,2,3,4,5,6,7,8,9}, b) Give recursive definition for the language of palindromes having odd length

Solution:

a)

Step1: 1 is in ODD

Step2: if x is in ODD, then x+2 and x-2 are also in ODD

Step3: No Strings except those constructed above, are allowed to be in ODD

b)

Step1: a and b are in PALINDROME.

Step2: if x is in PALINDROM then axa and bxb are also in PALINDROME

Step3: No Strings except those constructed above, are allowed to be in PALINDROME

Question No. 3 Marks: 6

Three Finite Automata (FAs) have been given below:

FA1

Page 3: Theory of Automata - Solved Assignments - Semester Spring 2010

FA2

FA3

Match the following Regular Expressions (RE’s) with corresponding Finite Automata (FA’s) given above. Also

describe their languages (in English).

Regular Expression Finite Automaton (FA) Language Description

a(aa)*(^+a)b+b

aa*b(aa*b)*

Page 4: Theory of Automata - Solved Assignments - Semester Spring 2010

(a+b)*a(a+b)*b(a+b)*

Solution:

Regular Expression Finite Automaton (FA) Language Description

a(aa)*(^+a)b+b FA3 All strings in which “b” occurs only

one time and that is only at the end

of the string.

aa*b(aa*b)* FA1 All strings which begins with “a”

and ends at “b” and not containing

the substring “bb”.

(a+b)*a(a+b)*b(a+b)* FA2 All strings having substring “ab”.

Question No. 4 Marks: 6

Build an FA over Σ={a, b} that accepts only those words that do not contain the substring “ba”.

Also make transition table for that FA.

Solution:

FA

Page 5: Theory of Automata - Solved Assignments - Semester Spring 2010

Transition table corresponding to above FA

Old States

New States

After Reading a After Reading b

q0- q3 q1

q1+ q2 q1

q2 q2 q2

q3+ q3 q1

Page 6: Theory of Automata - Solved Assignments - Semester Spring 2010

Assignment 2

Question No.1

Find the Regular Expression corresponding to the given FA. Show all steps. Mark: 5

[Hint: FA to GTG a GTG to RE.]

Solution:

Step1:

Page 7: Theory of Automata - Solved Assignments - Semester Spring 2010

Step2:

Eliminating loop from q1

Step3:

Eliminating state q1

Step4:

To connect the initial state to the final state by single transition edge, the above TG can be reduced to the following

Hence the Required RE is

((0+1)0*11*0 )* . (0+1)0*11*

Page 8: Theory of Automata - Solved Assignments - Semester Spring 2010

Question No.2 Mark: 5

A student walks into a classroom and sees on the blackboard a diagram of a TG with two states that accepts only the

string Λ. The student reverses the direction of exactly one edge, leaving all other edges and all labels and all final

states (+’s) and start state (-) the same. The new TG accepts the language a*. What was the original TG?

Solution:

Original TG

accepts only Λ

New TG

accepts the language a*

Question No. 3

Page 9: Theory of Automata - Solved Assignments - Semester Spring 2010

Determine RE corresponding to the following TG. Show all steps. Mark: 10

[Hint: TG to GTG a GTG to RE.]

Solution:

Step 1:

By eliminating states 7,8,10,11

Page 10: Theory of Automata - Solved Assignments - Semester Spring 2010

Step 2:

By eliminating states 6

Page 11: Theory of Automata - Solved Assignments - Semester Spring 2010

Step 3:

By eliminating states 9 and 12

Step 4:

By combining loops at state 3

Step 5:

By eliminating states 2

Step 6:

Page 12: Theory of Automata - Solved Assignments - Semester Spring 2010

By eliminating states 4

Step 7:

By eliminating states 3

Hence the Required RE is ba(abba+aabb)*(b+Λ)ab

Page 13: Theory of Automata - Solved Assignments - Semester Spring 2010

Assignment 3

Question No 1: Marks: 12

Let

r1= (a+b)*ab(a+b)*

r2= (a*ba)*a*b (The RE given in assignment was incorrect so it has been corrected now

According to the requirement of the question you had to work with FAs so mistake in RE will not effect your

solution)

FA1 corresponding to r1

FA2 corresponding to r2

Using the FAs corresponding to r1 and r2, Build an FA corresponding to

a) r1+ r2 (Union of r1 and r2).

b) r1r2 (Concatenation of r1 and r2).

c) r1* (Closure of r1).

Page 14: Theory of Automata - Solved Assignments - Semester Spring 2010

Note :You have to specify transition table and transition diagram for each part(a)(b)(c)

Solution:

a)

Transition table corresponding to required FA

Old States

New states after reading

a b

1 1 1( , )z x y− ≡ 2 1 2( , )x y z≡ 1 2 3( , )x y z≡

2 2 1( , )z x y≡ 2 1 2( , )x y z≡ 3 2 4( , )x y z≡

3 1 2( , )z x y+ ≡ 2 1 2( , )x y z≡ 1 3 5( , )x y z≡

4 3 2( , )z x y+ ≡ 3 1 7( , )x y z≡ 3 3 6( , )x y z≡

5 1 3( , )z x y≡ 2 3 8( , )x y z≡ 1 3 5( , )x y z≡

6 3 3( , )z x y+ ≡ 3 3 6( , )x y z≡ 3 3 6( , )x y z≡

7 3 1( , )z x y+ ≡ 3 1 7( , )x y z≡ 3 2 4( , )x y z≡

8 2 3( , )z x y≡ 2 3 8( , )x y z≡ 3 3 6( , )x y z≡

Page 15: Theory of Automata - Solved Assignments - Semester Spring 2010

FA corresponding to r1+ r2

b)

Transition table corresponding to required FA

Old States

New states after reading

a b

1 1z x− ≡ 2 2x z≡ 1 1x z≡

2 2z x≡ 2 2x z≡ 3 1 3( , )x y z≡

3 3 1( , )z x y≡ 3 1 3( , )x y z≡ 3 1 2 4( , , )x y y z=

4 3 1 2( , , )z x y y+ ≡ 3 1 3( , )x y z= 3 1 2 3 5( , , , )x y y y z≡

5 3 1 2 3( , , , )z x y y y+ ≡ 3 1 3 6( , , )x y y z≡ 3 1 2 3 5( , , , )x y y y z≡

6 3 1 3( , , )z x y y≡ 3 1 3 6( , , )x y y z≡ 3 1 2 3 5( , , , )x y y y z≡

FA corresponding to r1r2

Page 16: Theory of Automata - Solved Assignments - Semester Spring 2010

c)

Transition table corresponding to required FA

Old States

New states after reading

a b

Final 1 1z x± ≡ 2 3x z≡ 1 2x z≡

Non-final 2 1z x≡ 2 3x z≡ 1 2x z≡

3 2z x≡ 2 3x z≡ 3 1 4( , )x x z≡

4 3 1( , )z x x+ ≡ 3 1 2 5( , , )x x x z≡ 3 1 4( , )x x z=

5 3 1 2( , , )z x x x+ ≡ 3 1 2 5( , , )x x x z≡ 3 1 4( , )x x z=

FA corresponding to r1*

Page 17: Theory of Automata - Solved Assignments - Semester Spring 2010

Question No 2: Marks: 4

Convert the following NFA into FA.

Page 18: Theory of Automata - Solved Assignments - Semester Spring 2010

Solution:

Transition table corresponding to required FA

Old States

New states after reading

a b

1 0z ± = 2(1, 2) z≡ Φ

2 (1, 2)z + ≡ 3(1, 2,3) z≡ 4(1,3) z≡

3 (1, 2,3)z + ≡ 3(1, 2,3) z≡ 4(1,3) z≡

4 (1,3)z + ≡ 3(1, 2,3) z≡ Φ

FA corresponding to given NFA

Question No 3: Marks: 4

Build an NFA equivalent to the FA* where

Page 19: Theory of Automata - Solved Assignments - Semester Spring 2010

FA

Solution:

NFA equivalent to the FA* where

Page 20: Theory of Automata - Solved Assignments - Semester Spring 2010

Assignment 4

Question No 1: Marks: 5

Let L be the language of strings defined over Σ= {0, 1} having odd no. of 1’s.Now Find Distinguishable and

Indistinguishable strings by taking z as “1” belonging to Σ*.

Solution:

x y Distinguishable/Indistinguishable

11000 10000 Distinguishable

00000 11111 Distinguishable

10010 00110 Indistinguishable

10011 10000 Indistinguishable

00111 10111 Distinguishable

Question No 2:

Marks:10

Convert the following Moore machine to an equivalent Mealy machine:

Page 21: Theory of Automata - Solved Assignments - Semester Spring 2010

a) Construct transition table for Mealy machine.

b) Run the string “babaaba” on both machines and find the output.

Solution:

Mealy machine corresponding to Moore machine

a) Transition table for Mealy machine.

States

input

a b

Output Next State Output Next State

q0 1 q2 0 q1

Page 22: Theory of Automata - Solved Assignments - Semester Spring 2010

b) Run the string “babaaba” on both machines and find the output.

Input b a b a a b a

States q0 q1 q2 q2 q3 q3 q3 q3

Mealy 0 1 1 0 0 0 0

Moore 1 0 1 1 0 0 0 0

Question No 3:

Marks:5

Using De-Morgan’s law determine an FA corresponding to FA1∩FA2

FA1:

FA2:

q1 1 q2 0 q3

q2 0 q3 1 q2

q3 0 q3 0 q3

Page 23: Theory of Automata - Solved Assignments - Semester Spring 2010

Solution:

FA1c:

FA2c:

Transition table for FA1c U FA2

c

Old states

New States After Reading

a b

Page 24: Theory of Automata - Solved Assignments - Semester Spring 2010

z1 ± ≡ (x0,y0) z2 ≡ (x1,y0) z3 ≡ (x1,y1)

z2 + ≡ (x1,y0) z1 ≡ (x0,y0) z4 ≡ (x0,y1)

z3+ ≡ (x1,y1) z5 ≡ (x0,y2) z4 ≡ (x0,y1)

z4+ ≡ (x0, y1) z6 ≡ (x1,y2) z3 ≡ (x1,y1)

z5 ≡ (x0,y2) z6 ≡ (x1,y2) z6 ≡ (x1,y2)

z6+ ≡ (x1,y2) z5 ≡ (x0,y2) z5 ≡ (x0,y2)

FA corresponding to FA1c U FA2

c

FA corresponding to ( FA1c U FA2

c)c = FA1 ∩ FA2

Page 25: Theory of Automata - Solved Assignments - Semester Spring 2010

Assignment 5

Question No 1: Marks: 5

Find the prefixes of Q in R where Q and R are languages give below:

Q={ ababa , aabbbbbbb,baabba , babbaaaaaa }

R={ aababa , bbaabba , bbaababa, aaaabbbbbbb, abbabbaaaaaa}

Solution:

Pref(Q in R)={a,b,aa,ab,bba, }

Question No. 2 Marks: 5

By marking the states, decide whether the following FA accepts any word or not?

Note: Show all steps

Page 26: Theory of Automata - Solved Assignments - Semester Spring 2010

Solution:

Marking and processing state q0

Processing state q1

Processing state q3

Page 27: Theory of Automata - Solved Assignments - Semester Spring 2010

Processing state q2

Since no new state is marked and the final state q4 is unmarked therefore, the given FA does not accept any word.

Question No. 3 Marks: 5

Describe the language (in English) generated by the following CFG:

S → XY

X → aX | bX | Λ

Y → baa | bab | bba | bbb

Solution:

The given CFG generates the language L = {w ∈ {a, b}*: length(w) ≥ 3 and third letter of w, from right is b.

Question No. 4 Marks: 5

Construct the CFG that generates the language L = {w ∈ {a, b}*: length(w) ≥ 2 and w begins with b and ends in aa or

bb}.

Page 28: Theory of Automata - Solved Assignments - Semester Spring 2010

Solution:

S → bXY|bb

X → aX | bX | Λ

Y → aa | bb

Page 29: Theory of Automata - Solved Assignments - Semester Spring 2010

Assignment 6

Question: Mark:5+5+5+5

Consider the following Context Free Grammar (CFG)

S → aY | Ybb | Y

X → Λ | a

Y → aXY | bb | XXa

a) Is the CFG ambiguous, if Yes then justify your answer.

b) Draw a Total Language Tree (TLT) for the given CFG.

c) Convert the CFG into CNF.

d) Build the PDA corresponding to the CFG (in CNF) of part (c).

Solution :

a) The CFG is said to be ambiguous if there exists atleast one word of it’s language that can be generated by

the different production trees.

The word abb can be generated by the following three different trees

S ⇒ aY

S ⇒ abb

S ⇒ Ybb

S ⇒ XXabb

S ⇒ Xabb

S ⇒ abb

S ⇒ Y

S ⇒ aXY

S ⇒ aY

S ⇒ abb

Page 30: Theory of Automata - Solved Assignments - Semester Spring 2010

Hence it has been concluded that the given CFG is ambiguous.

b) Total Language Tree

We can see that Y is repeating it self so Tree is growing in recursive manner.

c) Converting CFG into CNF

Remove the null production X →Λ.The new CFG, without null productions, is:

S → aY | Ybb | Y

X →a

Page 31: Theory of Automata - Solved Assignments - Semester Spring 2010

Y →aXY | aY | bb | XXa | Xa | a

The only unit production is S → Y, where the Y can be replaced with all Y’s non-unit productions (i.e. all of them).

The new CFG, without unit productions, is:

S → aY | Ybb | aXY | aY | bb | XXa | Xa | a

X →a

Y →aXY | aY | bb | XXa | Xa | a

Introduce the non-terminal

A →a

B →b

The new CFG, consisting of only solid non-terminals or one terminal is:

S → AY | YBB | AXY | AY | BB | XXA | XA | a

X → a

Y → AXY | AY | BB | XXA | XA | a

A → a

B → b

Shorten the strings of non-terminals to length 2.Create new, intermediate non-terminals to accomplish this.

C → BB

D → XY

E → XA

Page 32: Theory of Automata - Solved Assignments - Semester Spring 2010

The new CFG, in CNF is:

S → AY | YC | AD | AY | BB | XE | XA | a

X → a

Y → AD | AY | BB | XE | XA | a

A → a

B → b

C → BB

D → XY

E → XA

d) PDA corresponding to the CFG

Page 33: Theory of Automata - Solved Assignments - Semester Spring 2010