views:

93

answers:

3

I have the following worked out:

T(n) = T(n - 1) + n = O(n^2)

Now when I work this out I find that the bound is very loose. Have I done something wrong or is it just that way?

+5  A: 

You need also a base case for your recurrence relation.

T(1) = c
T(n) = T(n-1) + n

To solve this, you can first guess a solution and then prove it works using induction.

T(n) = (n + 1) * n / 2 + c - 1

First the base case. When n = 1 this gives c as required.

For other n:

  T(n)
= (n + 1) * n / 2 + c - 1
= ((n - 1) + 2) * n / 2 + c - 1
= ((n - 1) * n / 2) + (2 * n / 2) + c - 1
= (n * (n - 1) / 2) + c - 1) + (2 * n / 2)
= T(n - 1) + n

So the solution works.

To get the guess in the first place, notice that your recurrence relationship generates the triangular numbers when c = 1:

T(1) = 1:

*

T(2) = 3:

*
**

T(3) = 6:

*
**
***

T(4) = 10:

*
**
***
****

etc..

Intuitively a triangle is roughly half of a square, and in Big-O notation the constants can be ignored so O(n^2) is the expected result.

Mark Byers
Could you tell me how you got to the third equation you have in your answer? What mathematical technique did you apply to it?
Tony
@Tony: It's a "guess". In reality it's because I know the formula for triangular numbers - I didn't actually guess it, I already knew it. It is often easier to "guess" the correct answer and prove that it is correct instead of deriving it from first principles. This is a standard technique for solving recurrence relations.
Mark Byers
@Tony: The technical term for an educated guess is "ansatz". See: http://en.wikipedia.org/wiki/Ansatz. There is some information about using a guess to solve a recurrence relation in Wikipedia: http://en.wikipedia.org/wiki/Recurrence_relation. Other possible methods of solving recurrence relations are also listed there.
Mark Byers
From O(n^2) you can more easily guess that the exact solution is a quadratic polynomial ax^2+bx+c and solve for a, b and c. There is a detailed description how to solve such problems in the book book Concrete Mathematics by Knuth, Oren, Patashnik.
starblue
A: 

Looks about right, but will depend on the base case T(1). Assuming you will do n steps to get T(n) to T(0) and each time the n term is anywhere between 0 and n for an average of n/2 so n * n/2 = (n^2)/2 = O(n^2).

shuttle87
+1  A: 

Think of it this way:
In each "iteration" of the recursion you do O(n) work.
Each iteration has n-1 work to do, until n = base case. (I'm assuming base case is O(n) work)
Therefore, assuming the base case is a constant independant of n, there are O(n) iterations of the recursion.
If you have n iterations of O(n) work each, O(n)*O(n) = O(n^2).
Your analysis is correct. If you'd like more info on this way of solving recursions, look into Recursion Trees. They are very intuitive compared to the other methods.

Rubys
I was too much into the math of it all I think, and forgot the fact we're dealing with a recursion. Maybe that's why I don't quite get it. For the above I got T(n) <= 2n would that be correct?
Tony
Didn't quite understand what you're asking, there alot above
Rubys
@Tony: No that is not correct. T(n) can be greater than 2n for small n.
Mark Byers