算法的时间复杂度:陈述对数的底数重要吗?


Answers:


63

这取决于对数在哪里。如果这只是一个因素,那没有什么区别,因为big-O或允许您乘以任何常数。θ

如果采用则基数很重要。在基数2中,您只有,在基数10中,它约为。O(2logn)O(n)O(n0.3010)


5
我想这是只打算拿出类似。我看不出有什么理由将数字表示为2clogbn,而不是表示n(无论是作为计算的中间阶段)。2logn2clogbnn
David Richerby

7
+1为“常数对指数很重要”
trognanders,

50

因为渐进符号忽略了常数因子,并且任何两个对数相差一个常数因子,所以底数没有区别:对所有a logan=Θ(logbn)b > 1。因此,在使用渐近表示法时,无需指定对数的底数。a,b>1


13
我更愿意看到代替==
Nayuki

16
恐怕标准符号使用=
Yuval Filmus

4
@YuvalFilmus标准表示法具有误导性,与其他地方的标准完全不同,并且使算法复杂性似乎与与此完全相似的事物完全不同。“这是标准表示法”绝不应该成为支持差的解决方案而不是更好的,同样清晰的解决方案的理由。(无论如何,通常从上下文中可以清楚地看出该符号的含义。)
wizzwizz4

7
@ wizzwizz4常见做法是一个极好的原因。它促进了有效的沟通。这就是我们所有人忍受英语拼写怪癖的原因。
Yuval Filmus

3
有时只是有太多的东西有更清晰比登录一个 ñ = Θ 日志b ñ nloganΘ(nlogbn)logan=Θ(logbn)
Jik

15

logxy=1logyxlogxy=logzylogzx,因此 loganlogbn=lognblogna=logab。由于logab为正常数(对于所有a,b>1),因此logan=Θ(logbn)


8

在大多数情况下,删除对数的底数是安全的,因为,正如其他答案所指出的那样,对数的基础更改公式意味着所有对数都是彼此的常数倍。

在某些情况下,这样做是不安全的。例如,@ gnasher729指出,如果指数中有一个对数,则对数底数的确很大。

bbΘ(n+b)logbUUO((n+b)logbU). For any fixed integer b this simplifies to O(nlogU). However, what happens if b isn't a constant? A clever technique is to pick b=n, in which case the runtime simplifies to O(n+lognU). Since lognU = logUlogn, the overall expression simplifies to O(nlogUlogn). Notice that, in this case, the base of the logarithm is indeed significant because it isn't a constant with respect to the input size. There are other algorithms that have similar runtimes (an old analysis of disjoint-set forests ended up with a term of logm/2+2 somewhere, for example), in which case dropping the log base would interfere with the runtime analysis.

Another case in which the log base matters is one in which there's some externally-tunable parameter to the algorithm that control the logarithmic base. A great example of this is the B-tree, which requires some external parameter b. The height of a B-tree of order b is Θ(logbn), where the base of the logarithm is significant in that b is not a constant.

To summarize, in the case where you have a logarithm with a constant base, you can usually (subject to exceptions like what @gnasher729 has pointed out) drop the base of the logarithm. But when the base of the logarithm depends on some parameter to the algorithm, it's usually not safe to do so.

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.