-1

I am working on the following algorithm time equation:

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

What would be its Big O?

Saurav
  • 1

2 Answers2

2

By expanding,

$$T(n) = n^2+n\cdot T(n-1)$$ $$ = n^2+n[(n-1)^2+(n-1) \cdot T(n-2)]$$ $$= n^2+n(n-1)^2 + n(n-1)T(n-2)$$ $$= n^2+n(n-1)^2 + n(n-1)[(n-2)^2+(n-2) \cdot T(n-3)]$$ $$= n^2+n(n-1)^2 + n(n-1)(n-2)^2+n(n-1)(n-2) \cdot T(n-3)$$ $$ = n \cdot \frac{n!}{(n-1)!} + (n-1) \cdot \frac{n!}{(n-2)!} + (n-2) \cdot \frac{n!}{(n-3)!} + T(n-3) \cdot \frac{n!}{(n-3)!}$$ $$=T(n-3) \cdot \frac{n!}{(n-3)!} + \sum_{i=1}^3 (n-i+1) \frac{n!}{(n-i)!}$$ $$\dots$$ $$=T(n-k) \cdot \frac{n!}{(n-k)!} + \sum_{i=1}^k (n-i+1) \frac{n!}{(n-i)!}$$

If you assume that $T(0) = 0$, then when $k = n$, we have $n-k = 0$ and hence,

$$T(0) \cdot \frac{n!}{(n-n)!} + \sum_{i=1}^n (n-i+1) \frac{n!}{(n-i)!}$$ $$ = \sum_{i=1}^n (n-i+1) \frac{n!}{(n-i)!}$$ $$ n! \cdot \sum_{i=1}^n \frac{(n-i+1)}{(n-i)!}$$ $$ \leq n! \cdot \sum_{i=1}^n n \quad \text{(for sufficiently large n)}$$ $$ = n! \cdot n^2 \in \mathcal{O}(n! \cdot n^2),$$

as desired.

EDIT: There's a lot of steps here, so let me know if there's a particular step that isn't clear and I can try to elaborate.

benguin
  • 146
  • 1
  • 1
    Note that there is a guess here, but no proof. Should be an easy exercise, though. – Raphael Oct 08 '17 at 13:04
  • @benguin, can you explain how 2nd last step came from the previous step, the one where wrote (sufficiently large n) ? – Zephyr Oct 08 '17 at 19:19
  • 1
    @Zephyr Sure, notice that as $n$ approaches infinity that $(n-i)!$ grows much faster than $(n-i+1)$ so $\frac{n-i+1}{(n-i)!}$ will approach $0$ as $n$ grows large. Since this fraction approaches $0$ for larger and larger values of $n$, then after some point, we can expect the function $n$ to overtake it after some point. Hope that's clear. – benguin Oct 08 '17 at 19:26
  • 1
    @benguin Oh.I didn't know that we could even substitute like this before. Thanks ! – Zephyr Oct 08 '17 at 19:37
-1

The upper bound can be expressed as O(n^2 * n!)

Saurav
  • 1