Skip to the workspace
Pebellslearn by building
Analysis II: Recurrences, Amortisation & Randomness  ›  Lesson

Amortised Analysis & the Cost of Growing

Amortised Analysis 28 minBound the total cost of a sequence of operations, so rare expensive steps are paid for by the cheap ones

dsa / Amortised Analysis & the Cost of Growing

Explore each scene at your own pace.

01 · The spike, measured

The spike, measured

Amortised Analysis & the Cost of Growing

Definition
Amortised analysis bounds the average cost per operation over a whole sequence of operations, so an occasional expensive operation is allowed as long as the total stays small, even though a single step may be costly.
In plain words
It is like paying a small fee on every bus ride so the rare big repair bill is already covered. Most steps are cheap, the odd one is expensive, and spread over the whole sequence each step still costs about the same.

In short: most appends into a dynamic array are cheap, but one in a while the block is full and the append has to copy everything into a bigger block first. Amortised analysis proves that the expensive copies are rare enough that the average cost over any sequence of n appends is still O(1) — by adding up the whole sequence, by charging a fixed toll that banks the change, or by watching a potential function rise and fall. The same move — "bound the whole sequence, not the worst step" — also explains why your BFS's inner loop isn't the O(V²) it looks like.

Eight appends charged 3 tokens each: small bars for cheap appends, one tall bar for the resize spike, and a bank balance row underneath that dips but never goes below zero

In the Big-O lesson you were told that a list's rare full copy "pays for itself". Here you prove it three ways, then use the same argument where it is less obvious: your breadth-first search's neighbour loop, and a stack that shrinks as well as grows.

One operation is occasionally very expensive. Is a sequence of n operations still cheap? Amortised analysis bounds the total cost of the whole sequence and divides by n. It is not a guess about typical inputs; it holds for every sequence.

The spike, measured

Here is a dynamic array of your own, the structure behind Python's list. It keeps a capacity and a size. An append into a spare slot costs 1 (one write). An append into a full block first copies all size items into a block twice as big, then writes: it costs size + 1.

Run it, then edit grow. Change it to return self.capacity + 1 and watch the copies pile up, or to self.capacity * 3 and watch the empty slots.

your code, drawn · array

Your dynamic array. Cheap appends fill spare slots, and each copy buys as many spare slots as it cost, so the bank of 3-per-append never runs dry.

Sixteen appends cost 16 writes plus 1 + 2 + 4 + 8 = 15 copies: 31, under 2 per append. The spikes are real but rare, each twice as far from the last.

Scene 1 of 11
Project context and further resources
You're building a piece ofTunebox — the cost lab
This piece — append_costs(): Records the real cost of every append, spikes included.
Scenario Tunebox's library import appends a million tracks, and a monitoring graph shows rare latency spikes. The append ledger shows every spike and proves the average stays flat.
append_costs.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
append_costs(n) → listThe real cost of each of n appends, spikes included.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.