You are on page 1of 16

ADSA Assignment-1 

 
Group - 3​ [SET-3] 
 
COE18B015 Naren 
CED18I009 Bhaskar 
CED18I017 Dhruv   
CED18I018 Dileep 
CED18I022 Haldhar  
CED18I035 Kasi 
CED18I041 Rahul  

Q.1 Construct a DFA for each of these languages. Do you notice any sort
of relationship between the 2 DFAs?
(a) Every string starts and ends with the same symbol.
(b) Every string starts and ends with different symbols.

Solution:
DFA For part a:
L = a(a+b)*a+a + b(a+b)*b+b+𝝴

​DFA - 1
1
DFA For part b:
L=a(a+b)*b + b(a+b)*a

DFA - 2

Inference :

DFA 1 and DFA 2 are complement of each other


I.e the final states of DFA 1 are the non final state of DFA2 and Non final
states of DFA 1 are the final state of DFA 2.

2
Q.2 Design a PDA which will check if a > b, a < b or a = b for 2 given
numbers a and b. In case a != b, then can you get the difference from this
PDA? Clearly mention the input representation.

Solution:

Input Representation:
a - represented as unary 1
b - represented as unary 0
input= 1^a 0^b (first write a number of ‘1’ and then b number of ‘0’ )

PDA​ ​Approach:
1. on seeing 1 push 1 into the stack
2. on seeing 0 and if stack is not empty, pop 1 out of the stack
3. on seeing 0 and if stack is empty, push 0 into the stack
4. on seeing 0 and stack has 0, push 0 into the stack
5. if input read is blank, pop the elements out of the stack and the last element
popped out of the stack is our ​answer(is we get nothing meaning stack is
empty and a=b) ​and if keep the count of the elements popped from the stack
that will be the difference of a and b

3
Q.3 Design a TM for computing ​ceil[log​3​(n)] ​, for a given n.

Solution:
LOGIC:

Hence we can say​ ceil( log 3​n​)​ is nothing but the count of no of times we can keep
dividing by 3​ until ​n becomes 0

For ex we have n=7


In first iteration n=7/3 =>n=2 -------- ​count=1
Second iteration n=2/3 =>n=0 -------- ​count=2
Since we get n=0 we stop
Hence our answer will be​ 2

4
Turing machine implementation :
Logarithm with base 3 of n is computed as repeated divisions by 3. Further, the
divisions are computed using another Turing Machine which does repeated
subtractions.
Note - The cost that is being mentioned below is the number of head moves in the
execution of the Turing machine.
TIme to perform a - b in unary :
O(a^2) assuming a ≥ b
Time to perform a/3 in unary:
To get a/3 from a, we need to subtract 3 from a (2n/9) times.
i.e a - x =​
a
3
⇒ ​x = 2a
3
x 2a
a - 3*(y) = a/3 ⇒ ​ y =​ 3 = 9
2a
Total number of subtractions = 9
Cost of division by 3 :
2a
Total subtractions * cost of subtractions = 9 * O( n2 ) = O​( n3 )
Time to perform log​3​n​:
Iteration1 : We perform n / 3 which costs O( n3 )
Iteration2 : We perform (n/3) / 3 which costs O((n/3)​3​)
.
.
.
After log 3​n​ iterations we incur cost as O(n^3) + O((n/3)^3) + O((n/3^2)^3) + …
<= O(n^3) + O(n^3) + O(n^3) + …
= O(n^3) log 3​n
= O(n^3 log 3​n​)

Complexity of computing log​3​n is O(​n3​ log 3​n​) 

5
Q.4 Design a TM for the language L = {a^nb^3n|n ≥ 1}.

Solution :

Suppose n = 3 , Here we represent a & b using 1


First Iteration,

1 1 1 0 1 1 1 1 1 1 1 1 1 B B

​ into​ Y​ and move right …..
change​ 1 Total step count for right movement == ​n+1

After delimiter convert 3 1’s into X. Total steps in right movement == 2

Y 1 1 0 X X X 1 1 1 1 1 1 B B

Again we have to move left until we encounter Y. Total step count == n+4

After the first iteration , total steps count as: ​(n+1 + 2 + n+4) + 4
We add 4 because we are converting 1 into X and Y .

Second Iteration,

Total Step Count = ​(n+3 + 2 + n+6 ) + 4

Total , we have n iterations so ,total step count is::

(n+1 ) +(n+3) +(n+5) +............ n times = n^2 + 1+3+5+........


2+2+2+.................................... n times = 2n
(n+4) +(n+6) +(n+8) +............. n times = n^2 + 4 + 6+8+ …...
4+4+4+.................................... n times = 4n

Input = 4n+1

Total step count = 2n^2 +.........

So, complexity will be Quadratic in input size.

6
Q5. Analyze the complexity of a mod b, for 2 numbers a and b, by
considering three different input representations. Present an outline
of how TM works avoiding details of transition function.

