You are on page 1of 17

Advanced Data Structures & Algorithms

Assignment no: 1

Group Members
(Group #14)

Shah Anwaar Khalid CED18I048


Animesh Kumar CED18I065
Chogale Rudra Jairaj CED18I012
Preety Banjare COE17B038
Syed Fawaz CED18I052
T Karthikeyan CED18I064

Date: 02/02/2021
Question 1)
Design a TM for the language L = {a^i b^j c^k : i × j = k and i, j, k ≥ 1}

Sol)
The logic for this T.M is very similar to multiplication logic in TM.
Let’s analyze the Time Complexity of this approach with an example & try to generalize the
result:

Consider aaabbcccccc:

For this example: In general:

Iteration 1:
● 3 steps- flip a and reach b ● i steps
● 2 steps - flip b and reach first c ● j steps
● 3 steps- backward to reach next b ● j+1 steps
● j steps
● 2 steps- flip 2nd b & reach 2nd c ● j+1 steps
● 3 steps- backward steps to realize that b’s have been ●
exhausted ● j steps
● 2 steps- to restore b ( for next iteration) ● i+1 steps
● 4 steps- backward to reach next a

Iteration 2: ● i-1 steps


● 2j steps
● 2 steps- flip 2nd a & reach b ● 2j +1 steps
● 4 steps- flip b & reach c ● 2j steps
● 5 steps- backward to reach next b ● 2j + 1 steps
● 4 steps- flip 2nd b & reach 2nd c ●
● 5 steps- backward to realize b’s have been exhausted ● j steps
● 2 steps- to restore b (for next iteration) ● i steps
● 3 steps- backward to reach next a

Iteration 3: ● i-2 steps


● 1 step- flip last a and reach b ● 3j steps
● 6 steps- flip b and reach c ● 3j +1 steps
● 3j steps
● 7 steps- backward to reach next b
● 3j +1 steps
● 6 steps- flip b & reach c ●
● 7 steps- backward to realize all b’s have been exhausted ● j steps
● 2 steps- to recover b

All a’s are done , so we enter verification phase: ● j steps


● ij steps
● 2 steps - turn b to Y
● 6 steps- pass through
● HALT!

Therefore, in general, no. of steps:

[ i + (i-1) + (i-2) + . . .+ (i-(i-1))]


+
[ j + 2j + 3j +...+ ij]
+
[ (j+1) + (2j+1) + (3j+1) +...+ (ij+1) ]
+
[j + 2j + 3j +...+ ij]
+
[ (j+1) + (2j+1) + (3j+1) +...+ (ij+1)]
+
[j + j + j +...+ j] --- i times
+
[ i + (i-1) +...+ (i-(i-3))]
+
j
+
Ij

Let’s analyse the Time Complexity now:

Input Size: i + j + ij = Θ(ij)

Case 1: j= Θ(1)
Input size= theta(i)
T.C = Θ(i^2) , quadratic in input size
Case 2: j= Θ(i)
Input size= Θ(i^2)
T.C= Θ(i^3)-> Θ( (i^2)^(3/2))
Case 3: j= O(i)
Input Size= O(i^2)
T.C = O(i^3) = O( (I^2)^(3/2))

Question 2)
Implement a Turing machine that, given an input number n in binary
representation, increments n by 1.

Sol) Here’s what we need to do:

● Step 1: Move the input pointer to the least significant bit


● Take one’s complement of the bits till the 1st zero (including it)
● If there’s no 0:
○ change the first Blank symbol to 1 and halt!
● Else:
○ Leave the rest of the bits unchanged and halt.

State Diagram:

Time Complexity:
Input size= log2(n) --given number n, we have to represent it in binary.

The Time Complexity will be Θ(log2(n)) as in the best as well as the worst case, we’re traversing
the input bits twice( one forward pass and one backward pass)

Question 3)
Give a PDA that accepts exactly the strings over {a, b} that contain the
same number of a 0 s and b 0 s. For example, aabbaabbab is accepted but
aba is not. Also bbbbaaaa and aaaabbbb are both accepted.
Sol) Here’s what we need to do:
● We’ll push the first symbol onto the stack.
● Thereafter, if the top of stack and current symbol is the same we’ll push. Otherwise, we’ll
pop.
State Diagram of PDA:

Question 4)
Outline the logic for constructing a Turing machine that computes/prints
x^2 prime numbers, given an integer x. That is, if x = 3, TM will output 9
prime numbers. Mention the input size and the complexity (poly/exp in
input size) of the logic in unary representation.

Input representation : n represented in unary followed by a dollar 


