in Algorithms retagged by
17,498 views
7 votes
7 votes
T.C of T(n)=2T(n-1)+n,n > 1 ,T(1)=1 ?
in Algorithms retagged by
by
17.5k views

2 Comments

2^n is a correct answer .

Using substract and conquer method u get O(n2^n) which is correct as

As O(2^n) < O(n2^n)

We can say S&C is giving Worst Case conplexity

Like Quick Sort have O(nlogn) normally & O(n^2) for special case i.e ascending and decreasing sequence..

Can anyone find special case like this for given algo.
0
0
Will this subtract and conquer method gives upper bound or tight bound ?

Because with subtract and conquer we get O(n2^n)

and when we solve we get O(2^n)
0
0

3 Answers

14 votes
14 votes
Best answer

$T(n) = 2T(n-1)+n \\= 2^2T(n-2) + 2(n-1) + n \\=2^3T(n-3) + 2^2(n-2) + 2(n-1) + n \\ .\\. \\.\\=2^{n-1}T(n-(n-1)) + 2^{n-2}(n-(n-2)) + 2^{n-3}(n-(n-3)) + \dots + 2(n-1) + n \\=  2^{n-1}T(1) +  2^{n-2} .2 + 2^{n-3}.3 + \dots + 2(n-1) + n$ $\to (1)$

Now multiply $T(n)$ By 2

$2T(n)=2^n+ 2^{n-1}.2 + 2^{n-2}.3+\dots + 2^2(n-1) + 2n$ $\to (2)$

Now $(2) - (1) \implies \\T(n) =2^n+ 2^{n-1}+ 2^{n-2}+ 2^{n-3}+ \dots +2^2+ 2 -n \\= 2^n +2^{n-1}+ 2^{n-2}+ 2^{n-3}+\dots +2^2+2 -n \\= 2.\frac{(2^{n} -1)}{(2-1)} \text{ (Sum to n terms of GP with a = r = 2) }  -n \\=2^{n+1} -2 - n \\= \Theta\left(2^n\right)$


Alternatively,

$T(1) = 1$
$T(2) = 2.1 + 2 = 4$
$T(3) = 2.4 + 3 = 11$
$T(4) = 2.11 + 4 = 26$
$T(5) = 2.26 + 5 = 57$
$T(6) = 2.57 + 6 = 120$
$\dots$ 
$T(n) = 2^{n+1} - (n+2)$

edited by

4 Comments

what mistake i have done? can you point me !
0
0
yes...why subtract and conquer method get fail here ...???
0
0
Narasimha Karumanchi's book is not a standard reference. People seem to be getting misled by following it.
0
0
3 votes
3 votes

Answer is $\theta(2^n)$

1 comment

When you are subtracting eq7 with eq6, how are you getting - n in the result, shouldn't it be +n? @Prince Sindhiya

1
1
0 votes
0 votes
  1. This is d correct approach

4 Comments

they are asymptotically equal.
0
0
k should be zero.then we get correct result
0
0