简体   繁体   English

显示摊销分析和平均案例分析可能给出渐近不同结果的示例

[英]An example to show that amortized analysis and average-case analysis may give asymptotically different results

I have read many explanations of amortized analysis and how it differs from average-case analysis.我读过很多关于摊销分析的解释以及它与平均案例分析的区别。 However, I have not found a single explanation that showed how, for a particular example for which both kinds of analysis are sensible, the two would give asymptotically different results.然而,我还没有找到一个单一的解释来说明,对于一个特定的例子,对于这两种分析都是明智的,两者会如何给出渐近不同的结果。

The most wide-spread example of amortized running time analysis shows that appending an element to a dynamic array takes O(1) amortized time (where the running time of the operation is O(n) if the array's length is an exact power of 2, and O(1) otherwise).摊销运行时间分析最广泛的示例表明,将元素附加到动态数组需要 O(1) 摊销时间(如果数组的长度是 2 的精确幂,则操作的运行时间为 O(n) ,否则为 O(1))。 I believe that, if we consider all array lengths equally likely, then the average-case analysis will give the same O(1) answer.我相信,如果我们考虑所有数组长度的可能性相同,那么平均情况分析将给出相同的 O(1) 答案。

So, could you please provide an example to show that amortized analysis and average-case analysis may give asymptotically different results?那么,您能否提供一个例子来说明摊销分析和平均案例分析可能会给出渐近不同的结果?

Consider a dynamic array supporting push and pop from the end.考虑一个支持从末尾pushpop的动态数组。 In this example, the array capacity will double when push is called on a full array and halve when pop leaves the array size 1/2 of the capacity.在此示例中,当对完整数组调用push时,数组容量将加倍,而当pop将数组大小保留为容量的 1/2 时,数组容量将减半。 pop on an empty array does nothing.在空数组上pop什么也不做。

Note that this is not how dynamic arrays are "supposed" to work.请注意,这不是动态 arrays “应该”工作的方式。 To maintain O(1) amortized complexity, the array capacity should only halve when the size is alpha times the capacity, for alpha < 1/2.为了保持 O(1) 摊销的复杂性,数组容量应仅在大小为容量的 alpha 倍时减半,因为 alpha < 1/2。

In the bad dynamic array, when considering both operations, neither has O(1) amortized complexity, because alternating between them when the capacity is near 2x the size can produce Ω(n) time complexity for both operations repeatedly.在糟糕的动态数组中,当考虑两个操作时,都没有 O(1) 摊销复杂度,因为当容量接近 2 倍大小时在它们之间交替可以为两个操作重复产生 Ω(n) 的时间复杂度。

However, if you consider all sequences of push and pop to be equally likely, both operations have O(1) average time complexity, for two reasons:但是,如果您认为所有pushpop序列的可能性相同,则这两个操作的平均时间复杂度为 O(1),原因有二:

  1. First, since the sequences are random, I believe the size of the array will mostly be O(1).首先,由于序列是随机的,我相信数组的大小主要是 O(1)。 This is a random walk on the natural numbers.这是对自然数的随机游走。

  2. Second, the array will be near size a power of 2 only rarely.其次,数组的大小很少会接近 2 的幂。

This shows an example where amortized complexity is strictly greater than average complexity.这显示了一个示例,其中摊销的复杂性严格大于平均复杂性。

They never have different asymptotically different results.他们永远不会有不同的渐近不同的结果。 average-case means that weird data might not trigger the average case and might be slower. average-case 意味着奇怪的数据可能不会触发平均情况并且可能会更慢。 asymptotic analysis means that even weird data will have the same performance.渐近分析意味着即使是奇怪的数据也会有相同的表现。 But on average they'll always have the same complexity.但平均而言,它们总是具有相同的复杂性。

Where they differ is the worst-case analysis.它们的不同之处在于最坏情况分析。 For algorithms where slowdowns come every few items regardless of their values, then the worst-case and the average-case are the same, and we often call this "asymptotic analysis".对于无论其值如何,每隔几个项目就会减速的算法,最坏情况和平均情况是相同的,我们通常将其称为“渐近分析”。 For algorithms that can have slowdowns based on the data itself, the worst-case and average-case are different, and we do not call either "asymptotic".对于基于数据本身可能会变慢的算法,最坏情况和平均情况是不同的,我们不称之为“渐近”。

In "Pairing Heaps with Costless Meld" , the author gives a priority queue with O(0) time per meld.“Pairing Heaps with Costless Meld”中,作者给出了一个优先队列,每个 meld 的时间为 O(0)。 Obviously, the average time per meld is greater than that.显然,每次融合的平均时间要长于此。

Consider any data structure with worst-case and best-case inserts and removes taking I and R time.考虑任何具有最坏情况和最佳情况插入和删除的数据结构,花费 I 和 R 时间。 Now use the physicist's argument and give the structure a potential of nR, where n is the number of values in the structure.现在使用物理学家的论证并为结构赋予 nR 的势能,其中 n 是结构中值的数量。 Each insert increases the potential by R, so the total amortized cost of an insert is I+R.每个插入都会增加 R 的潜力,因此插入的总摊余成本为 I+R。 However, each remove decreases the potential by R. Thus, each removal has an amortized cost of RR=0!然而,每次移除都会减少 R 的潜力。因此,每次移除的摊销成本为 RR=0!

The average cost is R;平均费用为R; the amortized cost is 0;摊余成本为 0; these are different.这些是不同的。

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

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