# Analysis of Algorithms (Recurrences)

Please wait while the activity loads.

If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled.

If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled.

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 (m). You can also prove using Master theorem.

S(m) = (m) = (logn) /* Since n = 2^m */Now, let us go back to the original recursive function T(n)

T(n) = T(2^m) = S(m) = (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) = (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() + 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]

There are 28 questions to complete.