简体   繁体   English

是否O(log(n * log n)可以视为O(log n)

[英]Is O(log(n*log n) can be considered as O(log n)

Consider I get f(n)=log(n*log n). 考虑我得到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)? 我应该说它的O(log(n * log n)?还是我应该做log(n * log n)= log n + log(log n)然后说函数f(n)是O(log n )?

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). 但请考虑log(log N)为N-> large(如Floris所建议)。

For example, let N = 1000, then log N = 3 (ie a small number) and log(3) is even smaller, this holds as N gets huge, ie way more than the number of instructions your code could ever generate. 例如,让N = 1000,则log N = 3(即一个小数),log(3)甚至更小,这随着N变大而成立,即比代码可以生成的指令数更多。

Thus, O(log(n * log n)) = O(log n + k) = O(log(n)) + k = O(log n) 因此,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: 另一种看待这种情况的方式是:n * log n << n ^ 2,因此在更坏的情况下:

  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) 因此,2 * O(log(n))是一个上限,O(log(n * log n))= O(log n)

Use the definition . 使用定义 If f(n) = O(log(n*log(n))), then there must exist a positive constant M and real n 0 such that: 如果f(n)= O(log(n * log(n))),则必须存在一个正常数M和实数n 0 ,使得:

|f(n)| | f(n)| ≤ M |log(n*log(n))| ≤M | log(n * log(n))|

for all n > n 0 . 对于所有n> n 0

Now let's assume (without loss of generality) that n 0 > 0. Then 现在让我们假设(不失一般性)n 0 > 0。

log(n) ≥ log(log(n)) log(n)≥log(log(n))

for all n > n 0 . 对于所有n> n 0

From this, we have: 由此,我们得到:

log(n(log(n)) = log(n) + log(log(n)) ≤ 2 * log(n) log(n(log(n))= log(n)+ log(log(n))≤2 * log(n)

Substituting, we find that 代入,我们发现

|f(n)| | f(n)| ≤ 2*M|log(n))| ≤2 * M | log(n))| for all n > n 0 对于所有n> n 0

Since 2*M is also a positive constant, it immediately follows that f(n) = O(log(n)). 由于2 * M也是一个正常数,因此立即得出f(n)= O(log(n))。

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 . 您可以通过在x->infinity时分析lim f(x)/g(x)来检查f(x)g(x)的关系。

There are 3 cases: 有3种情况:

  1. lim = infinty <=> O(f(x)) > O(g(x)) lim =无限<=> O(f(x)) > O(g(x))
  2. inf > lim > 0 <=> O(f(x)) = O(g(x)) inf> lim > 0 <=> O(f(x)) = O(g(x))
  3. 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 . 注意:我假设log log n / log n是微不足道的,但是您可以通过de l'Hospital Rule做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM