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]}}
Related
Consider this recurrence relation: x(n) = x(n/2) + n, for n > 1 and x(1) = 0.
Now here the method of back substitution will struggle for values of n not powers of 2, so it is best known here is to use the smoothness rule to solve this types of questions, and when we use the smoothness rule, where we will solve for n = 2^k (for n = values powers of 2) we will have a solution of x(n) = 2n - 1.
However, if we use the method of backward substitution, this recurrence relation will have a solution!
x(n) = x(n/2) + n = x(n/4) + n/2 + n = x(n/8) + n/4 + n/2 + n = x(n/16) + n/8 + n/4 + n/2 + n = ....
where the pattern is
x(n) = x(n/i) + n/(i/2) + n/(i/4) + n/(i/8) + n/(i/16) + ...
which will stop when n = 1 (i.e when i = n) and in this case
x(n) = x(n/n) + n/(n/2) + n/(n/4) + n/(n/8) + n/(n/16) + ... = 1 + 2 + 4 + 8 + 16 + ... = 2^(n+1) - 1
which is two different answers!
So please I am so confused here because in the textbook (Introduction to Analysis and Design of Algorithms by Anany Levitin) it is mention that we should use here the smoothness rule, but as you can see I have solved it exactly by the method of backward substitution where the method was expected here to struggle but nothing has happened!
The transition 1 + 2 + 4 + 8 + 16 + ... = 2^(n+1) - 1 is false.
That is since the number of elements in the left series is log n so the sum is 2^(log n + 1) - 1, which is exactly 2n - 1.
The reason there are log n elements is that n/(2^i) = 1 (the last element of the series is 1) when i = log n.
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)
I have been asked to solve that recurrence relation. I got the next solution: https://imgur.com/a/xWoTI40
So I decided to ask my teacher if it was right. He told me that it wasn't; and that this is the right solution: https://imgur.com/a/CGD0ta8
I'm totally clueless right now. The most I try to understand why mine is wrong; the most I think it's actually right.
Can somebody explain?
Your solution is correct. Here's a different approach with the same result:
t(1) = 0 (given)
t(2) = t(1) + 1 = 1
t(3) = t(2) + 2 = 1 + 2 = 3
t(4) = t(3) + 3 = 1 + 2 + 3 = 6
...
t(n) = 1 + 2 + ... + (n-1) = n * (n - 1) / 2 = Theta(n^2).
The teacher's solution is wrong after the second = sign. Here's what the teacher wrote:
t(n-1) + n - 1 = t(n-2) + n - 1 - 2
But actually the following is correct:
t(n-1) + n - 1 = ( t(n-2) + n - 2 ) + n - 1
which is actually exactly what you got. It appears that the teacher dropped an n term.
In fact, the teacher's solution ends with a dominant term of -n^2 which is clearly wrong, as t(n) >= 0 for all n >= 0.
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
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.