Time complexity of the program using recurrence relation - algorithm

This program calculate fibonacci numbers. I want to find out its time complexity using recurrence relation.
Fib(n)
if n<=1
return n
else
x= Fib(n-1)
y= Fib(n-2)
return x+y
The recurrence equation for this program is
T(n)=T(n-1)+T(n-2)+c
I tried to expend it , but couldn't find the solution.
=2T(n-1)+T(n-3)+c+c
=3T(n-3)+2T(n-4)+c+c+3c
=5T(n-4)+3T(n-3)+c+c+3c+5c
-------------------------
-------------------------
-------------------------

You need to think about how many calls you make to your function.
Each call makes 2, so it makes a binary tree:
n
(n-1)--------------(n-2)
(n-2)--(n-3)------(n-3)---(n-4)
and so on.
Consider the level of the tree when you first reach 1 and ignore everything below.
This happens on level n/2 (since the lowest number of each level is the rightmost and it always decreases by 2).
It's clear that the nodes on each level up to n/2 are always twice as many as on the previous level.
Thus the total number of nodes is 1 + 2 + 2^2 + ... + 2^(n/2) = 2^(n/2+1) - 1 = O(2^(n/2))
This means that the time complexity is at least exponential.
You can probably compute it even more accurately, but for all practical purposes this should be enough to avoid this implementation.

The given recurrence relation is,
T(n) = T(n-1) + T(n-2) + c ------ 1
T(n-1)= T(n-2) + T(n-3) + c ------ 2
1-2 -> T(n) = 2T(n-1) - T(n-3) ----- 3
T(n) - 2T(n-1) + T(n-3) = 0 ----- 4
The characteristic equation of 4 is x3 - 2x2 + 1 = 0 ---- 5
Solve equation 5,
The solutions are x = 1, x = (1 + √5)/2 and x = (1 −√5)/2
There for the general solution is,
Tn = a((1 + √5)/2)n + b((1 - √5)/2)n + c . 1n
There for Tn = a((1 + √5)/2)n + b((1 - √5)/2)n + c
Let us assume T(0) = 0, from equation 1 we get T(1) = c and T(2) = 2c
There for,
T(0) = a + b + c = 0 ---- 6
T(1) = a((1 + √5)/2) + b((1 - √5)/2) + c = c
There for a((1 + √5)/2) + b((1 - √5)/2) = 0 ----- 7
T(2) = a((1 + √5)/2)2 + b((1 - √5)/2)2 + c = 2c
There for a((1 + √5)/2)2 + b((1 - √5)/2)2 = c ---- 8
solve 6, 7 and 8, to get the values of a, b and c.
The general solution is,
Tn = a((1 + √5)/2)n + b((1 - √5)/2)n + c
since (1 + √5)/2 < 2,
T(n) = O(2n).

The thing to note about your recurrence relation is that it's the same as the Fibonacci recurrence itself. This means that you're doing c units of work times whatever Fibonacci number you're calculating. You can see it yourself from the first few steps that you computed. The c's start growing like the Fibonacci numbers.
Basically your recurrence comes down to O(Fib(n)). Fibonacci numbers are exponential in n, so you're going to do exponential work.
A better way to do this is to remember one of the numbers. Like this:
Fib(n):
if n <= 2:
return 1,0
else:
x,y = Fib(n-1)
return x+y,x
So when you call Fib(n), you're getting two values, Fib(n) and Fib(n-1). That extra x that you return "remembers" Fib(n-2) so you don't have to compute it twice. This recurrence comes down to T(n) = T(n-1) + c, which is O(n).
Once you have that, you can reduce this to a nice little for loop:
x = 1, y = 0
for i from 3 to n:
x,y = x+y,x

Related

solving recurrence relation with multiple T(n)s

T(n) = 1/2(T(n − 1) + T(n − 2)) + cn, with c > 0
I am having trouble understanding how to solve recurrences with multiple T(n)s. I did a lot of practices by solving recurrence with just one T(n) and following the definition I can do it well. But this is not a recurrence directly solvable with the Master theorem. Anyway I can start a good approach to this question?
solve the homogeneous recurrence:
T_H(n) = 1/2(T_H(n − 1) + T_H(n − 2))
r^2 - r/2 - 1/2 = 0
r = 1 or r = -1/2
T_H(n) = alpha * 1^n + beta * (-1/2)^n (alpha and beta to be determined by initial conditions)
solve the special solution
(1) we want to find a s(n) such that s(n) = 1/2(s(n-1)+s(n-2)) + cn
we know cn is a polynome (in n) so special solution can be found as a polynome too.
Trying with s(n) = an leads to:
an = 1/2(an-1 + an-2) + cn and all terms in an simplify themselves so try the next degree: s(n)=an^2 + bn
an^2 + bn = 1/2 (a(n-1)^2 + b(n-1) + a(n-2)^2 + b(n-2) ) + cn
developping everybody then identifying we get
a = c/3
b = 5c/9
A quick check if we don't trust our ability to make valid calculus:
since s(n) must be valid for all n, let's put arbitrarily n=2, c=7 and check whether s(2) still verifies (1) idem
n = 2, c=7
s(n)-1/2(s(n-1)+s(n-2))-cn ?= 0
below octave shows that indeed s(2) = 0
octave:1> n=2
n = 2
octave:2> c=7
c = 7
octave:3> c/3*n^2 + 5*c/9*n - 1/2*(c/3*(n-1)^2 + 5*c/9*(n-1) +c/3*(n-2)^2 + 5*c/9*(n-2))-c*n
ans = 0
Complexity
T(n) = T_H(n) + sp(n) = alpha + beta (-1/2)^n + c/3n^2 + 5c/9n
so T(n) is in O(n^2)

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

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

