Anda di halaman 1dari 33

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,……………..
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
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)*
(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
Transition table corresponding to above FA

New States

Old States After Reading a After Reading b

q0- q3 q1

q1+ q2 q1

q2 q2 q2

q3+ q3 q1
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:
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*
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
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


Step 2:

By eliminating states 6
Step 3:

By eliminating states 9 and 12

Step 4:

By combining loops at state 3

Step 5:

By eliminating states 2

Step 6:
By eliminating states 4

Step 7:

By eliminating states 3

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


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).
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

z1 − ≡ ( x1 , y1 ) ( x2 , y1 ) ≡ z2 ( x1 , y2 ) ≡ z3

z2 ≡ ( x2 , y1 ) ( x2 , y1 ) ≡ z2 ( x3 , y2 ) ≡ z4

z3 + ≡ ( x1 , y2 ) ( x2 , y1 ) ≡ z2 ( x1 , y3 ) ≡ z5

z4 + ≡ ( x3 , y2 ) ( x3 , y1 ) ≡ z7 ( x3 , y3 ) ≡ z6

z5 ≡ ( x1 , y3 ) ( x2 , y3 ) ≡ z8 ( x1 , y3 ) ≡ z5

z6 + ≡ ( x3 , y3 ) ( x3 , y3 ) ≡ z6 ( x3 , y3 ) ≡ z6

z7 + ≡ ( x3 , y1 ) ( x3 , y1 ) ≡ z7 ( x3 , y2 ) ≡ z4

z8 ≡ ( x2 , y3 ) ( x2 , y3 ) ≡ z8 ( x3 , y3 ) ≡ z6
FA corresponding to r1+ r2

b)

Transition table corresponding to required FA

Old States New states after reading

a b

z1 − ≡ x1 x2 ≡ z2 x1 ≡ z1

z2 ≡ x2 x2 ≡ z2 ( x3 , y1 ) ≡ z3

z3 ≡ ( x3 , y1 ) ( x3 , y1 ) ≡ z3 ( x3 , y1 , y2 ) = z4

z4 + ≡ ( x3 , y1 , y2 ) ( x3 , y1 ) = z3 ( x3 , y1 , y2 , y3 ) ≡ z5

z5 + ≡ ( x3 , y1 , y2 , y3 ) ( x3 , y1 , y3 ) ≡ z6 ( x3 , y1 , y2 , y3 ) ≡ z5

z6 ≡ ( x3 , y1 , y3 ) ( x3 , y1 , y3 ) ≡ z6 ( x3 , y1 , y2 , y3 ) ≡ z5

FA corresponding to r1r2
c)

Transition table corresponding to required FA

Old States New states after reading

a b

Final z1 ± ≡ x1 x2 ≡ z3 x1 ≡ z2

Non-final z2 ≡ x1 x2 ≡ z3 x1 ≡ z2

z3 ≡ x2 x2 ≡ z3 ( x3 , x1 ) ≡ z4

z4 + ≡ ( x3 , x1 ) ( x3 , x1 , x2 ) ≡ z5 ( x3 , x1 ) = z4

z5 + ≡ ( x3 , x1 , x2 ) ( x3 , x1 , x2 ) ≡ z5 ( x3 , x1 ) = z4

FA corresponding to r1*
Question No 2: Marks: 4

Convert the following NFA into FA.


Solution:

Transition table corresponding to required FA

Old States New states after reading

a b

z1 ± = 0 (1, 2) ≡ z2 Φ

z2 + ≡ (1, 2) (1, 2,3) ≡ z3 (1,3) ≡ z4

z3 + ≡ (1, 2,3) (1, 2,3) ≡ z3 (1,3) ≡ z4

z4 + ≡ (1,3) (1, 2,3) ≡ z3 Φ

FA corresponding to given NFA

Question No 3: Marks: 4

Build an NFA equivalent to the FA* where


FA

Solution:

NFA equivalent to the FA* where


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:


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.

input

States a b

Output Next State Output Next State

q0 1 q2 0 q1
q1 1 q2 0 q3

q2 0 q3 1 q2

q3 0 q3 0 q3

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:
Solution:

FA1c:

FA2c:

Transition table for FA1c U FA2c

New States After Reading

Old states a b
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 FA2c

FA corresponding to ( FA1c U FA2c)c = FA1 ∩ FA2


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


Solution:

Marking and processing state q0

Processing state q1

Processing state q3
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}.
Solution:

S → bXY|bb

X → aX | bX | Λ

Y → aa | bb
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 ⇒ Ybb S⇒Y

S ⇒ abb S ⇒ XXabb S ⇒ aXY

S ⇒ Xabb S ⇒ aY

S ⇒ abb S ⇒ abb
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
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
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