Invariants and Amortised Analysis
Keep a structure's rules true and count the cost of a sequence of operations.
invariant, data structure, amortised analysis, resizing arrayRules that survive change
Think of a row of labelled boxes: every label must still point to the correct box after a box is moved. A data structure also needs rules that remain true after each completed operation. Such a rule is an invariant.
For a max-heap, every parent is at least as large as its children. For a binary search tree, values in the left subtree are smaller and values in the right subtree are larger under that example's distinct-key rule. A doubly linked list needs consistent previous and next links and correct head and tail references.
An operation can temporarily rearrange data while it works, but it must leave the structure valid before the next operation uses it. Check the starting state, how the update restores the rule, and what the rule allows a later operation to conclude. An invariant by itself does not prove an operation finishes or returns the intended value.
Why count a sequence?
An occasional expensive operation can be safe to use often if many cheaper operations separate the expensive ones. Amortised analysis bounds the total work of a sequence and spreads that cost across its operations. It is not the same as an average over randomly chosen inputs.
Consider the resizing-array idea. In this model the array starts empty with capacity one and doubles its capacity only when full. Charge one unit for writing the new item and one per copied old item. This is a teaching model, not Python's exact allocation policy.
Follow five appends
| Append | Capacity after append | Old items copied | Total units this append |
|---|---|---|---|
| 1 | 1 | 0 | 1 |
| 2 | 2 | 1 | 2 |
| 3 | 4 | 2 | 3 |
| 4 | 4 | 0 | 1 |
| 5 | 8 | 4 | 5 |
Five writes cost 5 units, and copies cost 1 + 2 + 4 = 7, for 12 units
altogether. Append 5 costs 5 units by itself; not every append is constant
worst-case work.
See the reasoning
Follow the example one step at a time
Count writes and copies over five appends in the doubling-array model. This is a prepared teaching demonstration, not live code execution.
Start empty
length: 0 capacity: 1 total units: 0
A new write costs one unit; copying an old item also costs one unit.
Python illustration
def append_costs(count):
if count < 0:
raise ValueError("count must be non-negative")
capacity = 1
length = 0
costs = []
for _ in range(count):
copies = 0
if length == capacity:
copies = length
capacity *= 2
length += 1
costs.append(1 + copies)
return costs
costs = append_costs(5)
print(costs)
print("total:", sum(costs))
print("empty:", append_costs(0))
Prepared output — not live execution:
[1, 2, 3, 1, 5]
total: 12
empty: []
This simulation expects an integer count. It counts the model's writes and copies; it does not allocate or copy the modelled array. It returns the costs for inspection, so the simulation itself stores O(n) output.
The aggregate bound
Over n appends, there are exactly n new-item writes. Copies happen at
capacities 1, 2, 4, and so on. For n > 1, their sum is less than 2n,
because this doubling series is less than twice its largest term and its
largest term is less than n. No copies are needed for zero or one append.
Total modelled work is therefore less than 3n for n > 0, or O(n) overall,
for any sequence of n appends from this empty state.
That gives O(1) amortised work per append, even though a single resize can cost O(n). The argument assumes doubling, this cost model, and append-only operations; a different growth or shrink policy requires its own analysis. The correctness bridge shows how a loop invariant supports a separate correctness argument.
Reflect on the concept
Four fixed questions to reinforce the reasoning. Answers are not saved or scored.
