Example: Resizing a Dynamic Array
A dynamic array starts at size 1 and doubles when full.
Most insertions cost 1, but an insertion into a full array costs \Theta(k) to copy k elements into a new array of size 2k.
Yet n insertions cost O(n) total — copies are rare (\log_2 n times), so the average cost per operation is O(1).
Three techniques for amortized analysis:
All three give the same result; pick whichever is most intuitive.
Goal: Increment a k-bit binary counter
(mod 2^k).
Cost model: Number of bits flipped per INCREMENT.
Worst-case: \le k flips per increment \Rightarrow O(nk) for n ops.
But not all increments flip k bits — can we do better?
Aggregate Method: Sum up total cost of sequence, divide by n.
Starting from zero, in n INCREMENT operations:
Total flips in n INCREMENTs = \displaystyle\sum_{j=0}^{k-1} \left\lfloor \frac{n}{2^j} \right\rfloor < 2n = O(n).
Proof. \sum_{j=0}^{k-1} \left\lfloor \frac{n}{2^j} \right\rfloor < n \sum_{j=0}^{\infty} \frac{1}{2^j} = 2n
So the amortized cost per operation is O(1) — much better than the worst-case O(k) per operation!
Credit invariant: Total credits in the data structure at any point \ge 0.
\sum_{i=1}^{t} \hat{c}_i - \sum_{i=1}^{t} c_i \ge 0 \quad \forall t
One credit pays for one bit flip.
Accounting scheme:
Analysis: Each INCREMENT flips at most one bit from 0 \to 1. So amortized cost per INCREMENT \le 2.
Total actual cost of n operations \le total amortized cost \le 2n = O(n). ✓
Requirements:
Amortized cost: \hat{c}_i = c_i + \Phi(D_i) - \Phi(D_{i-1}) The amortized cost = actual cost + change in potential.
Total amortized cost: \sum_{i=1}^{n} \hat{c}_i = \sum_{i=1}^{n} c_i + \Phi(D_n) - \Phi(D_0)
Potential: \Phi(D_i) = b_i, the number of 1’s in the counter after i^{\text{th}} increment.
Analysis: Suppose i^{\text{th}} INCREMENT resets t_i bits (from 1 \to 0) and sets one bit 0 \to 1.
Amortized cost: \hat{c}_i = c_i + \Phi(D_i) - \Phi(D_{i-1}) \le (t_i + 1) + (1 - t_i) = 2
Thus total amortized cost of n INCREMENTs is O(n). ✓
| Method | Key Idea | Binary Counter Result |
|---|---|---|
| Aggregate | \frac{1}{n}\sum c_i | 2n total, O(1) per op |
| Accounting | Overcharge cheap ops, save credits | 2 per INCREMENT |
| Potential | \hat{c}_i = c_i + \Delta\Phi | \Phi = \#\text{ones}, \hat{c} \le 2 |
All three methods show: n INCREMENT operations on a binary counter cost O(n) total — O(1) amortized per operation.
CS F364: Design & Analysis of AlgorithmsTulasimohan Molli