Definition
Amortized analysis is a method for bounding the cost of operations on a data structure by bounding the total cost of every possible sequence of them. If every sequence of operations, started from the initial state of the structure, costs at most , each operation is said to have amortized cost , even if a single operation costs far more than . The bound uses no probabilities: unlike an average-case bound, it holds for every sequence, including one chosen by an attacker.
The idea
A dynamic array §14.5 keeps its elements side by side in one block of memory, with room to spare. Appending an element usually writes it into the next free slot: one step. When the block is full, the next append must reserve a block twice as large, copy every element across, and only then write the new one; for an array of elements, that one append copies all 1,048,576. Judged one operation at a time, an append to an array of elements costs up to steps, which suggests that appends could cost about steps. That estimate is far too pessimistic.
The expensive appends are rare, and each follows many cheap ones. Between a doubling from slots to slots and the next doubling, about appends happen, and the next copy moves elements: about two per append in that stretch. So charge every append 3 units, 1 to write its own element and 2 set aside for the next copy, and the savings always cover the copy when it comes, much as a fixed sum set aside each month pays a large annual bill. That picture stops at one point: the program saves nothing. The savings exist only in the proof, as bookkeeping showing that the total cost never exceeds the total charged.
How it works
Amortized analysis has three standard methods, which prove the same kind of statement.
Aggregate method: bound the total actual cost of any sequence of operations directly, then divide: each operation has amortized cost .
Accounting method: give each operation a fixed charge. A charge above the actual cost is stored as credit, and an operation that costs more than its charge pays the difference from the credit. The proof must show that the credit never becomes negative.
Potential method: define a number (the Greek capital letter phi), the potential, computed from the current state of the data structure, 0 for the initial state and never negative. The amortized cost of an operation is its actual cost plus the change in it causes. Cheap operations raise ; expensive ones lower it, and the drop pays for their extra work.
For the dynamic array, count one unit for every element written: the new element, and each element copied into a new block. Reserving and releasing memory is not counted; its time, and the chance that it fails, must be considered separately. Start from an empty array with capacity 0, the capacity being the number of slots reserved. The first append reserves 1 slot, and each later append that finds the array full reserves twice the old capacity. The capacities are therefore 1, 2, 4, 8 and so on, and the copies made on reaching them move 0, 1, 2 and 4 elements.
The growth must be by a constant factor. Growing by a fixed 4 slots instead makes the array copy itself after every 4 appends, with longer copies each time: appends copy about elements, about , so each append costs amortized, proportional to . Any constant factor greater than 1 keeps the amortized cost constant; common choices are 1.5 and 2.
Worked example
Append 8 elements to an empty array. The eight appends cost:
append 1: capacity 0 becomes 1; write 1 element: cost 1;
append 2: full; capacity 1 becomes 2; copy 1, write 1: cost 2;
append 3: full; capacity 2 becomes 4; copy 2, write 1: cost 3;
append 4: a free slot: cost 1;
append 5: full; capacity 4 becomes 8; copy 4, write 1: cost 5;
appends 6, 7 and 8: free slots: cost 1 each.
The total is units, the same as 8 writes plus copies. The average is units per append, yet append 5 alone cost 5 units, five times an ordinary append.
The potential method gives a bound for sequences of every length. Let be the number of elements, the capacity, and . The empty array has , so . Append 5 starts with , , so , and ends with , , so . Its amortized cost is its actual cost plus the change in potential: . Append 6 costs 1 and raises from 2 to : amortized cost . Append 1 costs 1 and raises from 0 to : amortized cost 2. Each of the other appends also has amortized cost 3, so the eight add up to . The final state has , and the actual total is the amortized total minus the final potential, , as counted directly.
Now attach times, using an invented figure: suppose writing one element takes 10 ns. An ordinary append then takes 10 ns. The append that grows an array of elements writes elements and takes ns, about 10.5 ms, roughly a million times longer. The amortized cost of every append is still at most 3 units, 30 ns.
Stated precisely
Let operations run in order from the initial state , let be the actual cost of operation , and let be the state after it. Let map each state to a real number, with and for every state that can be reached. The amortized cost of operation is When these are added for to , each intermediate potential appears once with a plus sign and once with a minus sign and cancels, which leaves So the amortized costs bound the actual total of every sequence, at every point in it.
For the dynamic array, is never negative on any reachable state §3.21: before the first append , and afterwards the array is always at least half full, . An append that finds a free slot costs 1 and raises by 2: amortized cost 3. An append that finds the array full, with , costs , and falls from to : amortized cost . The first append has amortized cost 2. Hence any appends from empty cost at most units: amortized , bounded by a constant, per append. The worst single append into elements costs units, which is .
This bound rests on five assumptions: the sequence starts from the empty array; only appends occur, with no removals and no shrinking; the capacity grows by doubling; each element written costs one unit, whatever the element; and every request to reserve memory succeeds.
Limits and common mistakes
Amortized is not average-case. No probabilities are involved and no input is assumed to be typical. The bound holds for every sequence, but only for the sequence as a whole.
An amortized bound does not bound one operation. The append that copies elements still takes time proportional to . A system with a deadline for every operation, such as a hard real-time §6.34 controller, needs a worst-case bound per operation. Two common fixes are to reserve the full capacity in advance, or to spread each copy over the following appends, copying a few old elements on each while both blocks exist; the second is called deamortization §14.40.
Adding removals can break the bound. If the array halves its capacity as soon as it is less than half full, then two appends followed by two removals, repeated at the boundary, resize the array again and again, each resize costing . Shrinking only when the array falls below one quarter full restores the amortized cost.
The cost model must match the real costs. If copying an element costs much more than writing one, or if reserving memory can take long or fail, the unit count no longer describes the time, and a failed reservation needs its own handling.
Growing moves the elements. A pointer saved before a resize still points into the old block, which has been released; using it is a use-after-free §4.6. Right after a doubling, almost half of the capacity is unused: that is the memory price of the constant amortized cost.
The same reasoning applies to other mechanisms with rare long pauses, such as garbage collection §8.16, which reclaims unused memory in occasional large batches: a low amortized cost can hide individual long pauses.
Questions answered
Why can constant amortized cost coexist with one long latency spike?
Constant amortized cost can coexist with one long latency spike because an amortized bound limits the total cost of a sequence of operations, not the cost of each operation: one operation may cost far more than its share, as long as the cheap operations before it have paid for it. In a dynamic array that doubles its capacity (its number of reserved slots) when full, an append usually writes one element, but the append that finds the array full copies all elements into a block twice the size. That append is the spike. The roughly cheap appends since the previous doubling each set aside 2 units toward the copy, so every sequence of appends from empty costs at most units, a constant amortized cost of 3 per append. The total stays small, and one operation is large.
Code with a deadline for each operation therefore needs a worst-case bound per operation, not an amortized one: for example, reserve the capacity in advance, or spread each copy across the following operations, which is called deamortization.
Related entries: Asymptotic complexity (§14.2), Array (§14.5), Availability (§1.10), Deamortization: spreading array migration while preserving lookup meaning (§14.40), Garbage collection (§8.16), Worst-case versus average-case complexity (§14.3).