Solution :

1)Input representation: Unary


The number a is represented by a string of 1’s whose count is a and b is
represented by a string of 1’s whose count is b.
E.g a = 5 => 11111
B = 3 => 111
On the input tape they are separated by delimiter 0.
Example input -
1 1 1 1 1 0 1 1 1 B

Approach in unary: To find a mod b, we subtract b from a till remainder


becomes smaller than b. Then the answer of a mod b is the remainder.We
shall see how this is performed in the Turing machine.
Let r = a mod b
a = xb + r => x = a - r / b
X will be maximum when r = 0
So in the worst case we will need to subtract b from a floor(a/b) times.
a mod b is calculated as a-b,((a-b)-b),(a-3b),...,(a-xb) such that a-xb >= 0.
Time to perform a -b in unary:
O(a​2​) assuming a >= b
Time to find a mod b:
Total number of subtractions* cost of each subtraction
= floor( ba )*O(a​2​)
a
= O(floor( b )a​2 ​)
a
Time Complexity of a mod b = O(floor( b )a​2 ​)

7
2)Input representation: Binary
We will represent a and b using binary.
Let us represent both a and b in input tape with delimiter x.
We have to make both a and b of same size binary strings. Let the size be
n.
Then n=max(log​2​a,log​2​b)
Let us represent carry after input b with delimiter y.
Then after carry with delimiter z we can store the sum (initially n d’s).

Eg:

B B 1 0 1 1 1 x 0 1 1 1 0 y 0 z d d d d d B

We will calculate a mod b by repetitive subtraction.


To calculate subtraction we will use 1’s complement subtraction.
We have to calculate one’s complement.
For calculating 1’s complement we will move n+1 steps until x.
After x until y we will change 0 to 1 and 1 to 0.

Step count of 1’s complement is n+2.


Complexity will be O(n).

B B 1 0 1 1 1 x 1 0 0 0 1 y 0 z d d d d d B

we have to calculate sum and carry for both 0th bits and initial carry 0.
Then we will change the carry bit with carry generated and 0th bit of sum
to sum generated.

Step count for calculating 0th bit sum and carry.


Reading 0th bit of a =n steps
Reading 0th bit on b= n+1 steps
Modifying carry = 2 steps
Changing 0th sum bit = n+1 steps
Total step count = 3n+4

8
Complexity for calculating sum and carry for 1 bit = O(n)
For calculating overall sum complexity = n * O(n)
Complexity of finding sum= O (n​2​)

Based on the final carry generated We have to increment the sum or take
1’s complement (if carry= 0) which can be done with O(n) complexity.

So overall complexity for finding binary difference = O(n​2​)+O(n)+O(n)


= O(n​2​)

We have to subtract b from a floor(a/b) times. We will get to know about it


if the final carry is not generated then that's the last iteration and what is
left in b’s part is a mod b.

So the complexity of a mod b = (a/b)*O(n​2​)


= O((a*n​2​)/b)

In the worst case a is far greater than b.


Then n=log​2​a

Then complexity of a mod b= O( (a*(log​2​a)​2​) / b)


= O( (a*(log a)​2​) /b)

3)Input representation: Decimal


We will represent a and b using decimal representation
Let us represent both a and b in input tape with delimiter x.
We have to make both a and b of same size decimal strings. Let the size
be n.
Then n=max(log​10​a,log​10​b)
Let us represent carry after input b with delimiter y.
Then after carry with delimiter z we can store the sum (initially n d’s).

9
Eg:

B B 1 5 8 4 3 x 1 0 2 4 5 y 0 z d d d d d B

We will calculate a mod b by repetitive subtraction.


To calculate subtraction we will use 9’s complement subtraction.
We have to calculate 9’s complement.
For calculating 9’s complement we will move n+1 steps until x.
After x until y we will change every bit to 9 - that digit.

Step count of 9’s complement is n+2.


Complexity will be O(n).

B B 1 5 8 4 3 x 8 9 7 5 4 y 0 z d d d d d B

we have to calculate sum and carry for both 0th bits and initial carry 0.
Then we will change the carry bit with carry generated and 0th bit of sum
to sum generated.

Step count for calculating 0th bit sum and carry.


Reading 0th bit of a =n steps
Reading 0th bit on b= n+1 steps
Modifying carry = 2 steps
Changing 0th sum bit = n+1 steps
Total step count = 3n+4

Complexity for calculating sum and carry for 1 bit = O(n)


For calculating overall sum complexity = n * O(n)
Complexity of finding sum= O (n​2​)

Based on the final carry generated We have to increment the sum with
carry or take 9’s complement (if carry=0) which can be done with O(n)
complexity.

10
So overall complexity for finding binary difference = O(n​2​)+O(n)+O(n)
= O(n​2​)

We have to subtract b from a floor(a/b) times. We will get to know about it


if the final carry is not generated (i.e 0) then that's the last iteration and
what is left in b’s part is a mod b.

