How to solve the following recurrence? - algorithm

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).

Related

What is the time complexity of the function T(n)=2T(n/4)+O(1)?(Without masters theorem)

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.

How to solve T(n) = T(n-3)+n^2 using iteration?

How can I solve T(n) = T(n-3)+n^2 using iteration?By master theorem answer is O(n^3) but I am having trouble solving it by iteration.
By direct resolution of the recurrence:
This is a linear recurrence of the first order. We first solve the homogeneous part,
T(n) = T(n - 3)
which is solved by a constant (more precisely three constants as three intertwined sequences form the solution).
Now for the non-homogeneous part, we use the Ansatz T(n) = an³ + bn² + cn + d, because we know that the difference of two cubic polynomials is a quadratic one.
Then
a(n³ - (n-3)³) + b(n² - (n-3)²) + c(n - (n-3)) = 9an² + 3(-9a + 2b)n + 3(9a - 3b + c) = n²
gives
a = 1/9, b = 1/2, c = 1/2.
Finally
T(n) = (2n³ + 9n² + 9n)/18 + T(0)
and similarly for the two other sequences.
Just try to expand the equation:
T(n) = n^2 + (n-3)^2 + (n-6)^2 + ... + 1 = \Theta(n^3)
T(3) = T(0) + 3²
T(6) = T(3) + 6² = T(0) + 3² + 6²
T(9) = T(6) + 9² = T(0) + 3² + 6² + 9²
...
More generally, T(3N) is the sum of T(0) and nine times the sum of the squared naturals up to N. The well-known Faulhaber formula justifies O(N³).
Similar results hold for T(3N+1) and T(3N+2).

What is the recurrence relation and big O for T(n) = 2T(n-1) + O(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).

What is the time complexity of recurrence 2T(n-1)+O(n)?

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.)

Recurrence Relation: T(n) = 2T(n/4) + T(n/2) + n^2

I've been working on these recurrence relations but I'm stumped on this one.
T(n) = 2T(n/4) + T(n/2) + n^2
I've seen them with one recursive call but not with two.
These sort of recurrences are solved with Akra-Bazzi method.
In your case a1 = 2, a2 = 1, b1 = 1/4, b2 = 1/2. So you have to solve the equation:
2/4^p + 1/2^p = 1
whose solution is p=1. Now because your g(x) = n^2, your integral from 1 to x will be just x - 1. Therefore you complexity is just x^p( 1 + x - 1) = O(x^2)

Resources