How to solve T(n) = T(n-1) + n^2? [closed] - algorithm

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
See title. I'm trying to apply the method from this question: Easy: Solve T(n)=T(n-1)+n by Iteration Method. What I have so far is this, but I don't know how to proceed from here on:
T(n) = T(n-1) + n2
T(n-1) = T(n-2) + (n-1)2 = T(n-2) + n2 - 2n + 1
T(n-2) = T(n-3) + (n-2)2 = T(n-3) + n2 - 4n + 4
T(n-3) = T(n-4) + (n-3)2 = T(n-4) + n2 - 6n + 9
Substituting the values of T(n-1), T(n-2) and T(n-3) into T(n) gives:
T(n) = T(n-2) + 2n2 - 2n + 1
T(n) = T(n-3) + 3n2 - 6n + 5
T(n) = T(n-4) + 4n2 - 12n + 14
Now I have to find a pattern but I don't really know how to do that. What I got is:
T(n) = T(n-k) + kn2 - ...???

I'd start with a guess that since each T (n) is equal to the previous one plus a square, T (n) is something cubic.
Write T (n) = an^3 + bn^2 + cn + d.
Substitute this into T (n) = T (n - 1) + n^2 and solve for a, b, c.
And obviously T (0) = d.

If you combine the equations you yourself write:
T(n)=
=n^2+T(n-1)=
=n^2+(n-1)^2+T(n-2)=
=n^2+(n-1)^2+(n-2)^2+T(n-3)=
=n^2+(n-1)^2+(n-2)^2+(n-3)^2+...+2^2+1^2+T(0)=
=n(n+1)(2n+1)/6+T(0) //based on well known formula for S(x^2, x=1..n)

Related

Determining the running time for recurrence relation T(n) = T(n-1)+n

How do I determine the running time (in terms of Big-Theta) for the algorithm of input size n that satisfies recurrence relation T(n) = T(n-1)+n where n >= 1 and with initial condition T(1) = 1?
Edit: I was practicing a past exam paper. Got stuck on this question. Need guidance
Look at it this way: T(n) = T(n-1) + n = T(n-2) + (n-1) + n = T(n-3) + (n-2) + (n-1) + n. Which means if n >= 1 then you will get something like T(n) = 1 + 2 + 3 + ... + n. If you work out the pattern of this series you will see that (n+1)n/2. Therefore, Ө(n^2)

What is the recurrence relation and big O for T(n) = 2T(n-1) + O(N)?

I thought it would be something like this...
T(n) = 2T(n-1) + O(n)
= 2(2T(n-2)+(n-1)) + (n)
= 2(2(2T(n-3)+(n-2))+(n-1))+(n)
= 8T(n-3) + 4(n-2) + 2(n-1) + n
Which ends up being something like the summation of 2i * (n-i), and my book says this ends up being O(2n). Could anybody explain this to me? I don't understand why it's 2n and not just O(n) as the (n-i) will continue n times.
This recurrence has already been solved on Math Stack Exchange. As I solve this recurrence, I get:
T(n) = n + 2(T(n-1))
= n + 2(n - 1 + 2T(n-2)) = 3n - 2 + 2^2(T(n-2))
= 3n - 2 + 4(n - 2 + 2(T(n-3))) = 7n - 10 + 2^3(T(n-3))
= 7n - 10 + 8(n - 3 + 2(T(n-4))) = 15n - 34 + 2^4(T(n-4))
= (2^4 - 1)n - 34 + 2^4(T(n-4))
...and so on.
Effectively the recurrence boils down to:
T(n) = (2n+1) * T(1) − n − 2
See the Math Stack Exchange link for how we arrive at this solution. Taking T(1) to be constant, the dominating factor in the above recurrence is (2(n + 1)).
Therefore, the rate of growth of given recurrence is O(2n).

