如何计算 Big Oh 符号
请有人告诉我2n = O(3n)
是如何计算的?
以下是其他一些示例:
2^4 = O(1)
10n = O(n)
n log2(n) = O(n log n)
Please can someone tell me how 2n = O(3n)
is calculated?
Here's some other examples:
2^4 = O(1)
10n = O(n)
n log2(n) = O(n log n)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
big-O 有严格的数学定义:
如果存在值 x0 >,则 f(x) 为 O(g(x)) ;= 0 且k > 0 使得对于所有 x > x0,f(x) <= k*g(x)。
要证明有关函数大 O 分类的陈述,您必须展示如何查找 x0 和 k 值。
There is a rigorous mathematical definition of big-O:
f(x) is O(g(x)) if there exist values x0 >= 0 and k > 0 such that for all x > x0, f(x) <= k*g(x).
To prove statements about the big-O classification of functions, you must show how to find the x0 and k values.
因此,
2^4
是O(1)
,且常量为2^4
。因此,
10 * n
是O(n)
,常数为10
。因此,
n log2 n
是O(n log n)
,常数为1 / log 2
。so
2^4
isO(1)
with constant2^4
.so
10 * n
isO(n)
with constant10
.so
n log2 n
isO(n log n)
with constant1 / log 2
.一般来说,使用渐近符号的定义,然后产生 f(n) = O(g(n)) 的证明,其中 f(n) 是您的函数,g(n) 是您要证明的界限。
对于您的第一个示例:
对于您的第二个示例:
您的第三个示例实际上等同于第一个示例。
你的第四个例子是错误的;这是假的
这是真的
但那是不同的表达方式。要看出 n log^2(n) 不是 O(n log n),我们可以这样论证:设 c 是任意固定常数。我们可以找到一个 n 使得 c*n*log(n) < n*log^2(n)。我们得到
所以选择 n = 2^(c+1)。因此,不存在这样的n0,对于n>1, n0, f(n) <= c*g(n)。
编辑:在第四个示例中,如果您的意思是“log2(n)”n 的对数基数二,那么是的,nlog2(n) = O(nlogn)。通常,在计算复杂性算法时,除非另有说明,否则对数被理解为以 2 为底。抱歉造成混乱。
Generally speaking, use the definition of the asymptotic notation, then produce a proof that f(n) = O(g(n)), where f(n) is your function and g(n) is the bound you are trying to prove.
For your first example:
For your second example:
Your third example is practically equivalent to the first.
Your fourth example is wrong; it is false that
It is true that
But that's a different expression. To see that n log^2(n) is not O(n log n), we can argue thusly: let c be an arbitary fixed constant. We can find an n such that c*n*log(n) < n*log^2(n). We get
So choose n = 2^(c+1). Therefore, there is no n0 such that, for n > n0, f(n) <= c*g(n).
EDIT: In the fourth example, if you mean by "log2(n)" the log-base-two of n, then yes, nlog2(n) = O(nlogn). Typically, in doing algorithmic complexities, the logarithm is understood to be to base two, unless otherwise specified. Sorry for the confusion.
有多种方法可以证明这一点:
其中一种可能是使用极限:
f(n) = O(g(n)) iff:如果以下成立:
因此,如果您取 2n/(3n) 在极限中,这是
1.5
。There are multiple ways of proving this:
One could be the use of the limit:
f(n) = O(g(n)) iff the following holds:
So, if you take
2n/(3n)
in the limit, this is1.5
.