Aggregate Analysis
Aggregate analysis is the simplest amortised analysis technique: compute the total cost of a sequence of operations and divide by . If some operations are cheap and others expensive, the average may be far lower than the per-operation worst-case bound.
Amortised vs. Worst-Case vs. Average-Case
- Worst-case: maximum cost of a single operation over all possible inputs. Pessimistic.
- Average-case: expected cost of a single operation, assuming a probability distribution over inputs. Requires assumptions about input distribution.
- Amortised: the average cost per operation over a worst-case sequence of operations. Guarantees the total cost of any sequence, not probabilistic.
Amortised analysis is essential when a data structure occasionally does expensive work (resizing, rebalancing, cleanup) that is paid for by many cheap operations.
The Binary Counter
A -bit binary counter increments from to . The counter is an array of bits , with the least significant bit.
INCREMENT(A):
i = 0
while i < k and A[i] == 1:
A[i] = 0
i = i + 1
if i < k:
A[i] = 1
A single INCREMENT may flip up to bits (e.g. flips 4 bits). Worst-case cost: . Starting at 0 and performing increments, the total number of bit flips is:
Bit flips every increments. Over increments, the number of flips of bit is .
Total flips . Amortised cost per INCREMENT: .
Even though a single INCREMENT may cost , over many operations the average is constant.
The Dynamic Array (Vector)
A dynamic array grows by doubling its capacity when full. Starting from capacity 1:
| Insert # | Capacity | Copies | Notes |
|---|---|---|---|
| 1 | 1 | 0 | |
| 2 | 2 | 1 | Resize 1→2, copy 1 element |
| 3 | 4 | 2 | Resize 2→4, copy 2 elements |
| 4 | 4 | 0 | |
| 5 | 8 | 4 | Resize 4→8, copy 4 elements |
| … | … | … | |
After appends starting from capacity 1, resizes occur at sizes . Total copies:
Each copy costs , so total copying cost is . Adding the per-insert cost for the inserts gives total . Amortised per insert: .
This proves a result assumed in Part IA Algorithms I (the stack ADT): push is amortised.
General Pattern
Aggregate analysis works well when the expensive operations occur at predictable, sparse intervals (powers of 2, Fibonacci numbers, etc.). Sum the geometric or arithmetic series, bound the total, divide by .
Limitations: the aggregate method gives a single amortised cost per operation averaged across all operation types. It cannot assign different amortised costs to different operations (accounting and potential methods can). For data structures with multiple operation types, the accounting and potential methods are more flexible.
Summary
| Method | Approach | When to use |
|---|---|---|
| Aggregate | Total cost | Simple operations, single type |
| Accounting | Assign charges, track credit | Multiple op types, credit attribution |
| Potential | function on data structure state | Complex interactions, formal proofs |
Past Tripos: y2024p1q9, y2023p2q7.