solving recurrence relation with multiple T(n)s - algorithm

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)

Related

Solution for the recurrence algorithm

How is T(n) = 4T(n/2 + 2) + n solved ?
I found a solution in a website:
https://ita.skanev.com/04/04/03.html
I don't understand it.
Is T(n) = 4T(n/2 + 2) + n equivalent with T(n) = 4T(n/2) + (n + 2) ?
I think this is just Case 1 in the Master Theorem. You can read the theorem here https://en.wikipedia.org/wiki/Master_theorem_(analysis_of_algorithms).
Basically, in your problem, a = 4, b = 2, and c = 1. So, we have log_b(a) = 2 > c. Therefore T(n) = Theta(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).

Use substitution to find out the running time of T(1) = 1 ; T(n) = 4T(n/3) + n

I've got to this point 4^logn + 3n[(4/3)^logn -1] and am having trouble finishing it.
Log is base 3.(Wasn't sure how to do subscripts and exponents.)
Thanks .
Masters theorem is general tool for such problems , but if you want the specific solution by substitution then it as follows :
T(n) = 4T(n/3) + n
T(n) = 4(4T(n/9) + n/3) + n = 4^2T(n/9) + (4/3)n + n
T(n) = 4^2(4T(n/27) + n/3^2) + (4/3)n + n = 4^3T(n/27) + (4/3)^2n + (4/3)n + n
T(n) = 4^kT(n/3^k) + (4/3)^(k-1)n + (4/3)^(k-2)n....
boundary condition
n/3^k = 1, k = log3(n)
T(1) = 1
geometric series summation
T(n) = 4^log3(n) + 1((4/3)^log3(n) - 1)/(log3(n)-1)*n
using log rules
T(n) = n^(log3(4)) + n^(1+log3(4/3))/(log3(n)-1) - n/(log3(n)-1)
T(n) = n^(log3(4)) + n^(1+log3(4)-log(3))/(log3(n)-1) - n/(log3(n)-1)
T(n) = n^(log3(4)) + n^(log3(4))/(log3(n)-1) - n/(log3(n)-1)
T(n) = O(n^log3(4))
This is a perfect spot to use the Master Theorem. In your case, we want to write the recurrence in the form
T(n) = aT(n / b) + O(nd).
With your recurrence, you get
a = 4
b = 3
c = 1
Since logba > d, the Master Theorem says that this solves to O(nlog3 4).
Hope this helps!

Solve the recurrence: T(n)=2T(n/2)+n/logn

I can find the sum of each row (n/log n-i) and also I can draw its recursive tree but I can't calculate sum of its rows.
T(n)=2T(n/2)+n/logn
T(1) = 1
Suppose n = 2^k;
We know for harmonic series (euler formula):
Sum[i = 1 to n](1/i) ~= log(n) [n -> infinity]
t(n) = 2t(n/2) + n/log(n)
= 2(2t(n/4) + n/2/log(n/2)) + n/log(n)
= 4t(n/4) + n/log(n/2) + n/log(n)
= 4(2t(n/8) + n/4/log(n/4)) + n/log(n/2) + n/log(n)
= 8t(n/8) + n/log(n/4) + n/log(n/2) + n/log(n)
= 16t(n/16) + n/log(n/8) + n/log(n/4) + n/log(n/2) + n/log(n)
= n * t(1) + n/log(2) + n/log(4) + ... + n/log(n/2) + n/log(n)
= n(1 + Sum[i = 1 to log(n)](1/log(2^i)))
= n(1 + Sum[i = 1 to log(n)](1/i))
~= n(1 + log(log(n)))
= n + n*log(log(n)))
~= n*log(log(n)) [n -> infinity]
When you start unrolling the recursion, you will get:
Your base case is T(1) = 1, so this means that n = 2^k. Substituting you will get:
The second sum behaves the same as harmonic series and therefore can be approximated as log(k). Now that k = log(n) the resulting answer is:
Follow Extended Masters Theorem Below.
Using Extended Masters Theorem T(n)=2T(n/2)+n/logn can be solved easily as follows.
Here n/log n part can be rewritten as n * (logn)^-1,
Effictively maaking value of p=-1.
Now Extended Masters Theorem can be applied easily, it will relate to case 2b of Extended Masters Theorem .
T(n)= O(nloglogn)
Follow this for more detailed explanation
https://www.youtube.com/watch?v=Aude2ZqQjUI

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