I am working on the following algorithm time equation:
T(n) = n^2 + n * T(n-1)
What would be its Big O?
I am working on the following algorithm time equation:
T(n) = n^2 + n * T(n-1)
What would be its Big O?
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.