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]
Question 11
Consider the following recurrence. T(n) = T(\sqrt{n}) + \Theta(Log Log n) What is the value of recurrence?
(A) \Theta( (Log Log n) ^ 2)
(B) \Theta(Log Log n)
(B) \Theta(n)
(B) \Theta(Log Log Log n)
A
A
B
B
C
C
D
D
Analysis of Algorithms (Recurrences)    
Discuss it


Question 11 Explanation: 
 Change of variables: let m = lg n. Recurrence becomes S(m) = S(m/2) + theta(lgm). Case 2 of master’s theorem applies, so T(n) = theta( (log Log n) ^ 2).
Question 12
Which one of the following correctly determines the solution of the recurrence relation with T(1) = 1?
T(n) = 2T(n/2) + Logn 
A
Θ(n)
B
Θ(nLogn)
C
Θ(n*n)
D
Θ(log n)
Analysis of Algorithms (Recurrences)    GATE-CS-2014-(Set-2)    
Discuss it


Question 12 Explanation: 
 T(n) = 2T(n/2) + log n
 T(1) = 1
Substitute n = 2^k

T(2^k)  = k + 2T(2^(k-1))
T(2^k)  = k + 2(k-1) + 4T(2^(k-2))
        = k + 2(k-1) + 4(K-2) + 8T(2^(k-3))
        = k + 2(k-1) + 4(K-2) + 8(k-3) + 16T(2^(k-4))
        = k + 2(k-1) + 4(K-2) + 8(k-3) + ...... + 2^kT(2^(k-k))
        = k + 2(k-1) + 4(K-2) + 8(k-3) + .......+ 2^kT(1)
        = k + 2(k-1) + 4(K-2) + 8(k-3) + .......+ 2^k  --------(1)

2T(2^k) =     2k     + 4(k-1) + 8(K-2) + ...... + 2*2^k + 2^(k+1) --------(2)

Subtracting 1 from 2, we get below
T(2^k) = - k + 2 + 4 ......    2^(k-2) + 2^(k-1) + 2^k + 2^(k+1)
           = - k + 2 * (1 + 2 + 4 + ..... 2^k)
           = -k + [2*(2^k - 1)] / [2-1]
           = -k + [2*(2^k - 1)]

T(n) = -Logn + 2*(n - 1)

T(n)  = Θ(n) 
Question 13
Consider the following recurrence relation GATECS2003Q35 The value of T(m2) for m ≥ 1 is
A
(m/6) (21m - 39) + 4
B
(m/6) (4m2 - 3m + 5)
C
(m/2) (m2.5 - 11m + 20) - 5
D
(m/6) (5m3 - 34m2 + 137m - 104) + (5/6)
Analysis of Algorithms (Recurrences)    GATE-CS-2003    
Discuss it


Question 13 Explanation: 
One easy way to solve this is to try putting different 
values of m.

For example, we know T(1) = 1. If we put m = 1, only A
and B satisfy the result.

m = 2

T(2) = T(1) + 1 = 2
T(3) = T(2) + 1 = 3
T(4) = T(3) + 2 = 5

Both A & B produce 5

m = 3
T(9) = T(4) + 2*5 + 1 = 5 + 10 + 1 = 16
Both A & B produce 16

m = 4
T(16) = T(9) + 3*7 + 1 = 16 + 21 + 1 = 38
Only B produces 38, A produces 34 which doesn't match 
Question 14
The solution to the recurrence equation T(2k) = 3 T(2k-1) + 1, T (1) = 1, is:
A
2k
B
(3k + 1 - 1)/2
C
3log2k
D
2log3k
Analysis of Algorithms (Recurrences)    GATE-CS-2002    
Discuss it


