GATE | GATE-CS-2004 | Question 90
The recurrence equation
T(1) = 1 T(n) = 2T(n - 1) + n, n ≥ 2
evaluates to
a. 2n + 1– n – 2
b. 2n – n
c. 2n + 1 – 2n – 2
d. 2n + n
(A) a
(B) b
(C) c
(D) d
Answer: (A)
Explanation: If draw recursion tree, we can notice that total work done is,
T(n) = n + 2(n-1) + 4(n-2) + 8(n-3) + 2n-1 * (n – n + 1)
T(n) = n + 2(n-1) + 4(n-2) + 8(n-3) + 2n-1 * 1
To solve this series, let us use our school trick, we multiply T(n) with 2 and subtract after shifting terms.
2*T(n) = 2n + 4(n-1) + 8(n-2) + 16(n-3) + 2n T(n) = n + 2(n-1) + 4(n-2) + 8(n-3) + 2n-1 * 1
We get
2T(n) - T(n) = -n + 2 + 4 + 8 + ..... 2n T(n) = -n + 2n+1 - 2 [Applying GP sum formula for 2, 4, ...] = 2n+1 - 2 - n
Alternate Way to solve is to use hit and try method. Given T(n) = 2T(n-1) + n and T(1) = 1 For n = 2, T(2) = 2T(2-1) + 2 = 2T(1) + 2 = 2.1 + 2 = 4 Now when you will put n = 2 in all options, only 1st option 2^(n+1) - n - 2 satisfies it.
Please Login to comment...