user2781902
user2781902

Reputation: 63

Is O(log(n*log n) can be considered as O(log n)

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)?

Upvotes: 0

Views: 247

Answers (3)

luk32
luk32

Reputation: 16080

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:

  1. lim = infinty <=> O(f(x)) > O(g(x))
  2. inf > lim > 0 <=> O(f(x)) = O(g(x))
  3. 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.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234847

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)).

Upvotes: 0

JackCColeman
JackCColeman

Reputation: 3807

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)

Upvotes: 1

Related Questions