The k-bit Counter: A Worked Example
The -bit binary counter is the canonical example for demonstrating all three amortised analysis methods on the same data structure. The contrast between worst-case and amortised cost is stark: a single INCREMENT can flip bits, but over increments, the average is constant.
Structure and Operation
A -bit counter stores an integer in binary. Bits are indexed (LSB) to (MSB). An INCREMENT operation walks from bit 0 upward, flipping 1’s to 0 until it finds a 0, which it flips to 1.
INCREMENT(A):
i = 0
while i < k and A[i] == 1:
A[i] = 0 // flip 1 to 0
i = i + 1
if i < k:
A[i] = 1 // flip 0 to 1
Worst-case: incrementing from (all 1’s: ) flips all bits, costing .
Aggregate Analysis
Bit (counting from LSB as bit 0) flips every increments. Over increments (with ), the number of flips of bit is:
Total flips:
Amortised per INCREMENT: .
Concrete trace: ,
| Bit | Flip interval | Flips in 16 increments |
|---|---|---|
| 0 | Every 1 | 16 |
| 1 | Every 2 | 8 |
| 2 | Every 4 | 4 |
| 3 | Every 8 | 2 |
| Total | 30 |
. Amortised: .
Accounting Method
Amortised cost per INCREMENT: .
Rules:
- Flipping 0 to 1: actual cost 1, amortised 2 (stores 1 credit on this bit)
- Flipping 1 to 0: actual cost 1, amortised 0 (consumes the credit stored on this bit)
Invariant: every 1-bit in the counter has 1 credit.
Verification for INCREMENT with trailing 1’s: flips of 1→0 consume credits (net 0 amortised each). One flip of 0→1 costs 1 and stores 1 credit (net 2 amortised). Total amortised: . Credits never go negative because we only flip a 1 to 0 if it existed (and thus had a credit).
Step-by-step trace: 0 → 1 → 2 → 3 → 4
| Increment | State | Actual cost | Bits flipped | Amortised | Credits after |
|---|---|---|---|---|---|
| 0→1 | 0001 | 1 | 0→1 | 2 | 1 on bit 0 |
| 1→2 | 0010 | 2 | 1→0, 0→1 | 2 | 1 on bit 1 |
| 2→3 | 0011 | 1 | 0→1 | 2 | 1 on bits 0,1 |
| 3→4 | 0100 | 3 | 1→0, 1→0, 0→1 | 2 | 1 on bit 2 |
Amortised consistently 2, actual varies 1-3.
Potential Method
Potential: .
Clearly (counter starts at 0) and .
For an INCREMENT converting trailing 1’s to 0 and one 0 to 1, the actual cost is . The number of 1-bits changes from to , so:
Amortised cost:
Again, amortised.
Trace: 0111 → 1000 (increment from 7 to 8)
- Before: state 0111, (three 1’s)
- Actual cost: flip bits 0,1,2 from 1→0 (cost 3), flip bit 3 from 0→1 (cost 1), total
- After: state 1000, (one 1)
The drop in potential () offsets the unusually high actual cost (), keeping the amortised cost constant.
Contrast: Worst-Case Single Operation
Increment from (binary all 1’s) flips all bits. Actual cost: . But this happens only once per increments. The amortised analysis correctly averages this spike over the entire cycle.
Summary
| Method | Approach | Result |
|---|---|---|
| Aggregate | Total flips , divide by | per increment |
| Accounting | Charge 2, credit on 1-bits | per increment |
| Potential | of 1’s, | per increment |
| Worst-case | Single increment at all 1’s | per increment |
All three methods confirm amortised per INCREMENT, whilst the worst case is . This is the textbook demonstration that amortised analysis can reveal dramatically better average performance than worst-case analysis suggests.
Past Tripos: y2024p1q9, y2023p2q7.