Constant Amortized Time

后端 未结 6 1186
太阳男子
太阳男子 2020-11-22 01:44

What is meant by \"Constant Amortized Time\" when talking about time complexity of an algorithm?

6条回答
  •  -上瘾入骨i
    2020-11-22 02:08

    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.

    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).

    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 (Thanks Rafał Dowgird)

提交回复
热议问题