3.1.5 · D3Complexity Analysis

Worked examples — Amortized analysis — aggregate, accounting, potential methods

4,439 words20 min readBack to topic

The scenario matrix

Every amortized problem is really a combination of a structure, a cost pattern, and an edge condition. The table lists every cell we must cover; the "Example" column tells you which worked example hits it.

Here = the total number of operations in the sequence we analyse (pushes, increments, log lines — whatever the structure's basic operation is). It is the single most important symbol on this page: amortized cost is cost averaged over these operations, so appears in every bound.

Cell What makes it tricky Example
A. Cheap-then-rare-expensive usual , occasional Ex 1 (aggregate), Ex 2 (potential)
B. Bit-cascade cost one op flips many bits Ex 3 (binary counter)
C. Zero / degenerate input , first op, empty structure Ex 4
D. Both-ends expensive grow and shrink both cost Ex 5 (shrinking array — the trap)
E. Wrong potential picking that breaks a rule Ex 6 (steel-man)
F. Non-uniform op mix different ops, aggregate too weak Ex 7 (stack with multipop)
G. Limiting behaviour what happens as Ex 8
H. Real-world word problem translate a story into Ex 9 (server log flush)
I. Exam twist doubling and halving thresholds Ex 10

Before any example, we re-earn every symbol used on this page — do not assume the parent.


A + limiting: the aggregate baseline

The figure below draws the first pushes — a smaller, readable slice of the same sequence (the pattern just repeats with taller, rarer spikes as you go further right, so 20 is enough to see the shape). Read it left to right: mint bars are the height-1 cheap writes that happen on every push, and the coral spikes are the rare copies at sizes . Notice the coral spikes get taller but further apart — that spreading-out is exactly why the dashed lavender line at height 3 (the amortized charge) sits comfortably above the average, at just as at .

Figure — Amortized analysis — aggregate, accounting, potential methods
Recall Why does the geometric sum beat the naive fear?

Naive fear counts the copy every push. ::: But that copy happens only once per doubling; the gaps between doublings grow, so averaged cost is constant.


A via potential — same answer, cleaner algebra

The next figure plots the potential as the array fills. Follow the lavender curve: on each ordinary push it climbs by (energy saved into the shaded well below it), building up a tall reserve. Then, at each doubling, the coral arrow marks the moment the curve plunges — that vertical drop is the stored energy being spent to pay the copy, leaving only behind. The saw-tooth shape is the piggy-bank filling and smashing.

Figure — Amortized analysis — aggregate, accounting, potential methods

B — the bit cascade


C — zero and degenerate inputs


D — the shrinking trap (both ends expensive)


E — a broken potential (steel-man)


F — non-uniform operations (aggregate shines here)


G — limiting behaviour


H — real-world word problem


I — exam twist: combined thresholds



Connections

  • Dynamic Arrays / Table Doubling — Ex 1, 2, 4, 5, 10 all live here.
  • Geometric Series — the engine behind every example.
  • Big-O, Big-Omega, Big-Theta — every ends in or asymptotics.
  • Disjoint Set Union (Union-Find), Splay Trees, Fibonacci Heaps — advanced structures where the potential method of Ex 6/10 is the only practical tool.