gotchaMinor
Time Complexity: Why does $n^n$ grow faster than $n!$?
Viewed 0 times
whythantimefastergrowdoescomplexity
Problem
Seeing the title, you will probably like to give your explanation as
$n!=n\times (n-1)\times (n-2)\times (n-3)\times\cdots\times 1$
where as
$n^n$ = $n\times n \times n\times n \times\cdots\times n$ $\text{($n$-times)}$
but consider one thing: if we do $\log(n!)$ then it comes out to be $O(n\log n)$.
On the other hand, if we do $\log(n^n)$ it also comes out to be $O(n\log n)$. So, aren't they equal asymptotically?
$n!=n\times (n-1)\times (n-2)\times (n-3)\times\cdots\times 1$
where as
$n^n$ = $n\times n \times n\times n \times\cdots\times n$ $\text{($n$-times)}$
but consider one thing: if we do $\log(n!)$ then it comes out to be $O(n\log n)$.
On the other hand, if we do $\log(n^n)$ it also comes out to be $O(n\log n)$. So, aren't they equal asymptotically?
Solution
The core source of your confusion is that you have assumed that the asymptotic behavior of functions is closed under taking $\log$, which is false. There are countless examples, take:
$$f(n)=n^{100}, \quad g(n)=n$$
If you take their logarithm they become:
$$\log f(n) = 100 \log n,\quad \log g(n) = \log n $$
So their logarithms are both linear in $\log n$, while it's obvious they do not have a similar growth rate. Another example can be:
$$f(n)=\exp(2 n), \quad g(n)=\exp(n)$$
and their logarithms are:
$$\log f(n) = 2n, \log g(n) = n$$
You can see that $f(n) = g(n)^2$ and it has faster growth rate, but both their logarithms are linear in $n$.
The intuitive reason is that, when you compare $\log f(n)$ and $\log g(n)$, you are basically comparing their exponents. Showing that the exponents of two functions have a similar growth rate is not the same as showing that the functions have the same growth rate.
In case you are wondering, the reversed direction would actually hold most of the time: If $\log f(n)$ grows faster than $\log g(n)$, and $g(n)=\Omega(1)$, then $f(n)$ grows faster than $g(n)$, and the condition $g(n)=\Omega(1)$ prevents cases like $f(n) = \exp(1/n), g(n) = \exp(1/n^2)$.
$$f(n)=n^{100}, \quad g(n)=n$$
If you take their logarithm they become:
$$\log f(n) = 100 \log n,\quad \log g(n) = \log n $$
So their logarithms are both linear in $\log n$, while it's obvious they do not have a similar growth rate. Another example can be:
$$f(n)=\exp(2 n), \quad g(n)=\exp(n)$$
and their logarithms are:
$$\log f(n) = 2n, \log g(n) = n$$
You can see that $f(n) = g(n)^2$ and it has faster growth rate, but both their logarithms are linear in $n$.
The intuitive reason is that, when you compare $\log f(n)$ and $\log g(n)$, you are basically comparing their exponents. Showing that the exponents of two functions have a similar growth rate is not the same as showing that the functions have the same growth rate.
In case you are wondering, the reversed direction would actually hold most of the time: If $\log f(n)$ grows faster than $\log g(n)$, and $g(n)=\Omega(1)$, then $f(n)$ grows faster than $g(n)$, and the condition $g(n)=\Omega(1)$ prevents cases like $f(n) = \exp(1/n), g(n) = \exp(1/n^2)$.
Context
StackExchange Computer Science Q#117584, answer score: 9
Revisions (0)
No revisions yet.