如何计算 Big Oh 符号

发布于 2025-01-01 03:57:52 字数 164 浏览 2 评论 0原文

请有人告诉我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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Saygoodbye 2025-01-08 03:57:52

big-O 有严格的数学定义:

如果存在值 x0 >,则 f(x) 为 O(g(x)) ;= 0 且k > 0 使得对于所有 x > x0,f(x) <= k*g(x)。

要证明有关函数大 O 分类的陈述,您必须展示如何查找 x0k 值。

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.

岁月打碎记忆 2025-01-08 03:57:52
2^4 = 2^4 * 1 <= 2^4 * 1

因此,2^4O(1),且常量为 2^4

10 * n = 10 * n <= 10 * n

因此,10 * nO(n),常数为 10

n log2 n = n log n / log 2 <= (1 / log 2) * n log n

因此,n log2 nO(n log n),常数为 1 / log 2

2^4 = 2^4 * 1 <= 2^4 * 1

so 2^4 is O(1) with constant 2^4.

10 * n = 10 * n <= 10 * n

so 10 * n is O(n) with constant 10.

n log2 n = n log n / log 2 <= (1 / log 2) * n log n

so n log2 n is O(n log n) with constant 1 / log 2.

眼眸里的那抹悲凉 2025-01-08 03:57:52

一般来说,使用渐近符号的定义,然后产生 f(n) = O(g(n)) 的证明,其中 f(n) 是您的函数,g(n) 是您要证明的界限。

对于您的第一个示例:

2n =? O(3n)
f(n) = 2n, g(n) = 3n
need c such that for n > n0, f(n) <= c*g(n); guess c = 1
We have f(n) = 2n <= 3n = c*g(n) for n >= 0, so
2n = f(n) = O(g(n)) = O(3n)

对于您的第二个示例:

2^4 =? O(1)?
f(n) = 2^4, g(n) = 1
need c such that for n > n0, f(n) <= c*g(n); guess c = 2^4 + 1
We have f(n) = 2^4 <= 2^4 + 1 = c*g(n), so
2^4 = f(n) = O(g(n)) = O(1)

您的第三个示例实际上等同于第一个示例。

你的第四个例子是错误的;这是假的

n log^2(n) = O(n log n)

这是真的

n log(n^2) = O(n log n)

但那是不同的表达方式。要看出 n log^2(n) 不是 O(n log n),我们可以这样论证:设 c 是任意固定常数。我们可以找到一个 n 使得 c*n*log(n) < n*log^2(n)。我们得到

c*n*log(n) < n*log^2(n)
c < log(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:

2n =? O(3n)
f(n) = 2n, g(n) = 3n
need c such that for n > n0, f(n) <= c*g(n); guess c = 1
We have f(n) = 2n <= 3n = c*g(n) for n >= 0, so
2n = f(n) = O(g(n)) = O(3n)

For your second example:

2^4 =? O(1)?
f(n) = 2^4, g(n) = 1
need c such that for n > n0, f(n) <= c*g(n); guess c = 2^4 + 1
We have f(n) = 2^4 <= 2^4 + 1 = c*g(n), so
2^4 = f(n) = O(g(n)) = O(1)

Your third example is practically equivalent to the first.

Your fourth example is wrong; it is false that

n log^2(n) = O(n log n)

It is true that

n log(n^2) = O(n log n)

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

c*n*log(n) < n*log^2(n)
c < log(n)

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.

给我一枪 2025-01-08 03:57:52

有多种方法可以证明这一点:

其中一种可能是使用极限:

f(n) = O(g(n)) iff:如果以下成立:

                f(n)
   limsup    ----------  < infinity
x->infinity     g(n)

因此,如果您取 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:

                f(n)
   limsup    ----------  < infinity
x->infinity     g(n)

So, if you take 2n/(3n) in the limit, this is 1.5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文