这个函数复杂度高吗?
我不确定以下问题:
Is loga(nb) in O(logb(na)) 对于常数 a, b?
I am not sure about the following question:
Is loga(nb) in O(logb(na)) for constants a, b?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当被问及函数 f(x) 是否在 O(g(x)) 中时,它实际上比较了这两个函数的增长率。 :http://en.wikipedia.org/wiki/Big_O_notation)
(参见维基百科 函数被忽略,因此 2x 的复杂度为 O(x)。此外,函数中具有较低增长率的组件同样会被忽略,因此 2x^2 + x + 1 的复杂度为 O(x^2)。
那么问题是:loga n^b 是否与 logb n^a 具有相似的增长率?
为了解决这个问题,我们将应用对数的几个很棒的属性:
首先要做的就是修复我们要比较的大 O 符号它不是最小的,通过应用上面的第一个属性,我们得到:
O(logb n^a) = O(a logb n) 因为从大 O 符号中删除了常数系数,所以增长率的真实表示是:
O(logb n)。
现在将第一个恒等式应用于我们得到的第一个公式:
loga n^b = b loga n
接下来我们使用我们得到的第二个属性更改基数:
loga n^b = b (logb n) / (logb a)
这也可以组织成如下形式:
loga n^b = (b / logb a) logb n
请注意 (b / logb a) 是常数系数,因此 (b / logb a) logb n 是 O(logb n)
所以答案到问题是是的。 loga n^b 的复杂度为 O(logb n^a)。
When asked if a function f(x) is in O(g(x)) it really compares the rate of growth of those two functions. (see wikipedia: http://en.wikipedia.org/wiki/Big_O_notation)
Constant factors of the functions are ignored so 2x is in O(x). Also components of the function that have lower growth rates are similarly ignored so 2x^2 + x + 1 is in O(x^2).
So the question is: does loga n^b have a similar growth rate as logb n^a?
To solve this we will apply a couple of awesome properties of logarithms:
First thing to do is to fix the big O notation we are comparing to as it is not minimal, by applying the first property above we get:
O(logb n^a) = O(a logb n) because constant coeficient are removed from big O notations the real representation of the rate of growth is:
O(logb n).
Now applying the first identity to the first formula we have:
loga n^b = b loga n
next we change the base using the second property we get:
loga n^b = b (logb n) / (logb a)
this can also be organized to look like:
loga n^b = (b / logb a) logb n
note that (b / logb a) is a constant coeficient therefore (b / logb a) logb n is in O(logb n)
So the answer to the question is yes. loga n^b is in O(logb n^a).
让我们将第一个表达式写为 b*loga(n),第二个表达式写为 a*logb(n)。
第一个相当于 b*log(n)/log(a),第二个相当于 a*log(n)/log(b)。
因此,假设是:“是否存在整数 n0 和 k,使得对于所有 n>n0,b*log(n)/log(a) < ; k*a*log(n)/logb ?"
稍微简化一下,那就是:“... b/log(a) < k*a/log(b) ?”
进一步重新排列,我们有:“... b*log(b) < k*a*log(a) ?”
因此,答案取决于“a”和“b”是什么。如果 b <= a,则为“是”。
Let us write the first expression as b*loga(n) and the second as a*logb(n).
The first one is equivalent to b*log(n)/log(a) and the second one is a*log(n)/log(b).
So, the hypothesis is: "Are there an integers n0 and k such that for all n>n0, b*log(n)/log(a) < k*a*log(n)/logb ?"
With a bit of simplification, that would be: "... b/log(a) < k*a/log(b) ?"
With further rearrangements, we have: "... b*log(b) < k*a*log(a) ?"
Hence, the answer depends on what "a" and "b" are. It is a "yes" if b <= a.