2

I try to solve this equations:

T(n) = T(n-1) + n^2 for n>1

T(1) = 1 for n=1 initial value

knowing that: Σ between i=0 and i=n-1 of ^2 = (+1)(2+1)/6

I proceed as follow:

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

T(n-2) = T(n-3) + (n-2)^2

T(n-3) = T(n-4) + (n-3)^2 ecc...

T(n) = T(n-4) + (n-3)^2 + (n-2)^2 + (n-1)^2 + n^2

T(n) = T(1) + ... + (n-2)^2 + (n-1)^2 + n^2 = = 1 + Σ(n-i)^2 = 1 + Σ(n^2 - 2in +i^2) = with i between 0 and n-1

= 1 + n^2 - 2nΣi + Σi^2 = =~ 1 + n^2 - 2n*(n-(n-1)+n-(n-2)+...+n-2+n-1) + n(n+1)(2n-1)/6

In the last passage I have changed the Σ with the given one...

T(n) =~ 1 + n^2 - 2n(an-b) + (2n^3 + 3n^2 + n)/6 =~ O(n^3)

Basically I ignore all the negligible powers of n but I not sure if this is the legitimate things to do...

ire
  • 23
  • 1
  • 7

1 Answers1

2

When you know that Σ i^2 for i=0 to i=n-1 equals (+1)(2+1)/6 you can state that the time complexity in Big O terms is O(n^3).

Since (+1)(2+1)/6 = (2^3+n^2+2^2+n)/6, you remove all the constants and lower powers of n (yes this is legitimate in Big O calculations as for very large numbers their value will be negligible compared to what the highest power variable produces), and get O(n^3).

user1984
  • 5,990
  • 2
  • 13
  • 32