简体   繁体   English

O(n + log(m)) 可以简化吗?

[英]Can O(n + log(m)) be simplified?

From my understanding, different variables are treated separately (but I might be wrong).据我了解,不同的变量是分开处理的(但我可能错了)。

I know O(n + log(n)) simplifies to O(n), and O(n + m) cannot be simplified, but what about O(n + log(m))?我知道 O(n + log(n)) 简化为 O(n),而 O(n + m) 不能简化,但是 O(n + log(m)) 呢?

Thanks for any help you can give!谢谢你提供的所有帮助!

Big O is asymptotic, so if you have condition fe m <= 2*n you may simplify this.大 O 是渐近的,所以如果你有条件 fe m <= 2*n 你可以简化它。 But without such condition you can not.但没有这样的条件你不能。

I think,我认为,

If m <= n^2 then O(n+log(m)) simplifies as O(n)如果 m <= n^2 则 O(n+log(m)) 简化为 O(n)

This is log base 2. Now take an example, let n = 8, now take m = 8^2 = 64 and log(64) = 6 < 8 so, The complexity will be O(n).这是以 2 为底的对数。现在举个例子,让 n = 8,现在取 m = 8^2 = 64 和 log(64) = 6 < 8,所以复杂度为 O(n)。 if m = 8^3 = 512, log(512) = 9 > n, then the complexity will be O(log(m))如果 m = 8^3 = 512, log(512) = 9 > n,那么复杂度将为 O(log(m))

If there no relation between n and m, then cannot simplify O(n + log(n)).如果 n 和 m 之间没有关系,则不能简化 O(n + log(n))。

Generally speaking, if you have any big-O of the form O(f(n, m)) where m and n are free and independent variables, there isn't a nice way to simplify the expression.一般来说,如果你有任何 O(f(n, m)) 形式的大 O,其中 m 和 n 是自由和自变量,那么就没有一种简化表达式的好方法。 That means that, say, O(m + n), O(m log n), O(log m+n n), O(m 137 + 2 n ), etc. can't be rewritten in terms of some simpler function that depends purely on m or n.这意味着,比如说,O(m + n)、O(m log n)、O(log m+n n)、O(m 137 + 2 n ) 等不能用更简单的方式重写function 完全取决于 m 或 n。 The reason for this is that big-O notation measures how some quantity grows for "sufficiently large" values of the inputs, and if m and n aren't related to one another then each of m and n can grow "sufficiently large" independently of one another.这样做的原因是,大 O 表示法衡量了一些数量如何在输入的“足够大”值下增长,如果 m 和 n 彼此不相关,则 m 和 n 中的每一个都可以独立地“足够大”增长彼此的。

There are some cases, though, where there is some known relationship between m and n.但是,在某些情况下,m 和 n 之间存在某种已知的关系。 For example, in graph algorithms, it's common to assume that m (the number of edges) is between n-1 and n 2 , representing the case where the graph is minimally connected and when the graph has the maximum possible number of edges.例如,在图算法中,通常假设 m(边数)在 n-1 和 n 2之间,表示图最小连接且图具有最大可能边数的情况。 In those cases, you do sometimes see some simplifications made.在这些情况下,您有时确实会看到一些简化。 If that relationship between m and n holds, for example, then you'll usually see O(log n) rather than O(log m) because log (n-1) ≤ m ≤ 2 log n in that case.例如,如果 m 和 n 之间的关系成立,那么您通常会看到 O(log n) 而不是 O(log m),因为在这种情况下 log (n-1) ≤ m ≤ 2 log n。 However, even then it's uncommon to replace m with n 2 because dense and sparse graphs are different cases and the runtimes may be better predicted with a more precise runtime analysis.然而,即使那样,用 n 2替换 m 也是不常见的,因为密集图和稀疏图是不同的情况,并且可以通过更精确的运行时分析更好地预测运行时。

Hope this helps!希望这可以帮助!

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

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