表达式的大 O 表示法

发布于 2024-08-18 07:53:38 字数 112 浏览 4 评论 0原文

如果我有一个需要 4n^2 + 7n 步才能完成的算法,它的 O 是多少? O(4n^2)? O(n^2)?

我知道 7n 被截断,但我不知道是否应该保留 n^2 系数。

谢谢

If I have an algorithm that takes 4n^2 + 7n moves to accomplish, what is its O?
O(4n^2)?
O(n^2)?

I know that 7n is cut off, but I don't know if I should keep the n^2 coefficient or not.

Thanks

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

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

发布评论

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

评论(6

幸福还没到 2024-08-25 07:53:38

您应该删除任何系数,因为问题实际上是在“按顺序”询问,它试图将其描述为线性、指数、对数等......也就是说,当 n 非常大时,系数并不重要。

这也解释了为什么你放弃+7n,因为当n非常大时,该项对最终答案的意义相对较小。如果您熟悉微积分,您可能会说 lim n->inf(4*n^2+7n) ~= lim n->inf(4*n^2) ~= lim n->inf(n ^2)

您也可以从图形意义上考虑这个问题...也就是说,如果您将函数 4n^2 + 7n 绘制为越来越大的 n 值,数学家可能会说“它看起来像 n^2”。诚然,它必须是一位相当自由的数学家,因为这不是一个严格的陈述,但这基本上就是 O(...) 试图传达的内容。

You should drop any coefficients because the question is really asking "on the order of", which tries to characterize it as linear, exponential, logarithmic, etc... That is, when n is very large, the coefficient is of little importance.

This also explains why you drop the +7n, because when n is very large, that term has relatively little significance to the final answer. If you are familiar with calculus, you might say lim n->inf(4*n^2+7n) ~= lim n->inf(4*n^2) ~= lim n->inf(n^2)

You can also think about this in a graphical sense... that is, if you graph the function 4n^2 + 7n for larger and larger values of n, a mathematician might say "it looks like n^2". Granted, it would have to be a fairly liberal mathematician, as this isn't a rigorous statement, but that's basically what O(...) is trying to convey.

不一样的天空 2024-08-25 07:53:38

这些系数与 Big O 表示法无关,因此只是 O(n2)。 正如维基百科所解释的

[...]如果我们与任何其他顺序的表达式(例如包含项 n3 或 n2 的表达式)进行比较,系数将变得无关紧要。< /p>

The coefficients aren't relevant in Big O notation, so it's just O(n2). As Wikipedia explains:

[...] the coefficients become irrelevant if we compare to any other order of expression, such as an expression containing a term n3 or n2.

抚你发端 2024-08-25 07:53:38

每个阅读或撰写有关算法复杂性的文章的人都应该确切地知道 Landau 符号渐近符号 是,否则他们并不真正理解发生了什么,或者只是有一个近似值(并且通常误导)的想法。

为了简化(很多),让 fg 是两个函数 f : N -> Ng : N -> N。当且仅当存在常数 M > 时,我们才说 f 是 O(g)。 0 使得 |f(n)| < M|g(n)|,对于所有n > M。也就是说,更通俗地说,从 n 的大值开始,所有值 f(n) 都小于 g(n) 的倍数code> (即,g f 增长得更快)。

该定义相当于

f is O(g) <==> There is K >= 0 such that lim{n -> +oo} |f(n)|/|g(n)| = K

所以,让我们采用 f(n) = 4n^2 + 7ng(n) = n^2,并尝试证明 f 是 O(g) (我将省略 {n -> +oo}):

lim |f(n)|/|g(n)| = lim f(n)/g(n) = lim (4n^2 + 7n) / n^2 = 4 + lim 7n/n^2 =
                  = 4 + lim 7/n = 4 + 0 = 4

这意味着存在一个 M 使得 n>中号==> |f(n)| < M|g(n)|,因此 f 是 O(g)

因此,从技术上来说,说 4n^2 + 7n 是 O(4n^2) 是正确的,因为说 4n^2 + 7n 是 O(n^3) 是正确的。 code>、4n^2 + 7n 是 O(e^n),依此类推。但为了有意义,我们对下界感兴趣。因此,如果 f 是 O(e^n) 并且 f 是 O(n^2),我们更感兴趣的是知道 f 是 O(n^ 2),因为这限制性更大。

非常重要的注意

事项选择算法时极其重要的是要理解大O符号指的是渐近情况,也就是说,当您考虑极其难以想象的情况时巨大的输入,远远超出了已知宇宙中可用的计算能力(即无限的输入集,在数学上用{n -> +oo}表示)。

对于实际用途(即,不是如此巨大的输入),在选择算法时,当然,您会观察候选算法big-O符号,但您必须确保选择的算法非常适合您的(预期)输入(并且性能更好)。

最后,通常性能更好的算法更难以理解和正确实施。在选择算法时,您也必须考虑这一事实(即,我将花在调试和修复该算法的实现上的时间大大优于我必须等待另一个算法的时间)算法,具有更差的大 O 表示法? 如果是这样,您应该考虑更简单、效率较低的算法,因为整体解决方案会更有效)。

