You are on page 1of 16

Assignment No.

Deadline
Your assignment must be uploaded/submitted via LMS before the 08, April 2021.

Uploading instructions
Please view the assignment submission process document provided to you by the
Bahria University to upload the assignment.

Rules for Marking


It should be clear that your assignment will not get any credit if:

o The assignment is submitted after due date


o The assignment is copied
o You may use any software for diagram but diagram should be clear.

Objective
The objective of this assignment is to provide hands on experience of:

o Building FAs for certain languages.


o Recursive definition of the language.
o Writing Regular Expression of the language.
Q1. Give regular expression of the language of all words of length 5, that starts and ends
with same letter.
Answer:

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

Q2. Build an FA accepting the language L = {baa} defined over ∑={a,b}.

a a
b
- +

a b b
a, b

a,b

Q.3 Find the regular expression corresponding to this FA. Show all steps. [15]
[ Hint: FA to GTG a GTG to RE.]
Answer: To find RE to this FA, we start eliminating states one by one and when
there remain only two states and one edge, then the label on that edge is called RE.
Strategy can be different for removing states. But the better way is to remove that state
first which is easy to remove so let us start with z4

z2 a
a b
a b
z1 - a z4+ z5+
b
a
b

z3+
111b
11

Step 1: Introduce some new states with null transition to make all initial and final states
as intermediate. So,

z2 a
a b
^ a b
- z1 a z4 z5
b
a
b ^
^ ^
z3 +
Step 2: Remove state z5 b

z2 a
a
^ a
- b b* a
z1 a z4
b
b ^
^ b b*
z3 +
b

Step 3: Add loops on z4

z2 a
a + b b* a
^ a
- z1 a z4
b
b ^
^ b b*
z3 +
b
Step 4: adding edges going from z4 to +

z2 a
a + b b*
^ a a
- z1 a z4
b
b ^
b*
z3 +
b
Step 5: removing z4

z2
^ a
- a(a + b b* a)*b*
z1 a
b
b ^
Step 6: removing z2
z3 +
b

^ aa(a + b b* a)*b*
- z1 ab

ab
b ^
z3 +
b

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

Step 7: adding edges going from z3 to + and adding the loops on z3.

^ aa(a + b b* a)*b*
- z1 ab
b
z3 +
b+ab

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

Step 8: adding edges going from z1 to z3.

^ aa(a + b b* a)*b*
- z1
b+ab

z3 +
b+ab

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

Step 9: removing z3.

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

(b+ab )(b+ab)* [^+aa(a + b b* a)*b* ]

Step 10: adding edges going from z1 to +.

[(b+ab )(b+ab)* [^+aa(a + b b* a)*b* ]] + [ aa(a + bb* a)*b*]


^
- z1
+

Step 11: removing z1

^+[(b+ab )(b+ab)* [^+aa(a + b b* a)*b* ]] + [ aa(a + bb* a)*b*]

Therefore the final RE is


RE = ^+[(b+ab )(b+ab)* [^+aa(a + b b* a)*b* ]] + [ aa(a + bb* a)*b*]

Q.4 Find FA from following NFA. [10]

B
22
a a

A- D

b b

C
3

Solution:
B

a a b
a,b

A- D
a,b Dea
d

b b

C a

Q.5 Find NFA corresponding to (FA1 U FA2). [10]

FA1 a,b
a b
y1- y2+ b y3
a

FA2 b a a,b

a b
- X2 X3
+
X1

Solution :

a b a,b

y1 y2+ b y3
a a

_ b a a,b
b
a b
X1 X2 X3 +

a
Assignment
o Determine a RE corresponding to the following TG.

X3

ab ba

X1- ba X2 b+Λ X5 ab X6+

bb aa

X4

Solution:
Step 1:eliminate X3

abba

X1- ba X2 b+Λ X5 ab X6+

bb aa

X4

Step 2: eliminate X4
abba

X1- ba X2 b+Λ X5 ab X6+

aabb

Step 3: eliminate X5
abba
X1- ba X2 (b+^)ab X6 +

aabb
Step 4: rewrite X2

X1- ba X2 (b+^) ab X6 +