Input size : n + 1 = Θ(n)
Number of prime numbers needed = n^2
Lets take an example 
111$BBBBBBBBBB…
n is converted to be represented as n x n using O(n^2) computations
1110111$BBBBBBB…
multiplication is carried out in O(n^2) computations
111111111$BBBBBB….
first B followed by $ is converted to A in O(n^2) computations [single time operation]
111111111$​A​BBBBBBBBB...

Iteration:-1
first B is converted to A
111111111$​AA​BBBBBBBB…
[since primality checking is done in bigoh(n^2) for TM]
Checking whether AA(two) is prime or not using k(2^2) operations where k is some
constant

If two comes out to be prime, the last A is changed to P, R/W pointer finds the leftmost 1
and changes it to D
Else R/W pointer simply goes to $
D​11111111$​AP​BBBBBBBB…

Iteration:-2
first B is converted to A
D​11111111$​APA​BBBBBBB…
Checking whether APA(three) is prime or not using k(3^2) operations where k is some
constant

DD​1111111$​APP​BBBBBBBB….

Iteration:-3
first B is converted to A
DD​1111111$​APPA​BBBBBBB….

Checking whether APPA(four) is prime or not using k(4^2) operations where k is some
constant
DD​1111111$​APPA​BBBBBBB….

.
.
.

Halt when no more 1s are there

Since we know that the nth prime number pn can be approximated as(asymptotically) :
n * ln(n) + n * (ln(ln(n))−1) < pn < n * ln(n) + n * ln(ln(n)) f or n≥6
(Source: I​ s there a known mathematical equation to find the nth prime?​ See answer by
ShreevatsaR)

thus, the nth prime number pn is approximately O(nln(n))


(n^2)th prime number will be approximately q(n^2)ln(n), where q is some constant

cost incurred in iterations:


(n^2 + k(2^2)) [ n^2 to change the leftmost 1 to D and k(2^2) for primality checking ]
+
(n^2 + k(3^2))
+
(k(4^2))
+
(n^2 + k(5^2))
+
(k(6^2))
+
(n^2 + k(7^2))
+
(k(8^2))
+
(k(9^2))
+
.
.
.
+
(n^2 + k(p)^2 )

(where p = (n^2)th prime number = q*n^2*ln(n) )


= (n^2) * (n^2) // only for n^2 prime numbers we go back to the MSB of 1s
+ k(2^2 + 3^2 + 4^2 + … + [p]^2)
<= (n^4) + k*p*(p+1)*(2p+1)/6
<= (n^4) + Θ(p^3)
<= (n^4) + K*(n^6)*((ln(n))^3) where K = k*(q^3) is also a constant
= O(n^6*(ln(n))^3)
Polynomial​ in input size
Question 5)
Is it possible to compute a/b for a given a, b in PDA/TM/RAM ? Analyze its
complexity.

[Assumption]: a = max(a, b) and b = min(a, b) where both a and b are positive integers for all
the mathematical models below.

We can implement integer division using repeated subtraction technique.


