📜  门| GATE-CS-2004 |第90章

📅  最后修改于: 2021-06-29 11:24:09             🧑  作者: Mango

递归方程

T(1) = 1
T(n) = 2T(n - 1) + n, n ≥ 2 

评估为
(A) 2 n +1 – n – 2
(B) 2 n – n
(C) 2 n + 1 – 2n – 2
(D) 2 n + n答案: (A)
说明:如果绘制递归树,我们可以注意到完成的总工作量是,
T(n)= n + 2(n-1)+ 4(n-2)+ 8(n-3)+ 2 n-1 *(n – n + 1)
T(n)= n + 2(n-1)+ 4(n-2)+ 8(n-3)+ 2 n-1 * 1

为了解决这个系列,让我们使用学校的技巧,我们将T(n)乘以2,并在移位项后减去。

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

我们得到

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. 

这个问题的测验