time complexity of following recurrence?

Find out the time complexity (Big Oh Bound) of the recurrence T(n) = T(⌊n⌋) + T(⌈n⌉) + 1.
How the time complexity of this comes out to be O(n)??
You probably ment T(n)=T(⌊n/2⌋)+ T(⌈n/2⌉) + 1.
Lets calculate first few values of T(n).
T(1) = 1
T(2) = 3
T(3) = 5
T(4) = 7
We can guess that T(n) = 2 * n - 1.
Lets prove that by mathematical induction
Basis
T(1) = 1
T(2) = 3
T(3) = 5
T(4) = 7
Inductive step
T(2*n) = T(⌊2*n/2⌋)+ T(⌈2*n/2⌉) + 1
= T(⌊n⌋)+ T(⌈n⌉) + 1
= (2*n - 1) + (2*n - 1) + 1
= 4*n - 1
= 2 * (2*n) - 1
T(2*n+1) = T(⌊(2*n+1)/2⌋)+ T(⌈(2*n+1)/2⌉) + 1
= T(n)+ T(n+1) + 1
= (2*n - 1) + (2*(n+1) - 1) + 1 =
= 4*n + 1 =
= (2*n+1)*2 - 1
Since both the basis and the inductive step have been proved, it has now been proved by mathematical induction that T(n) holds for all natural 2*n - 1.
T(n) = 2*n - 1 = O(n)
What you have currently does not make sense. Since n is usually taken to be a natural number, then n=⌊n⌋=⌈n⌉. The recurrence then reads: break down a problem of size n into two problems of size n and spend time 1 doing that. The two new problems you just created will be split in turn, and so on- all you are doing is creating more work for yourself.

Solving a recurrence relation

I'm not sure if this is the right place to post this, but the problem actually belongs to a programming assignment. This recursion is something I probably should know how to solve but Im having a bit of trouble with it.
Solve the recursion:
T(0) = 2;
T(n) = T(n-1) + 2;
Solution:
T(n) = 2(n+1)
Could someone please show me how they got to that solution?
Please not that its not the main part of the assignment to solve this particular problem.
You have to figure out what is solution and then you can use induction, to prove it.
To figure solution is simple.
Value is previous value + 2.
2, 2+2, 2+2+2, 2+2+2+2, 2+2+2+2+2, ...
Use induction to prove:
T(0) = 2
T(n) = T(n-1) + 2;
Solution
T(n) = 2(n+1)
Proof:
T(n) = T(n-1) + 2 => 2((n-1)+1) + 2 = 2(n+1)
Check for n=0
2(0+1)=2
End of proof
Try writing out the first few values - it should then be obvious.
Take T(5):
T(5)
|
+-> T(4) + 2
|
+-> T(3) + 2
|
+-> T(2) + 2
|
+-> T(1) + 2
|
+-> T(0) + 2
|
+-> 2
Now count the number of 2's that are added together for T(5).
Then try to figure out how many 2's would be added for T(n).
It's an arithmetic progression with ratio common difference 2.
The first term is T[0] = 2 and the ratio common difference is r = 2 so the n + 1th term (n + 1th because there are n + 1 numbers in 0, 1, 2, ..., n) is T[0] + r*(n + 1 - 1) = 2 + 2*n = 2*(n + 1).
No guessing required, just recognize it as an arithmetic progression.
Each time n decreases by one, 2 is added. This gives a variable term of 2n. Since T(0) is fixed at 2, this gives a constant term of 2. Adding them together gives 2n + 2, or 2(n + 1).
I'd solve it as follows:
Assume that T(n) = a*n + b for some a and b.
T(0) = 2. So a * 0 + b = 2, thus b = 2.
T(n) = T(n-1) + 2, so
a * n + b = (a * (n-1) + b) + 2 consequently
a * n + b = a * n - a + b + 2 and
0 = - a + 2, thus a = 2.
So we have T(n) = 2 * n + 2 = 2 (n+1).
This one is pretty straightforward to solve by hand as the other answers point out, but in case it's ever useful, Mathematica is pretty good solving recurrence relations like this.
Evaluating
RSolve[{T[0] == 2, T[n] == T[n-1] + 2}, T[n], n]
returns
{{T[n] -> 2 (1 + n)}}
It can, for example, find the closed form of the nth Fibonacci number as well:
RSolve[{F[1] == 1, F[2] == 1, F[n] == F[n-1] + F[n-2]}, F[n], n] //FunctionExpand
returns
{{F[n] -> (((1 + Sqrt[5])/2)^n - (2/(1 + Sqrt[5]))^n*Cos[n*Pi])/Sqrt[5]}}

Resources