a/b is given by the number of subtractions possible till we reach zero by doing repeated
subtraction.
((( .. (a - b) - b)-b)...

PDA:
a - b be can be implemented as:
Input representation: unary : 11111(a times) 0 1111(b times)$BBBBBB...
Mathematical model: PDA

So, we push 1’s for every 1 present in a and pop for every 1 present in b. Final result a-b
is stored in the stack. Since the Read head is restrained to move right and cannot move
to the left, it is not possible to subtract ‘b’ multiple times from the result stored in the
stack. Since repeated subtraction is impossible => Integer division a/b is impossible.

TM:
a / b be can be implemented as:
Input representation: unary : 11111(a times) 0 1111(b times)$BBBBBB...
input size: 2a + 2b + 2 = Θ(a)
Mathematical model: TM
Using a-b subtraction as a subroutine it is possible to do integer division in TM.
a-b subroutine will be executed a/b times.
● Case1: (b = Θ(1)): TC of (a-b) = Θ(a)
TC of (a/b) = Θ(a) * (a / Θ(1)) = Θ(a^2)
● Case2: (b = Θ(a)): TC of (a-b) = Θ(a^2)
TC of (a/b) = Θ(a^2) * (a / Θ(a)) = Θ(a^2)
● Case3: (b = O(a)): TC of (a-b) = O(a^2)
TC of (a/b) = O(a^2) * (a / O(a)) <= O(a^2) * (a) = O(a^3)
(Since input size is Θ(a), these complexities are based upon the input size and not upon the
magnitude)

*************************************************************************************************************
Turing Machine for division is shown below:

The high level description of the above T.M is given below:


● The T.M is essentially repeating subtraction as a subroutine a/b times
● For each (a-b) step , a blank is converted to 1 on right hand side

Trace & Analysis for 9 / 3 using the above T.M:


Input tape : $ 1111111110111$BBBBBBBBBBBBB

For 9/3 In general

Iteration 1:
● 10 steps - to reach first 1 in b (and flip ● a + 1 steps
it to X)
● 2 steps - backward to reach ● b-1 steps
corresponding 1 in a (and flip it to Y)
● 3 steps- forward to reach next 1 in ● b steps
b(and flip it to X)
● 4 steps- backward to reach ● b + 1 steps
corresponding 1 in a (and flip to Y)
● 5 steps- forward for next 1 in b ● b+2 steps
● 6 steps - backward for next 1 in a ● 2b steps
● 8 steps - forward to realize b has been ● 2b + iteration_no + 1 steps
exhausted and turn the first B after $
to 1
● 6 steps - to restore b ● b+2 + iteration_no steps

Iteration 2:
● 5 steps - backward after flipping the ● 2b - 1 steps
first 1 in b
● 6 steps- forward for next 1 in b ● 2b steps
● 7 steps - backward for next 1 in a ● 2b + 1 steps
● 8 steps- forward for next 1 in b ● 2b +2 steps
● 9 steps - backward for next 1 in a ● 3b steps
● 12 steps- forward to realize all b’s ● 3b + iteration_no + 1 steps
have been exhausted and to turn the
next blank to 1
● 7 steps to restore b ● b + 2 + iteration_no steps

Iteration 3:
● 8 steps - backward ● 3b -1 steps
● 9 steps- forward ● 3b steps
● 10 steps- backward ● 3b+1 steps
● 11 steps- forward ● 3b +2 steps
● 12 steps- backward ● 4b steps
● 16 steps- forward and to turn the next ● 4b + iteration_no + 1 steps
blank to 1
● 8 steps to restore b ● b +2 + iteration_no steps

Concluding Step:
● 11 steps to realize a has been ● a +2
exhausted
● HALT!

Note: We could have done some cleaning up


of the tape ( turn a, b & 0 to $) but we skipped
that part.

Therefore, step count in general :

[ a + 1] +
[ (b-1) + (2b -1) +...+ ((a/b)(b) -1)] +
[b + 2b +...+ (a/b)b] +
[ (b+1) + (b+2) +...+ ((a/b)b + 1)] +
+....+ [2b + 3b +...+ ((a/b +1)(b) ) ] +
[2b + 3b +...+ ((a/b +1)(b) ) ] +
[ 1+ 2 +...+ a/b] +
[ 1 + 1 +...+1] a/b times +
[ 2+ 2+...+2] a/b times +
[1 + 2 +…+ (a/b) ]

Time Complexity Analysis:


Input Size: a + b + 2 ( $ & 0 ) + a/b ( Blanks)

Case 1: b= Θ(1)
Input size - Θ(a)
T.C- Θ( a^2)

Case 2: b= Θ(a)
Input Size- Θ(a)
T.C- Θ(a^2)

Case 3: b= O(a)
Input Size= O(a)
T.C- O(a^3)

Division in T.M is polynomial in i/p size

*************************************************************************************************************

RAM Model:

Base 2:
a / b be can be implemented as:
Input representation: binary : log 2 a bits of a and log 2 b bits of b are given as input.
[Assumption]: 0 bits are padded to the left of b, so that log 2 a and log 2 b are the same.
Also a = max(a,b). Thus input size: log 2 a
Mathematical model: RAM (base 2)
Using a-b subtraction as a subroutine it is possible to do integer division in RAM.
Complexity of a-b = Θ(log 2 a)
● Case1: (b = Θ(1)): TC of (a-b) = Θ(log 2 a)
TC of (a/b) = Θ(log 2 a) * (a / Θ(1)) = Θ(a.log 2 a) = Θ(2log2 a .log 2 a) (exponential in
input size)
● Case2: (b = Θ(a)): TC of (a-b) = Θ(log 2 a)
TC of (a/b) = Θ(log 2 a) * (a / Θ(a)) = Θ(log 2 a) (linear in input size)
● Case3: (b = O(a)): TC of (a-b) = Θ(log 2 a)
TC of (a/b) = Θ(log 2 a) *(a / O(a)) <= Θ(log 2 a) * (a) = O( a * log 2 a ) =
O(2log2 a .log 2 a) (exponential in input size)

Base 10:
Input representation: binary : log 10 a bits of a and log 10 b bits of b are given as input.
Mathematical model: RAM (base 10)

Method1: Repeated Subtraction


Cost of (a-b) = Θ(1) in all cases
Repeated subtraction for a/b times, will result in cost = a/b * Θ(1)
● Case1: (b = Θ(1)):
TC of (a/b) = Θ(1) * (a / Θ(1)) = Θ(a) = Θ(10log10 a ) (exponential in input size)
● Case2: (b = Θ(a)):
TC of (a/b) = Θ(1) * (a / Θ(a)) = Θ(1) (constant in input size)
● Case3: (b = O(a)):
TC of (a/b) = Θ(1) * (a / O(a)) <= Θ(1) * (a) = O(a) = O(10log10 a ) (exponential in
input size)

Method2: Inbuilt integer division operator


a / b is a constant time operation in all the cases since the RAM base 10 model resembles the
high level language programming model. Operations like integer division are considered
constant time operations.
Thus under all cases: c = a / b
Time required to fetch the value of a = Θ (1)
Time required to fetch the value of b = Θ (1)
Time required to calculate a/b = Θ (1)
Time required to write-back the result in c = Θ (1)
Therefore overall TC = Θ (1)

Question 6) Θ(nlogn) solution


