Week - 2
Solutions for Week - 2 Questions
Find the time complexity of the following recurrence relation using forward substitution: T(n)=3T(n−1),n>0T(0)=1
T(n)=3T(n−1)
Soln:
Derive the value of T(n) for values starting from 1
Generic equation : T(i)=3i
For i=n , T(n)=3n
Solve recurrence T(n)=T(n−1)+logn,n>0;T(1)=0
Soln:
T(n)=T(n−2)+log(n−1)+log(n) T(n)=T(n−3)+log(n−2)+log(n−1)+log(n)
Generic equation
T(n)=T(n−i)+log(n−i+1)+log(n−i+2)+........log(n)
Substitute n−i=1 (base condition)
i=n−1
T(n)=T(1)+log(0)+log(1)+log(2)+...log(n)
log(0) is ignored
T(n)=T(1)+log(1.2.3....n)
T(n)=0+log(n!)
T(n)=nlog(n)
Use Master theorem to solve the following recurrence relation. (Indicate the case and the solution) For all the cases, consider
T(1)=1
a) T(n)=4T(n/2)+n2,n>1
Case 2 : T(n)=n2logn
b) T(n)=2nT(n/2)+nn,n>1
Master's theorem cannot be applied as a is not constant
c) T(n)=2T(n/4)+n0.51
Case 3: n0.51>n0.50
Regularity condition = 2.(n/4)0.51<=c.n0.51
c=(1/2) = 0.5 condition holds
d) T(n)=3T(n/3)+(n/2),n>1
Case 1: nlogn
e) T(n)=7T(n/3)+n2,n>1
Case 3: n2
Regularity condition = 7.(n/3)2<=c.n2
c=(7/9) = 0.7 condition holds
Draw the recursion tree and derive cost for recurrence T(n)=aT(n/b)+f(n),n>1,T(1)=1
T(n)=f(n)+nlogba+∑i=1logn−1ai(f(n/b))i
Can master theorem be used for recurrence relation which takes a poly-logarithmic number of basic operations?
[ Extended version of masters theorem ]
Yes (Refer to slides in the classroom)