Question 14 Explanation: 
We have T (2k) = 3  T (2k-1) + 1 = 32  T (2k-2) + 1 + 3 = 33  T (2k-3) + 1 + 3 + 9 . . . (k steps of recursion (recursion depth)) = 3k  T (2k-k) + (1 + 3 + 9 + 27 + ... + 3k-1) = 3k + ( ( 3k - 1 ) / 2 ) = ( (2 * 3k) + 3k - 1 )/2 = ( (3 * 3k) - 1 ) / 2 = (3k+1 - 1) / 2 Hence, B is the correct choice.   Please comment below if you find anything wrong in the above post.
Question 15
Select the correct asymptotic complexity of an algorithm with runtime T(n, n) where
T(x, c) = Θ(x) for c <= 2,
T(c, y) = Θ(y) for c <= 2, and
T(x, y) = Θ(x+y) + T(x/2, y/2) 
A
Θ(nLogn)
B
Θ(n2)
C
Θ(n)
D
Θ(n2Logn)
Analysis of Algorithms (Recurrences)    GATE-CS-2015 (Mock Test)    
Discuss it


Question 15 Explanation: 
The recurrence is
T(x, y) = Θ(x+y) + T(x/2, y/2) 
It can be written as below.
T(x, y) = Θ(x+y) + Θ((x+y)/2) + Θ((x+y)/4) +  Θ((x+y)/8)  .....
T(x, y) = Θ((x+y) + (x+y)/2 + (x+y)/4 + (x+y)/8  ..... )
The above expression forms a geometric series with ratio as 2 and starting element as (x+y)/2 T(x, y) is upper bounded by Θ(x+y) as sum of infinite series is 2(x+y). It is lower bounded by (x+y) Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/assignments/MIT6_006F11_ps1.pdf
Question 16
Let f(n) = n and g(n) = n(1+sin n), where n is a positive integer. Which of the following statements is/are correct?
I.  f(n) = O(g(n))
II. f(n) = Ω(g(n))  
A
Only I
B
Only II
C
Both I and II
D
Neither I nor II
Analysis of Algorithms (Recurrences)    GATE-CS-2015 (Set 3)    
Discuss it


Question 16 Explanation: 
The value of sine function varies from -1 to 1.  

For sin = -1 or any other negative value, I becomes false.

For sin = 1 or any other negative value, II becomes false
Question 17
Consider a list of recursive algorithms and a list of recurrence relations as shown below. Each recurrence relation corresponds to exactly one algorithm and is used to derive the time complexity of the algorithm.
Recursive Algorithm Recurrence Relation
P. Binary search I. T(n) = T(n-k) + T(k) + cn
Q. Merge sort II. T(n) = 2T(n-1) + 1
R. Quick sort III. T(n) = 2T(n/2) + cn
S. Tower of Hanoi IV. T(n) = T(n/2) + 1
A
P-II, Q-III, R-IV, S-I
B
P-IV, Q-III, R-I, S-II
C
P-III, Q-II, R-IV, S-I
D
P-IV, Q-II, R-I, S-III
Analysis of Algorithms (Recurrences)    GATE-IT-2004    QuickSort    
Discuss it


Question 17 Explanation: 
These are examples of some standard algorithms whose  Merge Sort:    T(n) = 2T(n/2) + Θ(n). It falls in case 2 as c is 1 and Logba] is also 1 and  the solution is Θ(n Logn) //time complexity can be evaluated using Master Method Binary Search: T(n) = T(n/2) + Θ(1). It also falls in case 2 as c is 0 and Logba is also 0 and the solution is Θ(Logn) //time complexity can be evaluated using Master Method Quick Sort : Time taken by QuickSort in general can be written as  T(n) = T(k) + T(n-k-1) + \theta(n) Tower of Hanoi : T(n) = 2T(n-1) + 1 Read More at : http://www.geeksforgeeks.org/analysis-algorithm-set-4-master-method-solving-recurrences/
Question 18
Consider the recurrence relation a1 = 8, an = 6n2 + 2n + an-1. Let a99 = k x 104. The value of K is _____   Note : This question was asked as Numerical Answer Type.
A
190
B
296
C
198
D
200
Analysis of Algorithms (Recurrences)    GATE-CS-2016 (Set 1)    
Discuss it


