Design and Analysis of Algorithm
  • Design and Analysis of Algorithm
  • Divide and Conquer Technique
    • Merge Sort
    • Quick sort
    • Maximum Sum Contiguous Subsequence
    • Comparison Between Quick Sort & Merge sort
    • Closest Pair of Points
  • Backtracking
  • Tutorial
    • Week - 1
    • Week - 2
    • Week - 3
    • Week - 4
    • Week - 5
Powered by GitBook
On this page

Was this helpful?

  1. Tutorial

Week - 2

Solutions for Week - 2 Questions

  1. Find the time complexity of the following recurrence relation using forward substitution: T(n)=3T(n−1),n>0 T(n) = 3T(n-1), n>0 T(n)=3T(n−1),n>0T(0)=1 T(0)=1 T(0)=1 T(n)=3T(n−1) T(n) = 3 T(n-1) T(n)=3T(n−1) Soln: Derive the value of T(n) for values starting from 1

    n

    T(n)

    1

    3

    2

    9

    3

    27

    Generic equation : T(i)=3iT(i) = 3^i T(i)=3i For i=n , T(n)=3nT(n) = 3^nT(n)=3n

  2. Solve recurrence T(n)=T(n−1)+logn,n>0;T(1)=0T(n) = T(n-1) + logn , n>0; T(1)=0T(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-2) + log(n-1) +log(n) T(n)=T(n−2)+log(n−1)+log(n) T(n)=T(n−3)+log(n−2)+log(n−1)+log(n) T(n) = T(n-3) + log(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)T(n) = T(n-i) + log(n-i+1) + log(n-i+2) +........log(n)T(n)=T(n−i)+log(n−i+1)+log(n−i+2)+........log(n) Substitute n−i=1 n-i = 1n−i=1 (base condition)

    i=n−1 i=n-1 i=n−1 T(n)=T(1)+log(0)+log(1)+log(2)+...log(n)T(n)=T(1)+log(0)+log(1) +log(2)+...log(n)T(n)=T(1)+log(0)+log(1)+log(2)+...log(n) log(0)log(0)log(0) is ignored T(n)=T(1)+log(1.2.3....n)T(n) = T(1)+log(1.2.3....n)T(n)=T(1)+log(1.2.3....n)

    T(n)=0+log(n!)T(n) = 0 + log(n!) T(n)=0+log(n!) T(n)=nlog(n)T(n) = nlog(n)T(n)=nlog(n)

  3. Use Master theorem to solve the following recurrence relation. (Indicate the case and the solution) For all the cases, consider T(1)=1 T(1) = 1 T(1)=1 a) T(n)=4T(n/2)+n2,n>1T(n) = 4T(n/2) + n^{2} , n>1T(n)=4T(n/2)+n2,n>1 Case 2 : T(n)=n2logn T(n) = n^{2} log nT(n)=n2logn b) T(n)=2nT(n/2)+nn,n>1T(n) = 2^{n} T(n/2) + n^{n}, n>1T(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.51T(n) = 2T(n/4) + n^{0.51}T(n)=2T(n/4)+n0.51 Case 3: n0.51>n0.50n^{0.51} > n^{0.50}n0.51>n0.50 Regularity condition = 2.(n/4)0.51<=c.n0.51 2.(n/4)^{0.51} <= c.n^{0.51} 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 T(n)= 3T(n/3) +(n/2), n>1 T(n)=3T(n/3)+(n/2),n>1 Case 1: nlognn logn nlogn e) T(n)=7T(n/3)+n2,n>1 T(n) = 7 T(n/3) + n^{2}, n>1 T(n)=7T(n/3)+n2,n>1 Case 3: n2n^{2}n2 Regularity condition = 7.(n/3)2<=c.n2 7.(n/3)^{2} <= c.n^{2} 7.(n/3)2<=c.n2 c=(7/9) = 0.7 condition holds

  4. Draw the recursion tree and derive cost for recurrence T(n)=aT(n/b)+f(n),n>1,T(1)=1 T(n) = a T(n/b) + f(n) , n> 1, T(1) = 1T(n)=aT(n/b)+f(n),n>1,T(1)=1 T(n)=f(n)+nlogba+∑i=1logn−1ai(f(n/b))i T(n) = f(n) + n^{log _{b} a} + \sum_{i=1}^{log n-1} a^{i} (f(n/b))^{i}T(n)=f(n)+nlogb​a+∑i=1logn−1​ai(f(n/b))i

  5. 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)

PreviousWeek - 1NextWeek - 3

Last updated 3 years ago

Was this helpful?