Solving an Inhomogeneous Recurrence Relation for Fibonacci Sequence - algorithm

I am trying to solve a recurrence relation for the Fibonacci Sequence, but the problem is that it is not homogeneous.
The recurrence relation is as follows:
F(n) = F(n -1) + F(n -2) + Θ(n) for n > 1, and Θ(n) = c1n + c2 , where c1, c2 > 0
Initial conditions: F(0) = 0, F(1) = 1
I've tried to solve it by treating it as a homogeneous linear second-ordered recurrence with constant coefficients, but I'm not sure how to solve it when I have:
F(n) - F(n - 1) - F(n - 2) = c1n + c2
Instead of:
F(n) - F(n - 1) - F(n - 2) = 0
What is the best method for solving this type of recurrence relation?

You can find a lower and upper bound using the following inequalities:
2F(n-2) + Theta(n) < T(n) < 2F(n-1) + Theta(n)
You can easily justify that the lower and upper bounds are in Theta(n 2^n). Hence, T(n) = Theta(n 2^n).

With the Ansatz F1(n) = a*n + b you get
F1(n) - F1(n-1) - F1(n-2) = -a*n + 3a - b = Θ(n) = c1*n + c2.
So we have a = -c1 and b = -3c1 - c2, i.e.
F1(n) = -c1*n - 3c1 - c2
solves the given recursion without looking at the initial conditions. Combine this with the solution F0 of the homogenous recursion (see Binet's formula)
F0(n) = d1*q1^n + d2*q2^n
with q1/2 = (1 +/- sqrt(5))/2 to get
F(n) = F0(n) + F1(n) = d1*q1^n + d2*q2^n - c1*n - 3c1 - c2 .
Now one can adjust the factors d1,d2 to match the given inital conditions by solving this system of linear equations
F(0) = d1 + d2 - 3c1 - c2 = 0
F(1) = d1*q1 + d2*q2 - 4c1 - c2 = 1
for d1,d2.

Related

Non-Linear Recurrence Relation

How can I find Nth term for this recurrence relation
F(n) = F(n-1) + F(n-2) + F(n-1)*F(n-2)
I have to find Nth term for this recurrence relation modulo 10^9+7.
I know how to find Nth term for linear recurrence relations but unable to proceed for this.
1<=N<=10^9
F(0) and F(1) are provided as an input.
There's a trick. Let G(n) = F(n) + 1. The equation
F(n) = F(n-1) + F(n-2) + F(n-1)*F(n-2)
becomes
G(n) - 1 = G(n-1) - 1 + G(n-2) - 1 + (G(n-1) - 1) * (G(n-2) - 1)
= G(n-1) - 1 + G(n-2) - 1 + G(n-1)*G(n-2) - G(n-1) - G(n-2) + 1
= G(n-1)*G(n-2) - 1,
so adding 1 to both sides,
G(n) = G(n-1)*G(n-2).
This is the multiplicative equivalent of the familiar Fibonacci recurrence. The solution is
G(n) = G(0)^Fib(n-1) * G(1)^Fib(n),
by analogy with the theory of linear recurrences (where Fib(-1) = 1 and Fib(0) = 0 and Fib(1) = 1), since
G(n-1)*G(n-2) = G(0)^Fib(n-2) * G(1)^Fib(n-1)
* G(0)^Fib(n-3) * G(1)^Fib(n-2)
= G(0)^Fib(n-1) * G(1)^Fib(n)
= G(n).
Hence,
F(n) = (F(0)+1)^Fib(n-1) * (F(1)+1)^Fib(n) - 1,
doing the Fib computations via the matrix power method mod p-1 per Fermat's little theorem and the exponentiation mod p.

Solving the Recurrence Relation T(n)=T(n-1)*T(n-2)+c where T(0)=1 and T(1)=2

I need to solve the recurrence relation for
T(n) = T(n - 1) * T(n - 2) + c where T(0) = 1 and T(1) = 2.
The algorithm computes 2f(n) where f(n) is the nth Fibonacci number. I assume that T(n - 2) is approximately equal to T(n - 1) and then I rewrite the relation as
T(n) = T(n - 1)^2 + c
and finality I reach to complexity of O(n). This the algorithm:
Power(n):
if n == 0 then x = 1;
else if n == 1 then x = 2;
else x = Power(n - 1) * Power(n - 2);
return x;
Is it true that the recurrence relation T(n) = T(n - 1) * T(n - 2) + c has O(n) complexity?
You are confusing multiplication in the algorithm with multiplication in the recurrence relation. You calculate Power(n - 1) and Power(n - 2) and multiply them, but this does not take T(n - 1) * T(n - 2) time. These values are computed separately, and then multiplied, taking T(n - 1) + T(n - 2) time, assuming constant-time multiplication.
The recurrence then becomes T(n) = T(n - 1) + T(n - 2) + c. We'll solve this by substitution. I'll skip choosing an n0; our induction hypothesis will be T(n) = d * 2^n - 1. (We need the -1 to allow of to later cancel the c; this is called strengthening the induction hypothesis, see e.g. here).
T(n) = T(n - 1) + T(n - 2) + c
= d * (2^(n - 1) - 1) + d * (2^(n - 2) - 1) + c (substitute by IH)
= 3d * 2^(n - 2) - 2d + c
<= d * 2^n - 2d + c (3 < 2^2)
<= d * 2^n (when d > c / 2)
Conclusion: T(n) ∈ O(2n).
Bonus fact: the tight bound is actually Θ(φn), where φ is the golden ratio. See this answer for an explanation.
First, the recurrence that models the running time is T(n) = T(n-1) + T(n-2) + C for T(1) = T(0) = 1, for some C>0. This is because you make two recursive calls, one on n-1 and the other on n-2.
The recurrence you have is precisely the Fibonacci recurrence if we forget the additional constant (which contributed a factor C in the end). The exact solution is this (image source: Wikipedia):
where
and
You could say that T(n) = Theta(1.618... ^ n).

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

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)

Θ notation for the sum of a geometric series

I have a question about geometric series. Why is
1 + c + c2 + ... + cn = Θ(cn)
when c > 1? I understand why it is Θ(n) if c = 1 and it is Θ(1) if c < 1, but I just can't figure out why it is Θ(cn) if c>1.
Thanks!
The sum of the first n terms of the geometric series
c0 + c1 + ... + cn-1
is given by the quantitiy
(cn - 1) / (c - 1)
Note that if c > 1, then this quantity is bounded from above by cn - 1 and from below by cn-1 - 1 / c. Therefore, it is O(cn) and Ω(cn), so it is Θ(cn).
Hope this helps!
Let c > 1 and g(c) = 1 + c + c2 + ... + cn.
The first thing to realize is that for some n we have S(n) = (cn+1 - 1) / (c - 1) where S(n) is the sum of the series.
So we have that (cn+1 - cn) / (c-1) <= (cn+1 - 1) / (c - 1) = S(n) since cn >= 1.
So (cn+1 - cn) / (c-1) = (cn(c-1)) / (c-1) = cn <= S(n)
Thus we have that S(n) >= cn.
Now that we have found our lower bound, let's look for the upper bound.
Observe that S(n) = (cn+1 - 1) / (c - 1) <= (cn+1) / (c - 1) = ((cn) c) / (c -1).
To simply our view of the algebra a bit, let y = c / (c-1) and substitute it into the equation above.
Hence, S(n) <= y * cn where y is just some constant since c is! This is an important observation since now it is just a multiple of cn.
So now we have found our upper bound as well.
Thus we have cn <= S(n) <= y * cn.
Therefore, S(n) = Θ(cn) when c > 1.

Resources