Question 18 Explanation: 
a1 = 8 an = 6n2 + 2n + an-1   an = 6[n2 + (n-1)2] + 2[n + (n-1)] + an-2   Continuing the same way till n=2, we get an = 6[n2 + (n-1)2 + (n-2)2 + ... + (2)2] + 2[n + (n-1) + (n-2) + ... + (2)] + a1   an = 6[n2 + (n-1)2 + (n-2)2 + ... + (2)2] + 2[n + (n-1) + (n-2) + ... + (2)] + 8   an = 6[n2 + (n-1)2 + (n-2)2 + ... + (2)2] + 2[n + (n-1) + (n-2) + ... + (2)] + 6 + 2   an = 6[n2 + (n-1)2 + (n-2)2 + ... + (2)2 + 1] + 2[n + (n-1) + (n-2) + ... + (2) + 1]   an = (n)*(n+1)*(2n+1) + (n)(n+1) = (n)*(n+1)*(2n+2)   an = 2*(n)*(n+1)*(n+1) = 2*(n)*(n+1)2   Now, put n=99. a99 = 2*(99)*(100)2 = 1980000 = K * 104 Therefore, K = 198.   Thus, C is the correct choice.
Question 19
The given diagram shows the flowchart for a recursive function A(n). Assume that all statements, except for the recursive calls, have O(1) time complexity. If the worst case time complexity of this function is O(nα), then the least possible value (accurate up to two decimal positions) of α is __________ z9
A
2.2 to 2.4
B
3.2 to 3.4
C
0 to 1.8
D
1
Analysis of Algorithms (Recurrences)    GATE-CS-2016 (Set 2)    
Discuss it


Question 19 Explanation: 
The time complexity of a recurrence relation is the worst case time complexity. First we have to find out the number of function calls in worst case from the flow chart. The worst case will be when all the conditions (diamond boxes) turn out towards non-returning paths taking the longest root. In the longest path we have 5 function calls. complexity So the recurrence relation will be – A(n) = 5A(n/2) + O(1) (O(1) because rest of the statements have O(1) complexity.) Solving this recurrence relation using Master theorem – a = 5 , b= 2 , f(n) =O(1) , nlogb a= nlog2 5 (case 1 master theorem) A(n) =nlogb a value of log 25 is 2.3219, so, the best option is option a This explanation has been contributed by Parul Sharma.
Question 20
When n = 22k for some k ≥ 0, the recurrence relation
T(n) = √(2) T(n/2) + √n, T(1) = 1
evaluates to :
A
√(n) (log n + 1)
B
√(n) (log n )
C
√(n) log √(n)
D
n log √(n)
Analysis of Algorithms (Recurrences)    Gate IT 2008    
Discuss it


Question 20 Explanation: 
Please note that the question is asking about exact solution. Master theorem provides results in the form of asymptotic notations. So we can't apply Master theorem here. We can solve this recurrence using simple expansion or recurrence tree method.
T(n) = √(2) T(n/2) + √n
     = √(2) [√(2) T(n/4) + √(n/2) ] + √n
     = 2 T(n/4) + √2 √(n/2) +√n
     = 2[ √2 T(n/8) + √(n/4) ]+√2 √(n/2)+√n
     = √(2^3) T(n/8)+ 2 √(n/4) + √2 √(n/2) +√n
     = √(2^3) T(n/8)+√n +√n +√n
     = √(2^3) T(n/(2^3))+3√n
     .............................................
     = √(2^k) T(n/(2^k))+k√n
     = √(2^logn) + logn √n
     = √n + logn √n
     = √n(logn +1)
There are 20 questions to complete.

GATE CS Corner


See Placement Course for placement preparation, GATE Corner for GATE CS Preparation and Quiz Corner for all Quizzes on GeeksQuiz.