So the complexity of a mod b = (a/b)*O(n​2​)


= O((a*n​2​)/b)

In the worst case a is far greater than b.


Then n=log​10​a

Then complexity of a mod b= O( (a*(log​10​a)​2​) / b)


= O( (a*(log​ a)​

2​
/ b)

11
6. Consider the operation a-b, for 2 given numbers a and b.
Construct a DFA, PDA, TM, RAM model for this operation, if it
exists, clearly defining the input representation in each model.
Compare the computational power of these models by step
count method, clearly defining a ‘step’ in each case.

Solution :
Operation to be formed is ( a-b ) using:

DFA: ​we can not calculate a-b using DFA

PDA

Input Representation:
a - represented as unary 1
b - represented as unary 0
input= 1^a 0^b (first write “a” number of ‘1’ and then “b” number of ‘0’ )

After completely reading the string, we pop all the elements from the stack
and the count of the popped elements gives us the difference of a and b

12
Here the​ Sign bit of the difference is given the PDA​ and the ​magnitude
of the difference is the value of the count​ [no. of the elements left in the
stack]

Example :

Say a=6 b=5


So the input string will be ​1^6 0^5​ i.e 11111100000

On seeing 1 we push 1 on the stack and we do that for all 6 1’s in the
input string

Now on seeing 0 we pop 1 from the stack and we repeat it until all the 0’s
are read
[ if all the 1’s are popped and we reach the bottom of the stack then start
pushing the 0’s into the stack until we finish all 0’s]

Finally the only one 1 will be left in the stack


So we will reach the state ​C(from the PDA)​.from which we can infer that
the difference is ​positive​ and the magnitude of the difference is given by
the no. of elements in the stack.

So the ​difference of a and b [a-b] is +1

Time Complexity :
Input n=a+b elements
Here if comparison and push requires one unit of time respectively
Then
Time to push(all a) = a(push)+a(check if the input is 1 i.e push only if 1 )
=2a

Total pop/ (push for b) +comparisons=b(pop)+b(comparison) =2b

Total time =2*(a+b)=2n

Hence time complexity of the above algo will be O(n)

13
Turing Machine

Input=​unary representation a number of 1’s + 0 + b number of 1’s


Lets say a=5 and b=3

111110111​$

1 1 1 1 1 0 1 1 1 $

Steps:
Start from the right side of 0
1. ​i​f 1 is found to the right of 0 , convert 1 into B and go left then convert first 1 after
0 into B and go right.​ And repeat until there are no 1’s left to the right of 0
2​. convert 0 to B
The remaining 1’s are the difference a-b

1 1 1 1 1 0 B 1 1 $

1 1 1 1 B 0 B 1 1 $

1 1 1 1 B 0 B B 1 $

1 1 1 B B 0 B B 1 $

1 1 1 B B 0 B B 1 $

1 1 1 B B 0 B B B $

1 1 B B B 0 B B B $
14
1 1 B B B B B B B $

The last remaining 1’s are the output of the turing machine i.e 2

Time complexity :
n= a+b+2(total no of elements)

First time, movement towards right and change 1 into B just after the delimiter will
take cost = a+1 .

Then move left and change the nearest 1 into B will take cost = 2

Again, we will move right and change 1 into B will take cost = 3

Again, we will move left and change 1 into B will take cost = 4
Again, we will move right and change 1 into B will take cost = 5

Again, we will move left and change 1 into B will take cost = 6
Again, we will move right and we will not get 1 and if we encounter end of the tape will
take cost = 7

Total cost = a+1+ [2+3+4+5+.........+2b+1]

= a+[1+2+3+4+5+...........+b+b]

= a+b(b+1)/2 +b

Random Access Machine

Input Representation =​ binary representation of input


To represent a and b we need log​2​a , log​2​b bits.
The number of bits are made equal by

Input size = ​log​2​a + log​2​a + 1(for carry) = Θ (​ ​log​2​a )


Approach:
Ram Base 2 is nothing but digital logic.
To subtract b from a, 2’s complement of b is added to a.
Time to perform 2’s complement of b:
15
The steps are:
Complement each bit of b (log​2​a moves)
To complement bits from least significant bit till we find the least significant
Zeroth bit. (log​2​a moves)
​ = Θ (​ ​log​2​a )
Total moves = log​2​a + log​2 a
Time taken for addition:
Number of bit level addition:<= log​2​a = Θ (​ ​log​2​a )
Number of register level writes <= log​2​a (for sum) + log​2​a (for carry)
+ 1(overflow)
= Θ (​ ​log​2​a )
​Total time taken = Θ ( log​2​a ) + Θ ( log​2​a ) + Θ ( log​2​a ) = Θ ( log​2​a )

COMPARISON of Models

Model DFA PDA Turing Machine RAM Model

Complexity Not Possible O(n) O(n​2​) Θ (​ ​log​2​n )

16

You might also like