Tut-2_Solution
Tut-2_Solution
General Instructions: Argue logically. Write it in a manner that explains your logic very
clearly. Do not miss steps in between.
Q1 Solve the recurrence relation of Tower of Hanoi problem using iterative method.
Q2 How many ones does the following procedure print when run with input n? Compute the best bounds
you can: the exact value if possible, a big-Θ expression if you can’t find the exact value, or big-O and
big-Ω bounds if you can’t find a big-Θ expression. [2022-23 Midsem]
Ones(n):
if n = 0 :
print 1
else:
for i = 1 to 2n :
Ones(n − 1)
Solution 2 The recurrence is T (n) = 2n T (n − 1), and T (0) = 1. Thus using iterative method the solution
n n(n+1)
will be T (n) = Πni=1 2i = 2Σi=1 i = 2 2 .
n(n+1) 2 2
Note that this quantitiy can be written as Θ(2 2 ) or 2Θ(n ) but not as Θ(2n ), since this last quantity
is roughly the square of the correct answer and is not bounded by a constant multiple of it.
Q3 Solve the following recurrence problems using substitution method.
1. T (n) = 2T (⌊ n2 ⌋ + 17) + n
Solution 3
Q4 Solve the following recurrence to get the best asymptotic bounds you can on T (n) in each
1. T (n) = T ( 4 logn n ) + 2n for n > 1 and T (1) = 1. You can assume that all numbers are rounded down
2
to their nearest integer. You can use Master’s theorem. [2021-22 Midsem]
2. T (n) ≤ T (⌈ n2 ⌉) + T (⌊ n2 ⌋) + cn log n, for n ≥ 4 and T (2) = 2,(Do not use Master theorem here).
[2021-22 Midsem]
Solution 4
1. We will provide an upper and a lower bound to show that the best asymptotic bound for the
recurrence is Θ(n). A lower bound of Ω(n) follows from the 2n term in T (n).
n n
An upper bound can be reached by observing that 4 log n > 4 for n > 1, so T ( 4(log n) ) ≤ T ( 4 ) and
we have T (n) ≤ T ( n4 ) + 2n which, by Master’s theorem, is O(n). It follows that T (n) ∈ Θ(n).
T (k) ≤ T (⌈ k2 ⌉) + T (⌊ k2 ⌋) + ck log k
≤ c(⌈ k2 ⌉ log2 ⌈ k2 ⌉) + c(⌊ k2 ⌋ log2 ⌊ k2 ⌋) + ck log k
= ck(log2 ⌈ k2 ⌉) + ck log k
≤ ck log k log⌈ k2 ⌉ + ck log k
≤ ck log k(log k − 1) + ck log k
= ck log2 k