What is the asymptotic complexity of T(n) = 2T(n-1) + O(n)? I guess it's solved using substitution method..How to solve this recurrence? You can assume that the algorithm stops when it reaches T(1).
The master theorem can't be used here because the size shrinkage factor b (=(n-1)/n) is basically equal to 1.
However, if you calculate the first few terms, then you can easily see that the time complexity is 2**n:
T(1) = 1 = 1
T(2) = 2*1 + O(n) = 2 + O(n)
T(3) = 2*(2*1 + O(n)) + O(n) = 2**2 + 3O(n)
T(4) = 2*(2*(2*1 + O(n)) + O(n)) + O(n) = 2**3 + 7O(n)
: : : : : : : : :
T(n) = 2**(n-1) + O(n) * (2**(n-1)-1) ≈ O(2**n)
There's a trick to the substitution method. If you try the straightforward approach, you'll get
T(n) <=? 2^n
T(n) = 2 T(n-1) + cn
<= 2^(n-1) + 2^(n-1) + cn
= 2^n + cn,
which is not less than or equal to 2^n. The solution is not intuitive: subtract a low-order term. Omitting some fiddling to get the right one,
T(n) <=? d 2^n - cn - 2c
T(n) = 2 T(n-1) + cn
<= 2 (d 2^(n-1) - c (n-1) - 2c) + cn
= d 2^n - cn - 2c,
and set d to cover the base case. (Given that you want big-O, you don't even need to guess the proper term very accurately.)
Related
Can anybody please explain the time complexity of T(n)=2T(n/4)+O(1) using recurrence tree? I saw somewhere it says O(n^1/2).
Just expand the equation for some iteration, and use the mathematical induction to prove the observed pattern:
T(n) = 2T(n/4) + 1 = 2(2T(n/4^2) + 1) + 1 = 2^2 T(n/4^2) + 2 + 1
Hence:
T(n) = 1 + 2 + 2^2 + ... + 2^k = 2^(k+1) - 1 \in O(2^(k+1))
What is k? from the expansion 4^k = n. So, k = 1/2 log(n). Thus, T(n) \in O(2^(1/2 log(n) + 1)) = O(sqrt(n)). Note that 2^log(n) = n.
public static int recurC(int n) {
if(n==1)
return 1;
return n + recurC(n-1) + recurC(n-2);
}
So I need to find the formal equation for T(n). I set it up as a recurrence relation with T(n) = C + T(n-1) + T(n-2). However, when I tried to evaluate it out, I got nowhere. The relation with each subsequent recursive call isn't entirely clear to me. Any help would be appreciated, thanks!
Let's analyze the recurrence relation below:
T(n) = T(n-1) + T(n-2) + C
T(0) = T(1) = 1
Notice that T(n-1) ≈ T(n-2), that is to say:
Number of operations performed with input size n - 1 is approximately equal to the number of operations performed with input size n - 2.
Therefore, we can show that:
T(n) = T(n-1) + T(n-2) + C
T(n) ≈ 2T(n-2) + C
T(n) ≈ 4T(n-4) + 3C
T(n) ≈ 8T(n-6) + 7C
...
T(n) = 2^k*T(n-2k) + (2^k-1)*C
n-2k = 0 --> k = n/2
T(n) = 2^(n/2) + (2^(n/2)-1)*C
T(n) = (1 + C)*2^(N/2) - C
Therefore we can conclude that, T(n) ∈ O(2^(n/2))
I am not familiar with recurrence-solving techniques outside of the master theorem, recursion trees, and the substitution method. I am guessing that solving the following recurrence for a big-O bound does not utilize one of those methods:
T(n) = T(n-1) + 2T(n-2) + 1
We can make the substitution U(n) = T(n) + 1/2 and then get a recurrence
U(n) = T(n) + 1/2
= T(n-1) + 2T(n-2) + 1 + 1/2
= T(n-1) + 1/2 + 2(T(n-2) + 1/2)
= U(n-1) + 2U(n-2),
which is a little magic but, as templatetypedef mentions, the magic can be created with the annihilator method. Now we just have to solve the linear homogeneous recurrence. The characteristic polynomial x^2 - x - 2 factors as (x+1)(x-2), so the solutions are U(n) = a(-1)^n + b2^n where a and b are any constants. Equivalently, T(n) = a(-1)^n + b2^n - 1/2, which is Theta(2^n) except in special cases.
This recursion is called non-homogeneous linear recurrence. and it is solved by converting it to a homogeneous one:
T(n) = T(n-1) + 2T(n-2) + 1
T(n+1) = T(n) + 2T(n-1) + 1
Subtracting 1 from 2 and changing the base, you get T(n) = 2 T(n-1) + T(n-2) - 2 T(n-3). The corresponding characteristic equation is:
x^3 - 2x^2 - x + 2 = 0
which has solutions x = {-1, 1, 2}. This means that the recursion looks like:
c1 * (-1)^n + c2 * 2^n + c3 * 1^n = c1 * 2^n + c2 (-1)^n + c3
Where all these constants can be found knowing T(0) and T(1). For your complexity analysis it is clear that this is exponential O(2^n).
I thought it would be something like this...
T(n) = 2T(n-1) + O(n)
= 2(2T(n-2)+(n-1)) + (n)
= 2(2(2T(n-3)+(n-2))+(n-1))+(n)
= 8T(n-3) + 4(n-2) + 2(n-1) + n
Which ends up being something like the summation of 2i * (n-i), and my book says this ends up being O(2n). Could anybody explain this to me? I don't understand why it's 2n and not just O(n) as the (n-i) will continue n times.
This recurrence has already been solved on Math Stack Exchange. As I solve this recurrence, I get:
T(n) = n + 2(T(n-1))
= n + 2(n - 1 + 2T(n-2)) = 3n - 2 + 2^2(T(n-2))
= 3n - 2 + 4(n - 2 + 2(T(n-3))) = 7n - 10 + 2^3(T(n-3))
= 7n - 10 + 8(n - 3 + 2(T(n-4))) = 15n - 34 + 2^4(T(n-4))
= (2^4 - 1)n - 34 + 2^4(T(n-4))
...and so on.
Effectively the recurrence boils down to:
T(n) = (2n+1) * T(1) − n − 2
See the Math Stack Exchange link for how we arrive at this solution. Taking T(1) to be constant, the dominating factor in the above recurrence is (2(n + 1)).
Therefore, the rate of growth of given recurrence is O(2n).
I am trying to find the time complexity for selection sort which has the following equation T(n)=T(n-1)+O(n)
First I supposed its T(n)=T(n-1)+n .. n is easier though..
Figured T(n-1) = T(n-2) + (n-1)
and T(n-2) = T(n-3) + (n-2)
This makes T(n) = (T(n-3) + (n-2)) + (n-1) + n so its T(n) = T(n-3) + 3n - 3..
K instead of (3) .. T(n) = T(n-k) + kn - k and because n-k >= 0 .. ==> n-k = 0 and n=k Back to the eqaution its.. T(n) = T(0)// which is C + n*n - n which makes it C + n^2 -n.. so its O(n^2).. is what I did ryt??
Yes, your solution is correct. You are combining O(n) with O(n-1), O(n-2) ... and coming up with O(n^2). You can apply O(n) + O(n-1) = O(n), but only finitely. In a series it is different.
T(n) = (0 to n)Σ O(n - i)
Ignore i inside O(), your result is O(n^2)
The recurrence relationship you gave T(n)=T(n-1)+O(n) is true for Selection Sort, which has overall time complexity as O(n^2). Check this link to verify
In selection sort:
In iteration i, we find the index min of smallest remaining entry.
And then swap a[i] and a[min].
As such the selection sort uses
(n-1)+(n-2)+....+2+1+0 = (n-1)*(n-2)/2 = O(n*n) compares
and exactly n exchanges(swappings).
FROM ABOVE
And from the recurrence relation given above
=> T(n) = T(n-1)+ O(n)
=> T(n) = T(n-1)+ cn, where c is some positive constant
=> T(n) = cn + T(n-2) + c(n-1)
=> T(n) = cn + c(n-1) +T(n-3)+ c(n-2)
And this goes on and we finally get
=> T(n) = cn + c(n-1) + c(n-2) + ...... c (total no of n terms)
=> T(n) = c(n*(n-1)/2)
=> T(n) = O(n*n)
EDIT
Its always better to replace theta(n) as cn, where c is some constant. Helps in visualizing the equation more easily.