Show that {0^k 1^k | k ≥ 0} ∈ O(n log n). Describe a TM and give an
asymptotic analysis).

Input representation: 0000.0(k times) 1111.11(k times)$BBBBBB…..


Mathematical model: TM
Logic:
● Step1: Check if sequence of 0s comes before 1s
● Step2: Replace every 2nd 0 with x starting from MSB of 0^k
● Step3: Replace every 2nd 1 with x starting from MSB of 1^k, corresponding to each 0
that was replaced in step1
● Step4: (Check if the #0s + #1s) have same parity
● if (only one 1 AND only one 0) does not remain, iterate again from step2.

Complexity analysis:
Given input: 0^k 1^k

We need 1 more iteration at the end to make sure only one 0 and one 1 remains.
So, total number of iterations = log 2 k
Time complexity of one iteration = Θ(k)
Time complexity of completing all iterations = Θ(k.log 2 k)
Input size: n = 2k + 1 = Θ(k)
therefore time complexity in terms of input size ‘n’ = Θ(n.log 2 n)

TM Description:
The state diagram for T.M that accepts a^n b^n ( n>=0) in Θ (nlogn) is given below.

Here’s the reasoning behind the construction of the above T.M:


● We need an algorithm that takes Θ ( n * log n ) steps.
○ From this, we can deduce that we need O(log n ) iterations and O(n) steps in
each iteration.
■ Which implies somehow we need to be able to divide no. of a’s (as well
as b’s) into half at every iteration. In other words, each iteration should
involve flipping half the number of a’s (and b’s) currently on the tape.
■ From this, we decide that we’ll flip every other a ( and b ) in each iteration.
● However, there’s a catch here:
○For each iteration, we also need to check that we passed
through even (or odd) no. of a’s and even (or odd) no. of
b’s.
○ So, if parity of a and b are different in any iteration- we
reject the language by entering into reject state ( refer state
diagram above)
○ And if parity of a & b are the same, we apply the same
logic right to left.
■ We repeat these steps until all a’s & b’s have been exhausted.
● In which case, the turing machine halts at a final state.
● Further, note that the above T.M is decidable; i.e it will always halt.

*************************************************************************************************************

Question 6) Θ (n) Solution


Here we use a TM with two tapes having R/W head of their own. There are only two states q0
and qf.

Scans the entire tape at once, switching the heads = Θ (1) time
Thus overall complexity = Θ (k) = Θ (n)

*************************************************************************************************************

Question 6) Θ (n^2) Solution


Show that {0^k 1^k | k ≥ 0} ∈ O(n log n). Describe a TM and give an
asymptotic analysis).

The idea is very similar to subtraction.


Input representation: 0000.0(k times) 1111.11(k times)$BBBBBB…..
Mathematical model: TM
State Diagram:

I. First step: Go to the left most 1, flip it to x


II. iterate:
A. Go to the right most 0, flip it to y
B. Go to the left most 1, flip it to x
C. halt when all 1’s are exhausted and you read $ symbol marking the end of string.

INPUT: 00001111$BBBB… input size = 2k + 1 = n


#steps(in example) #steps(in general)

First step: 4 k

Iteration1: A. 1 .
B. 2

Iteration2: A. 3 .
B. 4

Iteration3: A. 5 .
B. 6

Iteration4: A. 7
B. 8

Total 1+2+3+…+8 1 + 2 + 3 + … + 2k

= k + (1 + 2 + 3 + … + 2k) = 2k(k+1) = Θ (n^2)

__________________________________END______________________________________

You might also like