I was able to solve this relation as described here Solving the recurrence relation $T(n)=T(n-1)+cn$
By the way, my exercises asks also to demostrate by "substitution" and "induction" why the result shall be $T(n) = O(n^2)$
This is what I tried:
$Goal$ We shall demostrate that T(n)<=$O(n^2)$
Where
- T(1) = c' if n=1 (c' is a constant greater than zero)
- T(n) = T(n-1) + cn if n>1 (c is a constant greater than zero)
Thesis: Exists some d>0 such as $T(n) <= dn^2$. d is a positive constant.
Base case (with n=1)
$T(1) = c' <= d(1^2)$ Hence true dor any d>=c'.
Induction hypotesis: the thesis is still true for any m<n.
| Equation | Applied hypotesis | Our constraint |
|---|---|---|
| $T(n) = T(n-1) + cn $ | $<= d(n-1)^2+cn$ $= dn^2-2dn-d+cn$ | <= dn^2 |
Now, solving the equation
$dn^2-2dn-d+cn <= dn^2 $
$-2dn-d+cn <= 0 $
$cn <= 2dn+d $
Then I stuck, I cannot demostrate for d because n is still there, normally I will get some kind of semplification where can I get something like true for all c greater than ...
What am I doing wrong?
You can solve the recurrence like this. We have $$T(1) = T(0) + c.$$ Then $$T(2) = T(1) + 2c = T(0) + c(1 + 2).$$ Then $$T(3) = T(2) + 3c = T(0) + c(1+2+3).$$ When you do this repeatedly, you get that $$T(n) = T(0) + c(1 + 2 + 3 + \dots + n).$$ Can you finish the problem from here?