Amortized Analysis
Amortized analysis bounds the average cost per operation over a sequence of operations, without assuming a probability distribution over inputs.
Why We Need It
A single operation may occasionally cost Θ(n), while most operations cost Θ(1). Looking only at the worst case of one operation can therefore be misleading.
For a sequence of m operations with total cost T(m), the amortized cost per operation is bounded by T(m)/m.
Classic Example: Dynamic Array
Appending to a dynamic array normally costs Θ(1). When capacity is full, the array is resized and all existing elements are copied.
If capacity doubles each time, n appends cost Θ(n) in total, so append has amortized Θ(1) cost.
Aggregate Method
Analyze the total cost of a complete operation sequence, then divide by the number of operations.
For geometric resizing, copied elements form a geometric series: 1 + 2 + 4 + ... < 2n.
Accounting Method
Charge some cheap operations more than their actual cost and store the extra amount as credit.
The stored credit later pays for expensive operations. The credit balance must never become negative.
Potential Method
Represent stored work with a potential function Φ(D).
If Φ starts at 0 and never becomes negative, the sum of amortized costs upper-bounds the sum of actual costs.
Other Standard Examples
- Stack with
MULTIPOP: amortized Θ(1) per stack operation. - Binary counter increments: amortized Θ(1) bit flips per increment.
- Union-Find: almost constant amortized cost with path compression and union by rank.
- Splay trees: amortized O(log n) access operations.
Interactive Dynamic Array Demo
Append elements and observe occasional resize operations. The expensive copies are spread over the full sequence.
Start with capacity 1.
Key Takeaway
Amortized analysis explains why a data structure can provide strong long-run guarantees even when individual operations occasionally have high worst-case cost.