算法分析中 O(1) 和 O(2) 有什么区别?

发布于 2024-08-17 08:13:42 字数 280 浏览 5 评论 0原文

根据大O的定义f(n) <= C*g(n)(即f(n) = O(g(n)),可以推断:

f(n) <= C
f(n) <= 2C

没有太大区别:

f(n) = 1 - 1 / n
f(n) = 2 - 1 / n
C = 1

但这两种复杂性有什么不同,因为两者都是恒定的复杂性?

我认为这两者之间 O(1) 和 O(2)。

According to the definition of big O f(n) <= C*g(n)(which means f(n) = O(g(n)), it could be deduced that:

f(n) <= C
f(n) <= 2C

I think there are no big differences between these two. What I could come up with is:

f(n) = 1 - 1 / n
f(n) = 2 - 1 / n
C = 1

But what differs this two complexities,since both are constant complexity?

Could you show some real world code to demonstrate the differences between O(1) and O(2).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

十雾 2024-08-24 08:13:42

O(1)O(2) 之间没有区别。分类为 O(1) 的算法为 O(2),反之亦然。事实上,对于任何正常数 c1c2O(c1) 都是 O(c2)

O(c) 其中 c 是一个正常数,仅意味着运行时间有界,与输入或问题大小无关。由此可以清楚地看出(非正式地)O(1)O(2) 是相等的。

形式上,请考虑 O(1) 中的函数 f。然后有一个常量c,使得对于所有nf(n) <= c * 1。让d = c / 2。然后 f(n) <= c = (c / 2) * 2 = d * 2 这表明 fO(2) >。类似地,如果 gO(2),则存在一个常数 c,使得 g(n) <= c * 2< /code> 代表所有 n。让d = 2 * c。然后 g(n) <= c * 2 = d = d * 1 这表明 gO(1)。因此O(1) = O(2)

There is no difference between O(1) and O(2). Algorithms classifying as O(1) are O(2) and vice versa. In fact, O(c1) is O(c2) for any positive constants c1 and c2.

O(c) where c is a positive constants simply means that the runtime is bounded independent of the input or problem size. From this it is clear (informally) that O(1) and O(2) are equal.

Formally, consider a function f in O(1). Then there is a constant c such that f(n) <= c * 1 for all n. Let d = c / 2. Then f(n) <= c = (c / 2) * 2 = d * 2 which shows that f is O(2). Similarly if g is O(2) there is a constant c such that g(n) <= c * 2 for all n. Let d = 2 * c. Then g(n) <= c * 2 = d = d * 1 which shows that g is O(1). Therefore O(1) = O(2).

落日海湾 2024-08-24 08:13:42

O(1) 和 O(2) 是相同的,任何 O(常数值)也是如此。

关键是两者都不依赖于 N 的某些函数。

O(1) and O(2) are the same, as is any O(constant value).

The point being that neither rely on some function of N.

别挽留 2024-08-24 08:13:42

没有区别。

在下图中,红线代表 O(n),绿色曲线代表 O(n2)。

正如您通过红线看到的,随着 x 的增加,21 变得微不足道(绿色曲线在速度快得多)。这就是 Big-O 表示法试图捕捉的内容; 常量相对来说没有什么意义。

There is no difference.

In the graph below, the red line represents O(n) and the green curve represents O(n2).

As you can see by the red line, the 2 and the 1 become insignificant as x increases (the green curve grows at a much faster rate). This is what Big-O notation is trying to capture; constants are relatively meaningless.

墨落画卷 2024-08-24 08:13:42

也许他们的意思是,无论输入大小(通常表示为 N),这两种算法都以恒定时间执行,但其中一种速度是其两倍。但这是对大 O 表示法的滥用。

Maybe they meant that both algorithms execute in constant time regardless of input size (usually denoted as N), but one of them is twice as fast. But it's an abuse of the big-O notation.

欢你一世 2024-08-24 08:13:42

O(1) 和 O(2) 之间没有区别。

符号顺序在常数范围内是唯一的。 O(f(x)) 表示存在某个常数 k,使得时间小于 kf(x )。

如果某件事的复杂度为 O(2),则存在某个常量 k,程序需要的时间小于 2k。因此,还有另一个常量 k' = 2k 适用于 O(1)。

There is no difference between O(1) and O(2).

The order-of notation is unique up to a constant. O(f(x)) means that there is some constant k such that the time would be less than kf(x).

If something is O(2), then there is some constant k that the program takes less than 2k. Therefore, there's another constant, k' = 2k that works for O(1).

帅气尐潴 2024-08-24 08:13:42

O(1) 和 O(2) 之间没有区别。事实上你不会使用这个符号。它更像是 O(N) 或 O(n^2)、O(log(N)) 等。这只是算法数量级的指示。换句话说,O(1) 在时间上是恒定的。 O(N) 与项目数量 (N) 呈线性关系,O(n^2) 与时间呈指数关系,等等。

There is not difference between O(1) and O(2). In fact you would not use this notation. It is more like O(N) or O(n^2), O(log(N)) etc. This is just a indication of the order of magnitude of the algorithm. Put in other words, O(1) would be constant in time. O(N) would be linear in the number of items (N), O(n^2) would be exponential in time, etc.

忘羡 2024-08-24 08:13:42

Big-O 表示法通常用于算法复杂性的渐近分析,即分析当 n 向无穷大增加时算法的性能。在这种情况下,函数 f(n) n 中的最高阶项将成为函数的主要部分。

因此,当以 big-O 表示时,n 中的低阶项通常会从函数中删除(例如,f(n)=2n^2+4 将导致 O(n^2) 渐近 big-O 复杂度)。

在最高项为常数且不依赖于 n 的情况下,所有这些常数实际上渐近地相同,并且通常简单地简化为 O(1)。

因此,O(2) 将被视为等同于 O(1)。

Big-O notation is generally used for asymptotic analysis of algorithm complexity, ie analysis of how the algorithm performs as n increases toward infinity. The highest order term in n of the function f(n) will be the dominant part of the function in this case.

As such, lower order terms in n are usually dropped from the function when expressed in big-O (for example, f(n)=2n^2+4 will result in O(n^2) asymptotic big-O complexity).

In the case of the highest term being constant and not dependent on n, then all these constants are effectively the same asymptotically speaking and are usually simply reduced to O(1).

Hence, O(2) would be considered equivalent to O(1).

日记撕了你也走了 2024-08-24 08:13:42

通常不写 O(2) 或 O(2n),而是写 O(1) 和 O(n)。当然,差异在于实际速度 - 例如 5 秒与 10 秒。我发现你的问题有点令人困惑。

You typically do not write O(2) or O(2n) but O(1) and O(n) instead. The difference is in actual speed, of course - 5s vs 10s for instance. I found your question a bit confusing.

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