讨论算法的时间复杂度时,“恒定分摊时间”是个什么鬼?

Q:What is meant by "Constant Amortized Time" when talking about time complexity of an algorithm?
讨论算法的时间复杂度时,“恒定分摊时间”是个什么鬼?

A1: Amortised time explained in simple terms:

分摊时间简单来说就是:

If you do an operation say a million times, you don't really care about the worst-case or the best-case of that operation - what you care about is how much time is taken in total when you repeat the operation a million times.

如果执行一个操作一百万次,你可能并不关心“最差”或“最好”的情况。你真正关心的是当你重复操作一百万次的时,总耗时是多少。

So it doesn't matter if the operation is very slow once in a while, as long as "once in a while" is rare enough for the slowness to be diluted away. Essentially amortised time means "average time taken per operation, if you do many operations". Amortised time doesn't have to be constant; you can have linear and logarithmic amortised time or whatever else.

所以这个操作偶尔的很慢并不那么重要,只要这个“偶尔”对于被稀释的慢很稀有的就好。实际上分摊时间意味着“每个操作的平均花费时间,如果你执行很多次”.

分摊时间没必要是恒定的,可能是线性时间、对数分摊时间或者其它时间。

Let's take mats' example of a dynamic array, to which you repeatedly add new items. Normally adding an item takes constant time (that is, O(1)). But each time the array is full, you allocate twice as much space, copy your data into the new region, and free the old space. Assuming allocates and frees run in constant time, this enlargement process takes O(n) time where n is the current size of the array.

让我们以“反复添加新元素的动态数组”为例。正常来说添加新元素花费常量时间(即O(1)).但每次数组满时,将会分配两倍的空间,拷贝原有数据至新区域,并释放旧空间。假设分配和释放需要常量时间,这个扩容动作花费O(n)时间,这里的n是当前数组的大小。

So each time you enlarge, you take about twice as much time as the last enlarge. But you've also waited twice as long before doing it! The cost of each enlargement can thus be "spread out" among the insertions. This means that in the long term, the total time taken for adding m items to the array is O(m), and so the amortised time (i.e. time per insertion) is O(1).

所以每次扩容的时候,要花费上次扩容时间的两倍envoy。但这样做之前你也等待了两倍长的时间。每次扩容的时间成本因此被“摊开”至插入动作中。这意味着长远来看,添加m个元素至动态数组的总时间是O(m),所以分摊时间(每次插入时间)是O(1)。

A2:It means that over time, the worst case scenario will default to O(1), or constant time. A common example is the dynamic array. If we have already allocated memory for a new entry, adding it will be O(1). If we haven't allocated it we will do so by allocating, say, twice the current amount. This particular insertion will not be O(1), but rather something else.

这意味着,随着时间推移,在最坏的情况下将默认为O(1),或固定的时间。最常见的例子是动态数组。如果已经为新条目分配好内存,添加动作将耗时O(1)。如果没有分配内存,则需要分配动作,比方说,耗时为当前的两倍。特定的插入动作耗时不是O(1),而是其它值。

What is important is that the algorithm guarantees that after a sequence of operations the expensive operations will be amortised and thereby rendering the entire operation O(1).

重要的是,算法保证在一系列的操作之后,耗时昂贵的操作时间被分摊,从而使整改操作的时间复杂度为O(1)。

Or in more strict terms,

There is a constant c, such that for every sequence of operations (also one ending with a costly operation) of length L, the time is not greater than c*L

或更严格的条件,

有一个常量c,对每一个长度为L的序列(以昂贵操作结束)操作之后,耗时将不大于 c*L 。

原文出处:http://stackoverflow.com/questions/200384/constant-amortized-time

posted @ 2015-08-20 19:02  envoy  阅读(1342)  评论(0编辑  收藏  举报