Recurrence complexity [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Find the tight asymptotic bound:
T(n) = 1 if n = 1
2T(n/4) + T(n/2) + n2 if n > 1
I tried drawing a recurrence tree. First row I had n2, second row I had (3/8)(n4), third row I had (27/1024)(n8).. Don't know how to continue from there.
Thanks in advance.
I think the Master Theorem can be applied in this problem. It is much easier to understand than recurrence tree.
Write the recurrence formula in another form. Add T(n/2) to both the left and right side.
T(n) + T(n/2) = + 2T(n/4) + 2T(n/2) + n2
T(n) + T(n/2) = + 2[ T(n/4) + T(n/2) ] + n2
Define G(n) = T(n) + T(n/2), then
G(1) = Θ(1)
G(n) = 2G(n/2) + n2, if n > 1
Apply the Master Theorem to the above new recurrence formula
G(n) = Θ(n2)
that is
T(n) + T(n/2) = Θ(n2)
for n > 1
T(n) = 2 * T(n/4) + T(n/2) + n2
= T(n/4) + [ T(n/2) + T(n/4) ] + n2
= T(n/4) + Θ(n2/4) + n2
= T(n/4) + Θ(n2)
Apply the Master Theorem to the above new recurrence formula
T(n) = Θ(n2)

Solving the recurrence T(n) = T(n/2) + lg n? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I am having some issues on how to solve recurrence relations.
T(n) = T(n/2) + log2(n), T(1) = 1, where n is a power of 2
This is a homework problem, so don't just give me the answer. I was just wondering how to start the problem.
In class we went over the Master theorem. But I don't think that would be the best way to solve this particular relation.
I don't really know how to start the problem... should I just be going
T(n) = T(n/2) + log_base2(n)
T(n/2) = [T(n/4)+log_base2(n/2)]
T(n) = [T(n/4)+log_base2(n/2)] + log_base2(n)
And just keep working my way down to get something I can see makes a basic equation?
This recurrence solves to Θ((log n)2). Here are two ways to see this.
Some Substitutions
If you know that n is a perfect power of two (that is, n = 2k), you can rewrite the recurrence as
T(2k) = T(2k-1) + k
Let's define a new recurrence S(k) = T(2k). Then we get that
S(k) = S(k - 1) + k
If we expand out this recurrence, we get that
S(k) = S(k - 1) + k
= S(k - 2) + (k - 1) + k
= S(k - 3) + (k - 2) + (k - 1) + k
= S(k - 4) + (k - 3) + (k - 2) + (k - 1) + k
...
= S(0) + 1 + 2 + 3 + ... + k
= S(0) + Θ(k2)
Assuming S(0) = 1, then this recurrence solves to Θ(k2).
Since S(k) = T(2k) = T(n), we get that T(n) = Θ(k2) = Θ(log2 n).
Iterating the Recurrence
Another option here is to expand out a few terms of the recurrence and to see if any nice patterns emerge. Here’s what we get:
T(n) = T(n / 2) + lg n
= T(n / 4) + lg (n / 2) + lg n
= T(n / 8) + lg (n / 4) + lg (n / 2) + lg n
...
Eventually, after lg n layers, this recurrence bottoms out and we’re left with this expression:
lg n + lg (n / 2) + lg (n / 4) + ... + lg (n / 2lg n)
Using properties of logarithms, we can rewrite this as
lg n + (lg n - 1) + (lg n - 2) + (lg n - 3) + ... + (lg n - lg n)
Or, written in reverse, this is the sum
0 + 1 + 2 + 3 + ... + lg n
That sum is Gauss’s sum up to lg n, which evaluates to (lg n)(lg n + 1) / 2 = Θ((log n)2).
Hope this helps!
If n is a power of 2 then you can just expand out the recurrence and solve exactly, using that lg(a/b) = lg(a) - lg(b).
T(n) = lg(n) + lg(n/2) + lg(n/4) + ... + lg(1) + 1
= (lg(n) - 0) + (lg(n) - 1) .... + (lg(n) - lg(n)) + 1
= lg(n)*lg(n) - lg(n)*(lg(n)+1)/2 + 1
= lg(n)*lg(n)/2 - lg(n)/2 + 1
This can be done with the Akra-Bazzi theorem. See the third example in http://people.mpi-inf.mpg.de/~mehlhorn/DatAlg2008/NewMasterTheorem.pdf.
This can be solved with Master theorem. Your a=1 and b=2 and f(n) = log(n). Then c = log2(1) = 0. Because of your c and f(n) you fall into the second case (where k=1).
So the solution is Θ(log2 n)

Solution for recurrence T(n)=t(n/2)+sqrt(n) using induction only [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am looking to prove that T(n)=T(n/2)+sqrt(n) is O(sqrt(n)) given T(1)=1
using only induction.
It is easy to solve using the Master theorem but this is not the case.
I tried to assume
T(n/2) < c*sqrt(n/2)
but didnt get very far with the rest of the proof.
Thank you all in advance for your answers.
Edit:
my line of solution (after the assumption above) is:
T(n) <= c*sqrt(n/2)+sqrt(n) = sqrt(n)(c/sqrt(2)+1) <= sqrt(n)(c+1)
I dont know how to move from this to the required
T(n)<=c*sqrt(n)
ok, you're close. So basically, as I mentioned in the comment, base case is simple. For induction case, you want to show that T(n) is O(sqrt(n)) given that T(n/2) is O(sqrt(n/2)).
So, it goes like this:
T(n) = T(n/2) + sqrt(n) ; this is just your recurrence
< c sqrt(n/2) + sqrt(n) ; since T(n/2) is O(sqrt(n))
; wlog here, assume c > 4
= c sqrt(n) / sqrt(2) + sqrt(n)
= (c/sqrt(2) + 1) sqrt(n)
observe that for c > 4, c / sqrt(2) + 1 < c, so
(c/sqrt(2) + 1) sqrt(n) < c sqrt(n)
so
T(n) < c sqrt(n)
Therefore, T(n) is O(sqrt(n))
So there's a couple key points here that you missed.
The first is that you can always increase the c to whatever value you want. This is because big O only requires <. if it's < c f(n) then it is < d f(n) where d > c.
The second is to note that the line f(c) = c/sqrt(2) + 1 intersects with the line f(c) = c at about c = sqrt(2) / (sqrt(2)-1) = 3.4143 (or so), so all you have to do is force c to be > this value in order to get (c/sqrt(2) + 1) < c. 4 certainly works, so that's where the 4 comes from.
In retrospect, I should have given the key points as hints. My fault. Sorry!
One line of thinking which may help is to expand the recurrence recursively. You get
T(n) = sqrt(n) + sqrt(n/2) + sqrt(n/4) + ... + sqrt(n/(2^k)) + ... + sqrt(1)
= sqrt(n) + sqrt(n)/sqrt(2) + sqrt(n)/sqrt(4) + ... + sqrt(n)/sqrt(2^k) + ... + sqrt(1)
= sqrt(n) * (1 + sqrt(1/2) + sqrt(1/2)^2 + ... + sqrt(1/2)^k + ...)
<= sqrt(n) * ∑(k=0 to ∞) sqrt(1/2)^k
= sqrt(n) * 1/(1 - sqrt(1/2))
Since 1/(1-sqrt(1/2)) is a finite constant (it's about 3.4), T(n) must be O(sqrt(n)). You can use this information to prove it using standard induction.

Resources