Module 16 of 39

Amortized analysis: the cost of many operations

Explain amortized cost with a growing array and distinguish a sequence guarantee from an average over random inputs.

An array-backed list has room for some number of items, called its capacity. Appending an item while space remains is cheap. When the array fills, it may allocate a larger array, copy every existing item, then append. That one append is expensive. Does that mean appending is generally expensive?

Background for this page. You should understand arrays, capacity, and sums. Amortized cost bounds a sequence of operations; it does not require random inputs or random choices. Related explanations: Arrays, linked lists, stacks, and queues · Summations for algorithm analysis.

Not if the capacity grows by a fixed factor, such as doubling. Starting at capacity 1, the copies happen at sizes 1, 2, 4, 8, and so on. By the time the list has grown to n items, the total number of items copied during expansions is less than 2n. Add the n ordinary writes for the appends, and n appends take O(n) total work. The amortized cost per append is therefore O(1), even though a particular append can take Θ(n).

Copying happens at capacities 1, 2, 4, 8 and so on.

This is a guarantee about a sequence of operations, not an average over random inputs. We do not need to assume that appends arrive in a lucky order. An occasional expensive operation is paid for by the many cheap ones between expansions.

There are three common ways to make this reasoning precise. Aggregate analysis bounds the total cost of the whole sequence, as we just did. The accounting method charges each cheap append a little extra and saves that credit to pay for a future copy. The potential method assigns a nonnegative stored value to the current state; a cheap operation can raise it, and an expensive one can spend it. These are different ways to prove the same kind of bound, not three kinds of runtime behavior.

The growth rule matters. If capacity increased by only one slot each time, growing from 1 to n items would copy 1 + 2 + ... + (n − 1) items, which is Θ(n²) total. Doubling uses extra unused space in exchange for fewer copies. It also needs a rule for shrinking: shrinking on every deletion and expanding on every insertion near the same boundary can cause repeated copying. A good design uses separate thresholds for growing and shrinking.

Amortized analysis also applies to structures that occasionally rebuild, reorganize, or compress their representation. It is useful whenever the cost of one operation depends on work saved or delayed by earlier operations. It does not mean that an individual operation has a small worst-case latency. If a program must respond within a strict time limit on every request, an O(1) amortized bound alone may not be enough.

Count the whole sequence

Starting from capacity one, append eight items. Each append writes its new item, making eight writes. Expansions copy one, then two, then four old items, making seven more writes. The sequence uses fifteen item writes, even though the append that triggers the last expansion copies four items at once.

For n appends, copied items form a geometric sum smaller than 2n, while new-item writes add n. Total work is O(n), so the amortized append cost is O(1). A single append can still cost Θ(n). These statements concern different units of analysis.

Shrinking needs its own policy. Shrinking immediately when an array becomes half full can cause repeated growth and shrinkage near a threshold. Leaving a gap between the thresholds avoids that oscillation.

Practice. A list doubles capacity at 1, 2, 4, and 8 items. Before it can hold 16 items, how many old items have been copied at those four expansions? If every expansion instead adds one slot, what changes as the list grows?

Answer. The four doublings copy 1 + 2 + 4 + 8 = 15 old items in total. Adding one slot repeatedly copies a growing list at every step, producing quadratic total copying over many appends.

Try another case. Why does adding one capacity slot at each expansion lead to quadratic work?

Reasoning. It copies 1+2+…+(n−1) old items, whose sum is n(n−1)/2. Doubling replaces that arithmetic sum with a geometric one.

View the full course and choose another topic.

Image preview

Fit