velen
velen

Reputation: 81

How to solve the following recurrence?

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

Upvotes: 8

Views: 470

Answers (2)

Salvador Dali
Salvador Dali

Reputation: 222521

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

Upvotes: 2

David Eisenstat
David Eisenstat

Reputation: 65458

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.

Upvotes: 3

Related Questions