abba+ aabb
Step 5: eliminate X2

X1- X6+
ba(abba+aabb)*(b+^)ab

o Determine the RE corresponding to the following TG.

X1-
a ab

b
a X2 X3

ba b
ba

X4+ aa X5

Solution:
Step1: eliminate X2
X1-
ab

b
aa* ba X3

ba b

X4+ aa X5

Step 2: eliminate X5

X1-
ab

b
aa* ba X3
ba

X4+

aabb*ba

Step 3: eliminate X3
abb*ba

X1- X4+

aabb*ba
aa* ba

X1- aa*ba+abb*ba X4+


aabb*ba

^
X1- aa*ba+abb*ba X4 +

aabb*ba

X1- ( aa*ba+abb*ba)( aabb*ba)* +

o Build an FA accepting to L1UL2 where L1 and L2 are accepted by the


following FA1 & FA2 respectively.

a b

b
X2 X4+
a

FA1 a
X1-
a

b a
X3 X5+

b
b

a a,b
FA2

Y1‫־‬ b Y2 b Y3+
a

Solution:
Reading “a” from Z1= Z2=(X2+Y1), Reading “b” from Z1= Z3(X3+Y2)
Reading “a” from Z2= Z2(X2+Y1), Reading “b” from Z2= Z4(X4+Y2)
Reading “a” from Z3= Z5 (X5+Y1), Reading “b” from Z3= Z6(X3+Y3)
Reading “a” fromZ4 = Z2(X2+Y1), Reading “b” from Z4=Z7 (X4+Y3)
Reading “a” fromZ5 = Z5 (X5+Y1), Reading “b” from Z5=Z3(X3+Y2)
Reading “a” fromZ6 = Z8(X5+Y3), Reading “b” from Z6=Z6(X3+Y3)
Reading “a” from Z7 = Z9(X2+Y3), Reading “b” from Z7= Z7(X4+Y3)
Reading “a” fromZ8 =Z8(X5+Y3), Reading “b” from Z8=Z6(X3+Y3)
Reading “a” from Z9=Z9(X2+Y3), Reading “b” from Z9=Z7(X4+Y3)

States:

-Z1=X1+Y1
Z2= X2+Y1
Z3=X3+Y2
+Z4= X4+Y2
+Z5= X5+Y1
+Z6=X3+Y3
+Z7= X4+Y3
+Z8=X5+Y3
+Z9=X2+Y3
Transition Table:
Old State New State New State
Transition with “a” Transition with “b”
-Z1=X1+Y1 Z2= X2+Y1 Z3=X3+Y2
Z2= X2+Y1 Z2= X2+Y1 +Z4= X4+Y2
Z3=X3+Y2 +Z5= X5+Y1 +Z6=X3+Y3
+Z4= X4+Y2 Z2= X2+Y1 +Z7= X4+Y3
+Z5= X5+Y1 +Z5= X5+Y1 Z3=X3+Y2
+Z6=X3+Y3 +Z8=X5+Y3 +Z6= X3+Y3
+Z7= X4+Y3 +Z9=X2+Y3 +Z7=X4+Y3
+Z8=X5+Y3 +Z8=X5+Y3 +Z6= X3+Y3
+Z9=X2+Y3 +Z9=X2+Y3 +Z7=X4+Y3

a
b

a b b
Z1- Z2 Z4+ Z7+
a
b a
b
b
b
Z3 Z6+ Z8+ Z9+
a
a b
a
a
b
Z5+ a

o Build an FA accepting the L2L1, where L1 and L2 are accepted by FA1 and
FA2 respectively.
FA1
X2

a
a
a,b

X1 a b X4+
-

b
b

X3

FA2
a a a,b

Y1- b Y2 b Y3+

