views:

110

answers:

6

Hello,

I am trying to find the big O bound for the following recurrence relation:

T(n) = T(n-1) + n^c, where c >= 1 is a constant

So I've decided to solve this by using iteration:

T(n) = T(n-1) + n^c
T(n-1) = T(n-2) + (n-1)^c
T(n) = T(n-2) + n^c + (n-1)^c
T(n-2) = T(n-3) + (n-2)^c
T(n) = T(n-3) + n^c + (n-1)^c + (n-2)^c
T(n) = T(n-k) + n^c + (n-1)^c + .... + (n-k+1)^c

Suppose k = n-1, then:

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

I'm not sure if this is correct however, plus I would really appreciate some guidance as to how to derive Big O from this. Thanks a lot!

A: 

Instead of working you way down from n, how about start by working your way up from 0 (I assume the recursion terminates at 0 and you left that bit out). When you start noticing a fixed point (ie a pattern which repeats the same in all cases) you have a good candidate for an answer. Try proving the answer, e.g. through induction.

Carsten
+2  A: 

What you have is not correct, but you were on the right track.

The mistake you made:

T(n) = T(n-3) + n^c + (n-1)^c + (n-2)^c    
T(n) = T(n-k) + n^c + (n-1)^c + (n-k+1)^c 

You cannot just go from the first line to the second line.

As you increase k, the number of terms in the right hand side increases too.

To see that think of writing it this way:

T(n) - T(n-1)  = n^c.

T(n-1) - T(n-2) = (n-1)^c
..

T(n-k) - T(n-k-1) = (n-k)^c.

..
T(2) - T(1) = 2^c

What happens if you add these up?

Once you do that, can you see what the answer will be for c=1 and c=2? Can you figure out a pattern for the final answer from there?

Moron
A: 

I would start by observing that n^c, whilst of course influenced by the value of n, is not going to be any more complex for n vs. n + 1 - it's c that determines the "runtime" of that particular term. Given that, you can assume (at least I would) that the term has constant runtime and determine how many times the recursion will execute for a given n and you'll get your bound.

Will A
I may have misunderstood what Big-O means - I was thinking in terms of runtime - not mathematically!
Will A
A: 

To figure these out, fill out a couple of terms and look for the pattern:

T(1) = 0 + 1^c

T(2) = T(1) + 2^c = 0 + 1^c + 2^c

T(3) = T(2) + 3^c = 0 + 1^c + 2^c + 3^c

T(4) = ...

Now express the pattern in terms of n and you have your answer.

NickLarsen
Ohh, and also only the most dominate term matters.
NickLarsen
Unfortunately the "most dominant term" isn't all that matters, less-dominant terms matter **if there are enough of them**. See my answer.
Artelius
I completely agree with this, and realize my last statement was misleading. Thank you for the correction.
NickLarsen
+2  A: 

Yes, you are correct:

T(n) = nc + (n-1)c + (n-2)c + … + 3c + 2c + 1,

and this sum is

T(n) = O(nc+1). See e.g. Faulhaber's formula. In fact, you can even determine the constant in the leading term (even if it's not germane to the algorithm's asymptotics): the sum is nc+1/(c+1) + O(c), as you can determine through e.g., using, say, integration.

ShreevatsaR
"You are correct" is not entirely accurate. He replaces n by k-1 but has only 4 terms (also see edit of question). Also, this is tagged as homework. Please don't just give away the answer. And there is a typo: n^(c+1)/(c+1) + O(n^c) :-)
Moron
I assumed the ellipsis (…) was missing as a minor oversight (he added them in one place, immediately after your answer presumably, and not in the others), so I added it explicitly in my answer.Homework: I didn't even answer this question till he asked it repeatedly! :-) If someone asks homework questions directly, it's their own loss, IMHO. I cannot even delete this answer because it's marked accepted.
ShreevatsaR
A: 

Here it is:

T(n) = n^c + (n-1)^c + (n-2)^c + ... + 2^c + 1^c
     < n^c +     n^c +     n^c + ... + n^c + n^c
     = n * n^c
     = n^(c+1)

which is O(nc+1).

To show this is a reasonable bound, note that when c = 1,

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

which is clearly Θ(n2).

Artelius
This could just be a misunderstanding on my part, being that I do not work on these types of problems very often, but `O(n^[c + 1]) = O(n^c)`. This is much like `O(cn) = O(n)`. Its based on growth rate, which is still `n` to a constant power.
NickLarsen
@Nick: Yes, it *is* a misunderstanding on your part. O(n^2) is not O(n).
ShreevatsaR
@ShreevatsaR: I see that corner case, what if you constrain c > 1?
NickLarsen
@Artelius: To show that it is a reasonable bound, consider: postive terms + (n/2)^c + (n/2+1)^c + ... + n^c > (n/2)^c * (n/2). Thus coupled with your earlier estimate, T(n) = Theta(n^(c+1)). So, it is not only reasonable, but accurate.
Moron
@Nick. c=1 is not a corner case. O(n^(c+1)) = O(n^c) is _false_ for every real c.
Moron
@ShreevatsaR, @Moron: Thanks guys, I see it now.
NickLarsen