Analysis of Algorithms (Recurrences)


123
Question 1
What is the value of following recurrence.

T(n) = T(n/4) + T(n/2) + cn^2
T(1) = c
T(0) = 0
Where c is a positive constant
A
O(n^3)
B
O(n^2)
C
O(n^2 Logn)
D
O(nLogn)
Analysis of Algorithms (Recurrences)    
Discuss it


Question 1 Explanation: 
Following is the initial recursion tree for the given recurrence relation.
           cn^2
         /      \
     T(n/4)     T(n/2)
If we further break down the expression T(n/4) and T(n/2), we get following recursion tree.
               cn^2
           /           \      
       c (n^2)/16       c(n^2)/4
      /      \          /     \
  T(n/16)     T(n/8)  T(n/8)    T(n/4) 
Breaking down further gives us following
                 cn^2
            /             \      
       c(n^2)/16           c(n^2)/4
       /      \            /      \
c(n^2)/256  c(n^2)/64  c(n^2)/64    c(n^2)/16
 /    \      /    \    /    \         /    \    
 

To know the value of T(n), we need to calculate sum of tree nodes level by level. If we sum the above tree level by level, we get the following series T(n) = c(n^2 + 5(n^2)/16 + 25(n^2)/256) + .... The above series is geometrical progression with ratio 5/16 To get an upper bound, we can sum the above series for infinite terms. We get the sum as (n^2) / (1 - 5/16) which is O(n^2) Refer following video lecture for more details. http://www.youtube.com/watch?v=whjt_N9uYFI
Question 2
What is the value of following recurrence. T(n) = 5T(n/5) + \sqrt{n}, T(1) = 1, T(0) = 0
A
Theta (n)
B
Theta (n^2)
C
Theta (sqrt(n))
D
Theta (nLogn)
Analysis of Algorithms (Recurrences)    
Discuss it


Question 2 Explanation: 
The given solution can be solved using Master Method. It falls in Case 1.
Question 3
What is the worst case time complexity of following implementation of subset sum problem.
// Returns true if there is a subset of set[] with sun equal to given sum
bool isSubsetSum(int set[], int n, int sum)
{
   // Base Cases
   if (sum == 0)
     return true;
   if (n == 0 && sum != 0)
     return false;
 
   // If last element is greater than sum, then ignore it
   if (set[n-1] > sum)
     return isSubsetSum(set, n-1, sum);
 
   /* else, check if sum can be obtained by any of the following
      (a) including the last element
      (b) excluding the last element   */
   return isSubsetSum(set, n-1, sum) || 
          isSubsetSum(set, n-1, sum-set[n-1]);
}
A
O(n * 2^n)
B
O(n^2)
C
O(n^2 * 2^n)
D
O(2^n)
Analysis of Algorithms (Recurrences)    
Discuss it


Question 3 Explanation: 
Following is the recurrence for given implementation of subset sum problem T(n) = 2T(n-1) + C1 T(0) = C1 Where C1 and C2 are some machine specific constants. The solution of recurrence is O(2^n) We can see it with the help of recurrence tree method
           C1
       /       \
    T(n-1)     T(n-1) 


                    C1
                /       \
              C1           C1
           /     \        /    \
      T(n-2)  T(n-2)   T(n-2)  T(n-2)

                    C1
                /       \
              C1           C1
           /     \        /    \
          C1     C1      C1     C1
        /   \   /  \    /  \   /  \

       
If we sum the above tree level by level, we get the following series
T(n) = C1 + 2C1 + 4C1 + 8C1 + ...
The above series is Geometrical progression and there will be n terms in it.
So T(n) = O(2^n)    
Question 4
Suppose T(n) = 2T(n/2) + n, T(0) = T(1) = 1 Which one of the following is false. ( GATE CS 2005)
a) T(n) = O(n^2)
b) T(n) = \theta(nLogn)
c) T(n) = \Omega(n^2)
d) T(n) = O(nLogn)
A
A
B
B
C
C
D
D
Analysis of Algorithms (Recurrences)    
Discuss it


Question 4 Explanation: 
Question 5
Consider the following recurrence:
gate_2006_51 Which one of the following is true?
(A) T(n) = \theta(loglogn)
(B) T(n) = \theta(logn)
(C) T(n) = \theta(sqrt(n))
(D) T(n) = \theta(n)
A
A
B
B
C
C
D
D
Analysis of Algorithms (Recurrences)    
Discuss it


Question 5 Explanation: 
This question can be solved by first change of variable and then Master Method.
  Let n = 2^m
  T(2^m) = T(2^(m/2)) + 1
  Let T(2^m) =  S(m)
  S(m)  = 2S(m/2) + 1  
