Sub topic 4
Accounting method: charging each append a fixed price
This chapter assumes you know Big-O notation, how a doubling dynamic array works, and the aggregate method from the previous chapter. The aggregate method totals the cost of all n appends and then divides. The accounting method works differently. It sets a fixed price per operation up front and shows that the prices always cover the real costs.
The idea
Give each operation an amortised charge. If the real cost is lower than the charge, put the difference into a bank as credit. If the real cost is higher, pay the extra from the bank. The analysis holds if the bank never goes negative. In that case, the total of the charges is an upper bound on the total of the real costs, so each charge is a valid amortised cost.
Credit can be pictured as sitting on specific elements, which makes the proof easier to follow.
Choosing the price for appends
Take a doubling array. A normal append costs 1 unit for the write. An append into a full array of size c also copies c elements first. Charge 3 units per append.
- 1 unit pays for writing the new element.
- 1 unit stays on the new element, to pay for copying that element at the next resize.
- 1 unit stays on the new element, to pay for copying one element that already has no credit left.
Consider the moment a full array of capacity c is resized to 2c. Just after the previous resize, the array held c/2 elements, and none of them had credit left. Since then, c/2 appends have filled it. Each of those appends left 2 credits, so the bank holds c/2 × 2 = c credits. The resize copies exactly c elements, and the bank pays for it. After the copy, the bank is empty. The new array has capacity 2c and c elements, and the cycle starts again.
Each append is charged 3, so n appends cost at most 3n in total. The amortised cost per append is O(1).
Checking the bank in code
You can make the bank explicit and assert that it never goes negative.
class AccountedArray:
CHARGE = 3 # amortised price of one append
def __init__(self):
self._capacity = 1
self._size = 0
self.bank = 0 # unspent credit
def append(self, value):
self.bank += self.CHARGE
if self._size == self._capacity:
self.bank -= self._size # pay to copy every element
self._capacity *= 2
self.bank -= 1 # pay for the write
self._size += 1
assert self.bank >= 0, "price too low"
arr = AccountedArray()
for i in range(100000):
arr.append(i)
print(arr._size, arr.bank)The array stores no real data because only the cost bookkeeping matters. If you change CHARGE to 2, the assertion fails on the first resize that needs more credit than the bank holds. That shows 2 is too low for this growth rule.
Aggregate versus accounting
| Aspect | Aggregate method | Accounting method |
|---|---|---|
| What you compute | Total cost of n operations, divided by n | A price per operation type, plus a bank invariant |
| Operation types | Gives the same average to every operation | Can charge each type differently |
| Proof obligation | Bound the total | Show the bank is never negative |
| Best for | Single-operation sequences such as appends | Mixes of operations such as push, pop and multipop |
Common errors
- Spending credit twice. Each credit pays for one real unit of work. If you count an element's credit for two different copies, the proof is wrong.
- Letting the bank go negative. A negative bank means the price is too low. The bound then does not hold, even if the numbers look close.
- Forgetting the first half. The credit comes only from appends made since the last resize. The c/2 elements that existed before it carry no credit, which is why the price has to be 3 and not 2.
- Treating the charge as the real cost. A single append can still cost O(n) in real time. The charge is an accounting device and not a prediction for one call.
Exercise
A dynamic array doubles when full, and a resize copies every element. Suppose you charge only 2 units per append, with 1 unit paying for the write and 1 unit stored as credit. Starting from capacity 4 with an empty bank and 4 elements already present, at which point does the bank first fail to cover a resize, and why?
Show answer
The bank fails at the next resize. The 4 existing elements carry no credit, and 2 units per append leaves only 1 credit each. After the 4 existing elements, the array holds 4 elements, so appending a fifth triggers a resize that copies 4 elements. The bank holds 0 credits at that point, because no appends have added credit since the start. The 5th append adds 1 credit but must pay for 4 copies, so the bank goes negative. A price of 3 fixes this by placing 2 credits on each new element.