Professional Documents
Culture Documents
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 :
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[log3(n)] , for a given n.
Solution:
LOGIC:
Hence we can say ceil( log 3n) is nothing but the count of no of times we can keep
dividing by 3 until n becomes 0
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 log3n:
Iteration1 : We perform n / 3 which costs O( n3 )
Iteration2 : We perform (n/3) / 3 which costs O((n/3)3)
.
.
.
After log 3n 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 3n
= O(n^3 log 3n)
5
Q.4 Design a TM for the language L = {a^nb^3n|n ≥ 1}.
Solution :
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
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,
Input = 4n+1
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 :
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(log2a,log2b)
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
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.
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 (n2)
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.
9
Eg:
B B 1 5 8 4 3 x 1 0 2 4 5 y 0 z d d d d d B
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.
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(n2)+O(n)+O(n)
= O(n2)
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:
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 :
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]
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
13
Turing Machine
111110111$
1 1 1 1 1 0 1 1 1 $
Steps:
Start from the right side of 0
1. if 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
= a+[1+2+3+4+5+...........+b+b]
= a+b(b+1)/2 +b
COMPARISON of Models
16