Open In App

Algorithms | Recurrences | Set 1

Last Updated : 27 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report
  • Question 1: Which of the following is the value of T3(n) where T3(n) is defined as T3(n) = 5*T3(n-1) – 4*T3(n-2)
    1. C1*5n + C2*4n
    2. C1 + C2*4n
    3. C1*2n + C2*4n
    4. C1*5n + C2*(-4)n
    Answer: 2 Explanation: The recursion function (equation) seems to have a strange form. Let’s change the variable T2(n) to get an equation of a familiar form; so, we let A(n) = T3(n); then we have:

    A(n) = 5*A(n - 1) - 4*A(n - 2)

    The characteristic equation of our new differential equation would be:

    r^{2} - 5*r + 4 = 0   \Rightarrow  (r-4)(r-1) = 0

    So, the homogeneous solution to this equation shall be:

    A(n) = C_{1} *  1^{n} + C_{2} * 4^{n} = C_{1} + C_{2} * 4^{n}

    As we have defined A(n) = T3(n), the final answer is:

    T^{3}(n) = C_{1} + C_{2} *   4^{n}  = C_{1} + C_{2} * 4^{n}

  • Question 2: Determine the value of initial condition F(1) in a way that we can have F(n) = (n+2)! as the solution to the following given recursive function:

    F(n) = (n+1) * F(n-1) + (n+1)!

    1. 3
    2. 4
    3. 6
    4. 2
    Answer: 3 Explanation: Through using the iteration technique to solve the given recursive function, we’ll have:

    F(n) = (n+1) * F(n - 1) + (n+1)! = (n+1) * (n*F(n-2) + n!) + (n+1)! =(n+1)*(n)*F(n-2) + 2* (n+1)!

    If we continue these derivations, we can easily guess that the answer should be of following form:

    F(n) = (n+1)(n)(n - 1)* ... *(n - k + 2)F(n - k) + k * (n+1)!

    The last step (stop point) in iteration method is when we reach the initial condition F(1); therefore, we let k = n-1, and the non-recursive form would be:

    F (n) = (n + 1)*(n)*(n - 1)*...*(3) * F(1) + (n - 1) * (n+1)!

     = (n + 1)*(n)*(n - 1)*...*(3) * (2 * \frac{1}{2}) * F(1) + (n - 1) * (n+1)!

      = (n+1)! * ( \frac{F(1)}{2}) + (n - 1) * (n+1)! = (n+1)! * (\frac{F(1)}{2} + (n - 1))

    In according to the given function F(n) in question, and what we have derived till now:

     F(n) = (n+2)! = (n+1)! * (n+2) = (n+1)! * (\frac{F(1)}{2} + (n - 1))

    Finally, as we shall see, the value of F(1) is:

     F(1) = 2*(n + 2) - 2*(n - 1) = 6

  • Question 3: What is the time Complexity of T(n) = 4* T(n/2) + n * log(n!).
    1. θ(n * log n)
    2. θ(n2)
    3. θ(n2 * log n)
    4. θ(n2 * log2 n)
    Answer: 4 Explanation: We know that log(n!) ∈ θ( n * log n ). Now, the equivalent problem is to analyze the order of the new recursive function:

     T(n) = 4 * T(\frac{n}{2}) + n^{2} * log(n)

    We can solve this by master theorem. In order to apply master theorem here, we have f(n) = n2 * log(n), and the parameters a (the number of sub-problems), b (the reduction factor), and C equal to 4, 2, and 2, respectively; so, θ( nlogba ), is of θ( n2 ) which lies in the same complexity class of θ( nC = 2 ); therefore, the given recursive function is belong to the case 2 of master theorem. In according to master theorem, T(n) would be of following order:

     T(n) \in \theta(n^{2} * log^{2}n )

  • Question 4: Which one gives the best estimation of T(n) complexity?

    T (n) =  3^{log_{3}6} * T(n/2)+ n2n+1.

    1. θ( n2 * √n * log n )
    2. O( n2 * √n+1 * log n )
    3. θ(  n^{log_{2}6} )
    4. θ(  n^{log_{3}6} )
    5. O( n2 * √n ).
    Answer: 3 Explanation: We know that a^{log_{b}C} = C^{log_{b}a}, and n^{2}*\sqrt(n+1) \in \theta (n^{2 + \frac{1}{2}}) \in \theta (n^{\frac{5}{2}}) ; so, we can simplify the recursion function as following:

    T(n) = 3^{log_{3}6} * T(\frac{n}{2}) + n * \sqrt(n+1) = 6* T(\frac{n}{2}) + \theta ( n^{\frac{5}{2}} )

    It belongs to the case 3 of the master theorem; so, the asymptotic complexity of T(n) is:

    T(n) \in \theta ( n^{log_{2}6} )

  • Question 5: Which asymptotic boundary is not correct for T (n) = T (n/4) + T (3n/4) + n ?
    1. O( nlog4/3 2 )
    2. Ω( n )
    3. O( n * log(n) )
    4. None of above
    Answer: 4 Explanation: By master theorem, we can specify the same boundaries as indicated in first two options (A) and (B):
    1. T(n) = T(\frac{n}{4}) + T(\frac{3n}{4}) + n     \leq   2*T(\frac{3n}{4})+n   \Rightarrow  T(n) \in  O (n^{ log_{4/3}2})
    2. T(n) = T(\frac{n}{4}) + T(\frac{3n}{4}) + n      \geq   2*T(\frac{n}{4})+n   \Rightarrow  T(n) \in  \Omega (n)
    We can find out the correctness of third option by the recurrence tree method. We draw a tree and we can easily guess an appropriate boundary for T(n):

    RecurrenceTree_of_T(n)

    The length of the branches of the recurrence tree cannot be less than hr, and neither can be more than hL; so the following estimations can be inferred:
    1. T(n)  \leq n * h_{r} = n * log_{\frac{4}{3}}n  \Rightarrow T(n)  \in O( n*log(n) )
    2. T(n)  \geq n * h_{L} = n * log_{4}n  \Rightarrow T(n)  \in  \Omega ( n*log(n) )
    In according to the two boundaries mentioned above, we also have T(n) ∈ θ( n*log(n) ). We know that if T(n) ∈ O(n * log(n)), it must be also of O(nlog4/3n); so if we have evaluated the third option first, we actually would have inferred the correctness of option (A) too. However, recurrence trees just give idea on how to guess an appropriate boundary. As someone may give a wrong guess, this method also needs verification or proof that it will not violate the definition of the notations in use. This verification (proof) can be obtained inductively by iterative substitutions into T(n), while considering the notation definitions.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads