Question 1 |

What is the value of following recurrence.

T(n) = T(n/4) + T(n/2) + cn^2 T(1) = c T(0) = 0Where c is a positive constant

O(n^3) | |

O(n^2) | |

O(n^2 Logn) | |

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) + ,
T(1) = 1,
T(0) = 0

Theta (n) | |

Theta (n^2) | |

Theta (sqrt(n)) | |

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]); }

O(n * 2^n) | |

O(n^2) | |

O(n^2 * 2^n) | |

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) = (nLogn)

c) T(n) = (n^2)

d) T(n) = O(nLogn)

a) T(n) = O(n^2)

b) T(n) = (nLogn)

c) T(n) = (n^2)

d) T(n) = O(nLogn)

A | |

B | |

C | |

D |

**Analysis of Algorithms (Recurrences)**

**Discuss it**

Question 4 Explanation:

See question 4 of http://www.geeksforgeeks.org/data-structures-and-algorithms-set-23/ for explanation.

Question 5 |

Consider the following recurrence:

Which one of the following is true?

(A) T(n) = (loglogn)

(B) T(n) = (logn)

(C) T(n) = (sqrt(n))

(D) T(n) = (n)

Which one of the following is true?

(A) T(n) = (loglogn)

(B) T(n) = (logn)

(C) T(n) = (sqrt(n))

(D) T(n) = (n)

A | |

B | |

C | |

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) + 1Above 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:

(A) (n)

(B) (n log n)

(C) (n^2)

(D) (n^2log n)

Which one of the following represents the time complexity of the algorithm?if n <= 3 then T(n) = n else T(n) = T(n/3) + cn

(A) (n)

(B) (n log n)

(C) (n^2)

(D) (n^2log n)

A | |

B | |

C | |

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();is best described by

O(n) | |

O(log n) | |

O(1og log n) | |

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:

(B) (nlogn)

(C) (logn)

(D) (loglogn)

int DoSomething (int n) { if (n <= 2) return 1; else return (DoSomething (floor(sqrt(n))) + n); }(A) (n)

(B) (nlogn)

(C) (logn)

(D) (loglogn)

A | |

B | |

C | |

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 > 2We 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)); }

0(n) | |

0(nlogn) | |

0(n^2) | |

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)

(B)

(c)

(D)

(A)

(B)

(c)

(D)

A | |

B | |

C | |

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() +
What is the value of recurrence?

(A)

(B)

(B)

(B)

(A)

(B)

(B)

(B)

A | |

B | |

C | |

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

Θ(n) | |

Θ(nLogn) | |

Θ(n*n) | |

Θ(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 |

(m/6) (21m - 39) + 4 | |

(m/6) (4m ^{2} - 3m + 5) | |

(m/2) (m ^{2.5} - 11m + 20) - 5 | |

(m/6) (5m ^{3} - 34m^{2} + 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(2

^{k}) = 3 T(2^{k-1}) + 1, T (1) = 1, is:2 ^{k} | |

(3 ^{k + 1} - 1)/2 | |

3 ^{log}2k | |

2 ^{log}3k |

**Analysis of Algorithms (Recurrences)**

**GATE-CS-2002**

**Discuss it**

Question 14 Explanation:

We have
T (2

^{k}) = 3 T (2^{k-1}) + 1 = 3^{2}T (2^{k-2}) + 1 + 3 = 3^{3}T (2^{k-3}) + 1 + 3 + 9 . . . (k steps of recursion (recursion depth)) = 3^{k}T (2^{k-k}) + (1 + 3 + 9 + 27 + ... + 3^{k-1}) = 3^{k}+ ( ( 3^{k}- 1 ) / 2 ) = ( (2 * 3^{k}) + 3^{k}- 1 )/2 = ( (3 * 3^{k}) - 1 ) / 2 = (3^{k+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)

Θ(nLogn) | |

Θ(n ^{2}) | |

Θ(n) | |

Θ(n ^{2}Logn) |

**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))

Only I | |

Only II | |

Both I and II | |

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 |

P-II, Q-III, R-IV, S-I | |

P-IV, Q-III, R-I, S-II | |

P-III, Q-II, R-IV, S-I | |

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 Log

_{b}a] 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 Log

_{b}a 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) + (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 a

_{1}= 8, a_{n}= 6n^{2}+ 2n + a_{n-1}. Let a_{99}= k x 10^{4}. The value of K is _____ Note : This question was asked as Numerical Answer Type.190 | |

296 | |

198 | |

200 |

**Analysis of Algorithms (Recurrences)**

**GATE-CS-2016 (Set 1)**

**Discuss it**

Question 18 Explanation:

a

_{1}= 8 a_{n}= 6n^{2}+ 2n + a_{n-1}a_{n}= 6[n^{2}+ (n-1)^{2}] + 2[n + (n-1)] + a_{n-2}Continuing the same way till n=2, we get a_{n}= 6[n^{2}+ (n-1)^{2}+ (n-2)^{2}+ ... + (2)^{2}] + 2[n + (n-1) + (n-2) + ... + (2)] + a_{1}a_{n}= 6[n^{2}+ (n-1)^{2}+ (n-2)^{2}+ ... + (2)^{2}] + 2[n + (n-1) + (n-2) + ... + (2)] + 8 a_{n}= 6[n^{2}+ (n-1)^{2}+ (n-2)^{2}+ ... + (2)^{2}] + 2[n + (n-1) + (n-2) + ... + (2)] + 6 + 2 a_{n}= 6[n^{2}+ (n-1)^{2}+ (n-2)^{2}+ ... + (2)^{2}+**1**] + 2[n + (n-1) + (n-2) + ... + (2) +**1**] a_{n}= (n)*(n+1)*(2n+1) + (n)(n+1) = (n)*(n+1)*(2n+2) a_{n}= 2*(n)*(n+1)*(n+1) = 2*(n)*(n+1)^{2}Now, put n=99. a_{99}= 2*(99)*(100)^{2}= 1980000 = K * 10^{4}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 __________2.2 to 2.4 | |

3.2 to 3.4 | |

0 to 1.8 | |

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.
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) , n

^{logb a}= n^{log2 5}(case 1 master theorem) A(n) =n^{logb a }value of log_{2}5 is 2.3219, so, the best option is option a This explanation has been contributed by**Parul Sharma.**Question 20 |

When n = 2

T(n) = √(2) T(n/2) + √n, T(1) = 1

evaluates to :

^{2k}for some k ≥ 0, the recurrence relationT(n) = √(2) T(n/2) + √n, T(1) = 1

evaluates to :

√(n) (log n + 1) | |

√(n) (log n ) | |

√(n) log √(n) | |

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.