Consider I get f(n)=log(n*log n). Should I say that its O(log(n*log n)? Or should I do log(n*log n)=log n + log(log n) and then say that the function f(n) is O(log n)?
-
What happens when n becomes very large. That's the test... – Floris Sep 16 '13 at 03:06
-
no. `log(n)` and `log(n * log(n))` intersect. – akonsu Sep 16 '13 at 03:09
-
@akonsu - That's kind of irrelevant. O(log(n*log(n))) = O(log(n) + log(log(n))) < O(2*log(n)) = O(log(n)) (since constant factors are irrelevant for big-O). – Ted Hopp Sep 16 '13 at 03:40
3 Answers
First of all, as you have observed:
log(n*log n) = log(n) + log(log(n))
but think about log(log N) as N->large (as Floris suggests).
For example, let N = 1000, then log N = 3 (i.e. a small number) and log(3) is even smaller, this holds as N gets huge, i.e. way more than the number of instructions your code could ever generate.
Thus, O(log(n * log n)) = O(log n + k) = O(log(n)) + k = O(log n)
Another way to look at this is that: n * log n << n^2, so in the worse case:
O(log(n^2)) > O(log(n * log n))
So, 2*O(log(n)) is an upper bound, and O(log(n * log n)) = O(log n)

- 3,777
- 1
- 15
- 21
Use the definition. If f(n) = O(log(n*log(n))), then there must exist a positive constant M and real n0 such that:
|f(n)| ≤ M |log(n*log(n))|
for all n > n0.
Now let's assume (without loss of generality) that n0 > 0. Then
log(n) ≥ log(log(n))
for all n > n0.
From this, we have:
log(n(log(n)) = log(n) + log(log(n)) ≤ 2 * log(n)
Substituting, we find that
|f(n)| ≤ 2*M|log(n))| for all n > n0
Since 2*M is also a positive constant, it immediately follows that f(n) = O(log(n)).

- 232,168
- 48
- 399
- 521
Of course in this case simple transformations show both functions differ by a constant factor asymptotically, as shown.
However, I feel like it is worthwhile remind a classic test for analyzing how two functions relate to each other asymptotically. So here's a little more formal proof.
You can check how does f(x)
relates to g(x)
by analyzing lim f(x)/g(x)
when x->infinity
.
There are 3 cases:
- lim = infinty <=>
O(f(x))
>O(g(x))
- inf > lim > 0 <=>
O(f(x))
=O(g(x))
- lim = 0 <=>
O(f(x))
<O(g(x))
So
lim ( log( n * log(n) ) / log n ) =
lim ( log n + log log (n) ) / log n =
lim 1 + log log (n) / log n =
1 + 0 = 1
Note: I assumed log log n / log n
to be trivial but you can do it by de l'Hospital Rule.

- 15,812
- 38
- 62