Solution:
Reading “a” from Z1= Z1 (Y1), Reading “b” from Z1= Z2 (Y2)
Reading “a” from Z2= Z2 (Y2), Reading “b” from Z2=Z3 (Y3+X1)
Reading “a” from Z3= Z4 (Y3+X1+X2), Reading “b” from Z3= Z5 (Y3+X1+X3)
Reading “a” fromZ4 = Z6 (Y3+X1+X2+X4), Reading “b” from Z4=Z5 (Y3+X1+X3)
Reading “a” fromZ5 =Z4 (Y3+X1+X2), Reading “b” from Z5=Z7 (Y3+X1+X3+X4)
Reading “a” fromZ6 =Z6 (Y3+X1+X2+X4), Reading “b” from Z6=Z7 (Y3+X1+X3+X4)
Reading “a” from Z7 =Z6 (Y3+X1+X2+X4), Reading “b”from Z7=Z7 (Y3+X1+X3+X4)

States:
-Z1= Y1
Z2=Y2
Z3=Y3+X1
Z4=Y3+X1+X2
Z5=Y3+X1+X3
+Z6=Y3+X1+X2+X4
+Z7=Y3+X1+X3+X4
Transition Table:
Old State New State New State
Transition With “a” Transition With “b”
-Z1= Y1 Z1= Y1 Z2=Y2
Z2=Y2 Z2=Y2 Z3=Y3+X1
Z3=Y3+X1 Z4=Y3+X1+X2 Z5=Y3+X1+X3
Z4=Y3+X1+X2 +Z6=Y3+X1+X2+X4 Z5=Y3+X1+X3
Z5=Y3+X1+X3 Z4=Y3+X1+X2 +Z7=Y3+X1+X3+X4
+Z6=Y3+X1+X2+X4 +Z6=Y3+X1+X2+X4 +Z7=Y3+X1+X3+X4
+Z7=Y3+X1+X3+X4 +Z6=Y3+X1+X2+X4 +Z7=Y3+X1+X3+X4

b b
Z1- Z2 Z3

a
b
a
b
Z4 Z5

a a b

a b
Z6+ Z7+ b

a
o Build an FA accepting L* where L of accepted by the following FA.

a
b a,b

a a b
X1- X2 X3+ X4

b
SOLUTION:
Reading “a” from Z1= Z3(X2), Reading “b” from Z1= Z2(X1)
Reading “a” from Z2= Z3(X2), Reading “b” from Z2= Z2(X1)
Reading “a” from Z3= +Z4(X3+X1), Reading “b” from Z3= Z2(X1)
Reading “a” from Z4= +Z5(X1+X2+X3), Reading “b” from Z4= Z6(X1+X4)
Reading “a” from Z5= +Z5(X1+X2+X3), Reading “b” from Z5= Z6(X1+X4)
Reading “a” from Z6= Z7(X2+X4), Reading “b” from Z6= Z6(X1+X4)
Reading “a” from Z7= +Z8(X1+X3+X4), Reading “b” from Z7= Z6(X1+X4)
Reading “a” from Z8= +Z9(X1+X2+X3+X4), Reading “b” from Z8= Z6(X1+X4)
Reading “a” from Z9= +Z9(X1+X2+X3+X4), Reading “b” from Z9= Z6(X1+X4)

States:
+-Z1=X1
Z2=X1
Z3=X2
+Z4=X3+X1
+Z5=X1+X2+X3
Z6=X1+X4
Z7=X2+X4
+Z8=X1+X3+X4
+Z9=X1+X2+X3+X4

Transition Table:

Old State New State New State


Transition with “a” Transition with “b”
+-Z1=X1 Z3=X2 Z2=X1
Z2=X1 Z3=X2 Z2=X1
Z3=X2 +Z4=X3+X1 Z2=X1
+Z4=X3+X1 +Z5=X1+X2+X3 Z6=X1+X4
+Z5=X1+X2+X3 +Z5=X1+X2+X3 Z6=X1+X4
Z6=X1+X4 Z7=X2+X4 Z6=X1+X4
Z7=X2+X4 +Z8=X1+X3+X4 Z6=X1+X4
+Z8=X1+X3+X4 +Z9=X1+X2+X3+X4 Z6=X1+X4
+Z9=X1+X2+X3+X4 +Z9=X1+X2+X3+X4 Z6=X1+X4

Z1-+ a a a
Z3 Z4+ Z5+

a b
b b
b
Z2
b a
Z6 Z7

b a
b b
b

a Z9+ Z8+
a

Assignment submission instructions:


o You may use any software for diagram but diagram should be clear

You might also like