Non-Linear Recurrence Relation - algorithm

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.

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.

could someone help me solve this recurrence using substitution method?

could someone help me solve this recurrence?
T(n) = 8T(n/2) + n^2 is T(n) = O(n^3) using the substitution method. Considering T(1)=1
One way to approach this recurrence relation, is rsolve from sympy, Python's symbolic math library:
from sympy import Function, rsolve, log
from sympy.abc import k, n
f = Function('f')
g = Function('g')
# T(n) = 8T(n/2) + n^2 T(1) = 1
T = f(n) - 8 * f(n / 2) - n ** 2 - 3
Tk = T.subs({n: 2 ** k, f(n): g(k), f(n / 2): g(k - 1)})
s = rsolve(Tk, g(k), {g(0): 1})
print("solution for k:", s.cancel())
print("solution for n:", s.subs({k: log(n, 2)}).simplify().cancel())
for k in range(0, 11):
print(f"k={k}, n={2 ** k}, T(n)={(17 * n ** 3 - 3) // 7 - n ** 2}")
This outputs the solution (17*n^3 - 3)/7 - n^2.
Note that the function is only defined for n a power of 2.
The first values for n=1,2,4,8,... are 1, 15, 139, 1179, 9691, 78555, 632539, 5076699, 40679131, 325695195, 2606610139
Just try to expand the relation and keep continue by the induction:
T(n) = 8 T(n/2) + n^2 =
8(8T(n/4) + (n/2)^2) + n^2 = 8^2 T(n/2^2) + 8 (n/2)^2 + n^2
As each time n is divided by 2 each time, if n = 2^k, the length of the series k = log(n):
T(n) = n^2 + 8 (n/2)^2 + 8^2 (n/2^2)^2 + ... + 8^log(n) T(1)
= sum_{i=0}^{log(n)} 8^i (n/2^i)^2
Rewrite the relation by 2^2i = 4^i:
T(n) = sum_{i=0}^{log(n)} 8^i/4^i n^2 = n^2 * sum_{i=0}^{log(n)} 2^i
= n^2 (2^(log(n)+1) - 1) = \Theta(n^3)
Notice that 2^log(n) = n.

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

Using induction to prove time complexity of functions

I want to find out the time complexity of this function by using induction f(n) = 0, if n = 0
f(n) = f(n − 1) + 2n − 1, if n ≥ 1 Im using a method call repeated substitution so then i found a close form for f(n)
f(n)= f(n − 1) + 2n − 1 =f(n-2)+4n-4 =f(n-3)+6n-8 .... =f(n-i)+2^in-d
and then by taking i=n i came out with f(n)=f(0)+2^(n+1)-d and can conclude that is has a time complexity of O(2^n) since f(0) and d are all constants.
however i found the answer should be O(n^2) and where did i do wrong
Your math was wrong.
f(n) = f(n - 1) + 2n - 1
f(n) = f(n - 2) + 4n - 4
f(n) = f(n - 3) + 6n - 9
...
f(n) = f(n - i) + 2i n - i^2
When i = n you have:
f(n) = f(n - n) + 2n n - n^2
= f(0) + (2 - 1) n^2
= n^2
Therefore, f(n) is O(n^2)
However you are mistaken. This is not the time-complexity of the function, which is O(n) since that's how many recursions it has, this is the function's order, which means how "quickly it diverges". O(n^2) means the function diverges at a quadratic rate.
I don't fully understand the question. The complexity of calculating the value is O(n), because you can just do the calculation starting from 0:
f(0) = 0
f(1) = 0 + 2*1 - 1 = 1
f(2) = 1 + 2*2 - 1 = 4
f(3) = 4 + 2*3 - 1 = 9
Actually, you probably get the idea . . . the nth value is n^2. I am guessing in the context of the problem that this is what they want the answer to be. However, to calculate it seems to be O(n).

Resources