Above expression is a binary tree traversal recursion whose time complexity is [Tex]\theta[/Tex](m). You can also prove using Master theorem.
S(m)  = [Tex]\theta[/Tex](m)  
      = [Tex]\theta[/Tex](logn)  /* Since n = 2^m */
Now, let us go back to the original recursive function T(n)
  T(n)  = T(2^m) = S(m)
                 = [Tex]\theta[/Tex](Logn)


Question 6
The running time of an algorithm is represented by the following recurrence relation:
    if  n <= 3  then   T(n) = n
    else T(n) = T(n/3) + cn
Which one of the following represents the time complexity of the algorithm?
(A) \theta(n)
(B) \theta(n log n)
(C) \theta(n^2)
(D) \theta(n^2log n)
A
A
B
B
C
C
D
D
Analysis of Algorithms (Recurrences)    
Discuss it


Question 6 Explanation: 
T(n) = cn + T(n/3)
     = cn + cn/3 + T(n/9)
     = cn + cn/3 + cn/9 + T(n/27)
Taking the sum of infinite GP series. The value of T(n) will
be less than this sum.
T(n) <= cn(1/(1-1/3))
     <= 3cn/2

or we can say 
cn <= T(n) <= 3cn/2
Therefore T(n) = [Tex]\theta[/Tex](n)
This can also be solved using Master Theorem for solving recurrences. The given expression lies in Case 3 of the theorem.
Question 7
The running time of the following algorithm
  Procedure A(n)  
  If n <= 2 return(1) else return A(\lceil \sqrt{n}  \rceil);
is best described by
A
O(n)
B
O(log n)
C
O(1og log n)
D
O(1)
Analysis of Algorithms (Recurrences)    
Discuss it


Question 7 Explanation: 
For explanation, please see question 5 of http://geeksforgeeks.org/?p=4545
Question 8
What is the time complexity of the following recursive function:
int DoSomething (int n) 
{
  if (n <= 2)
    return 1;
  else  
    return (DoSomething (floor(sqrt(n))) + n);
}
(A) \theta(n)
(B) \theta(nlogn)
(C) \theta(logn)
(D) \theta(loglogn)
A
A
B
B
C
C
D
D
Analysis of Algorithms (Recurrences)    
Discuss it


Question 8 Explanation: 
Recursive relation for the DoSomething() is
  T(n) =  T([Tex] \sqrt{n}[/Tex]) + C1 if n > 2  
We have ignored the floor() part as it doesn't matter here if it's a floor or ceiling.
  Let n = 2^m,  T(n) = T(2^m)
  Let T(2^m) =  S(m)

  From the above two, T(n) = S(m)

  S(m) = S(m/2) + C1  /* This is simply binary search recursion*/
  S(m)  = O(logm)      
          = O(loglogn)  /* Since n = 2^m */
  
  Now, let us go back to the original recursive function T(n) 
  T(n)  = S(m) 
          = O(LogLogn)
Question 9
The time complexity of the following C function is (assume n > 0 (GATE CS 2004)
int recursive (mt n)
{
   if (n == 1)
     return (1);
   else
     return (recursive (n-1) + recursive (n-1));
}
A
0(n)
B
0(nlogn)
C
0(n^2)
D
0(2^n)
Analysis of Algorithms (Recurrences)    
Discuss it


Question 9 Explanation: 
Recursive expression for the above program will be.
  T(n) = 2T(n-1) + c
  T(1) = c1.
Let us solve it.
  T(n) = 2(2T(n-2) + c) + c        = 4T(n-2) + 3c
  T(n) = 8T(n-3) + 6c + c          =  8T(n-3) + 7c
  T(n) = 16T(n-4) + 14c + c        =  16T(n-4) + 15c
  ............................................................
  .............................................................
  T(n) = (2^(n-1))T(1) +  (2^(n-1) - 1)c

  T(n) = O(2^n)
Question 10
Consider the following recurrence T(n) = 3T(n/5) + lgn * lgn What is the value of T(n)?
(A) \Theta(n ^ \log_5{3})
(B) \Theta(n ^ \log_3{5})
(c) \Theta(n Log n )
(D) \Theta( Log n )
A
A
B
B
C
C
D
D
Analysis of Algorithms (Recurrences)    
Discuss it


Question 10 Explanation: 
By Case 1 of the Master Method, we have T(n) = Theta(n ^ (log5(3)) ). [^ is for power]
There are 23 questions to complete.
123

Company Wise Coding Practice    Topic Wise Coding Practice