Everyone reading or writing about complexity of algorithms should know exactly what the Landau Symbols and asymptotic notations are, otherwise they don't really understand what is going on, or simply have an approximate (and often misleading) idea.

To simplify (a lot), let f and g be two functions f : N -> N and g : N -> N. We say that f is O(g) if and only if there's a constant M > 0 such that |f(n)| < M|g(n)|, for all n > M. That is, more informally, starting from a big value of n, all the values f(n) are smaller than a multiple of g(n) (ie, g grows faster than f).

That definition is equivalent to

f is O(g) <==> There is K >= 0 such that lim{n -> +oo} |f(n)|/|g(n)| = K

So, let's take f(n) = 4n^2 + 7n and g(n) = n^2, and try to prove f is O(g) (I will omit {n -> +oo}):

lim |f(n)|/|g(n)| = lim f(n)/g(n) = lim (4n^2 + 7n) / n^2 = 4 + lim 7n/n^2 =
                  = 4 + lim 7/n = 4 + 0 = 4

This implies that there is a M such that n > M ==> |f(n)| < M|g(n)|, and thus f is O(g).

So technically it is correct to say 4n^2 + 7n is O(4n^2), as it is correct to say 4n^2 + 7n is O(n^3), 4n^2 + 7n is O(e^n), and so on. But to be meaningful, we are interested in the lower bound. So if f is O(e^n) and f is O(n^2), we are more interested into knowing that f is O(n^2), since this is much more restrictive.

VERY IMPORTANT note

What is extremelly important when choosing an algorithm is to understand that big-O notations refers to asymptotic cases, that is, when you consider extremely, unimaginable huge inputs, that can go well beyond the computational power available in the known universe (ie, infinite input sets, expressed mathematically by {n -> +oo}).

For practical uses (ie, not so huge inputs), when choosing an algorithm, sure, you will observe candidate algorithms big-O notations, but you must be sure that the chosen algorithm is well adapted (and performs better) for your (expected) input.

Finally, usually better performing algorithms are more difficult to understand and to implement properly. You must consider this fact as well when choosing an algorithm (ie, is the time I will spend debugging and fixing my implementation of this algorithm considerably superior to the time I would have to wait with another algorithm, with a worse big-O notation?. If so, you should consider the simpler, less efficient algorithm, as the overall solution would be more efficient).

仙气飘飘 2024-08-25 07:53:38

是 O(n^2)。常数因子“移入O”。您只保留最大的指数,因为这是主导的指数。并且您可以省略系数,因为在比较不同的算法时,即使非常大的系数也会比具有较大指数(n 足够大)产生的总数更小。

It's O(n^2). Constant factors "move into the O". You only keep the largest exponent since this is the one dominating. And you can leave out coefficients since when comparing different algorithms even very large coefficients result in smaller total numbers than having a larger exponent (with n large enough).

ま昔日黯然 2024-08-25 07:53:38

像这样的语句

4n² + 7n = O(n²)

意味着对于某个常数乘数c,表达式cn²最终将超过4n² + 7n。从技术上讲,将系数保留在那里并没有错 - O(n²)O(4n²) 意味着完全相同的事情,因为任何常数 c前者的 > 可以用 c/4 代替后者。然而,这样的事情不太清楚,可能会产生误导,而且绝对不标准。

A statement like

4n² + 7n = O(n²)

means that for some constant multiplier c, the expression cn² will eventually overtake 4n² + 7n. It's technically not incorrect to leave the coefficient in there — O(n²) and O(4n²) mean the exact same thing, because any constant c for the former can be replaced by c/4 for the latter. However, such a thing is less clear, possibly misleading, and definitely nonstandard.

柠栀 2024-08-25 07:53:38

从数学上来说,你可以写成 O(4n²)。这意味着算法的复杂度函数表现为 n->4n² 趋于正无穷。

但在计算机科学/算法中,你只会写 O(n²),这足以对你的算法进行分类。

Mathematically speaking, you would write O(4n²). It means that the complexity function of your algorithms behaves as n->4n² towards positive infinite.

But in computer science/algorithm, you would only write O(n²), which is sufficient to categorize your algorithm.

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