3.1.5 · D4Complexity Analysis

Exercises — Amortized analysis — aggregate, accounting, potential methods

2,511 words11 min readBack to topic

Every symbol used below, defined once so line one is readable:


Level 1 — Recognition

Recall Solution L1.1

This is the Aggregate method. Why: the defining move is computing directly as one lump and reporting identically for every operation. Weakness: because it hands every operation the same amortized cost, it is weak when different operation types have genuinely different amortized behaviour (e.g. a structure with both cheap find and expensive delete) — it can't distinguish them.

Recall Solution L1.2

They broke requirement 1: (the empty start must have zero stored energy). A negative starting potential means the amortized total could undershoot the real total, so the bound would be dishonest. (It also violates .)


Level 2 — Application

Recall Solution L2.1

(a) Copy work. Copies happen when size crosses The last power of two is . Copy costs sum to So total copy work is exactly . (b) Total work . Amortized . Why the "3": even in the loosest bound , so always.

Recall Solution L2.2

Direct count. Bit (least significant) flips every increment: times. Bit flips every 2 increments: . Bit : every 4: . Bit : every 8: . Bit : every 16: . Check the charge: . ✓ The bank stays solvent. Why it works: every 0→1 flip prepays its own future 1→0 flip. In general total flips over increments — the geometric tail again.


Level 3 — Analysis

Recall Solution L3.1

Actual cost (write one element). Potential change: Why: the push does unit of real work but deposits units of stored energy for the copy that is coming. Amortized .

Recall Solution L3.2

Actual cost (copy + insert ).

  • Before (full): .
  • After (doubled): . What cancels: the giant real cost is annihilated by the potential dropping by . The stored energy pays the copy, leaving . See the picture below — the red bar (potential) collapses exactly as the copy fires.
Figure — Amortized analysis — aggregate, accounting, potential methods
Recall Solution L3.3

If then , so — the potential goes negative, violating requirement . A negative potential means we might be "borrowing" energy we never stored, and the amortized total could fall below the real total → dishonest bound. The real fix: structures that also shrink use a two-piece potential, e.g. so that potential rises again as the table empties, ready to pay for a shrink copy. Below , each pop deposits energy for the eventual halving. The figure shows both wings.

Figure — Amortized analysis — aggregate, accounting, potential methods

Level 4 — Synthesis

Recall Solution L4.1

Requirements: (empty) ✓ and (a count) ✓. Push (): , , so . Multipop popping elements: , but (stack shrinks by ). So Every operation has amortized cost . Why it works: you can only pop what you pushed; each element's pop was prepaid by the stored at its push. A multipop of elements looks expensive but its real cost is exactly cancelled by the potential it releases.

Figure — Amortized analysis — aggregate, accounting, potential methods
Recall Solution L4.2

Choose . Check: ✓, ✓. Enqueue (push to In): , , so . Dequeue with Out non-empty: (one pop), , . Dequeue triggering a flip of elements: . In empties, so . Then Every operation is amortized. Why the factor 2: each enqueued element will later be moved twice (popped from In, pushed to Out), so we bank units at enqueue time to prepay exactly that.


Level 5 — Mastery

Recall Solution L5.1

Design. Let . Check: ✓, ✓. SET on an unset slot: , . SET on an already-set slot (idempotent, no state change): , . RESET clearing set slots: , . Both . Each set slot stored at its SET, released to pay its own clear. Degenerate RESET on empty structure: , so , , . No underflow, potential never negative — the bound survives the empty case cleanly.

Recall Solution L5.2

All three methods bound the same quantity: , and each reports a valid upper bound on .

  • Aggregate computes itself and sets for all : total .
  • Accounting requires the bank , i.e. .
  • Potential gives since . All three satisfy the same inequality . The real total work is a physical fact — it doesn't depend on our bookkeeping. What differs is only how each method distributes (plus leftover credit/potential) across the operations. Given the tightest choices (, zero leftover credit), they collapse to the identical bound . Bookkeeping cannot change reality.

Recall Feynman recap

The three methods are three ways to pay a bill you already owe. The bill () is fixed by physics. Aggregate = pay the lump sum and split evenly. Accounting = put coins in a jar per element. Potential = track the "stored energy" of the whole structure. Every jar-of-coins or energy trick must, when you cash out, cover the same real bill — so all three land on the same per operation.

Connections

  • Dynamic Arrays / Table Doubling — L2, L3 live here.
  • Disjoint Set Union (Union-Find) — potential functions power its near-constant amortized bound.
  • Splay Trees, Fibonacci Heaps — advanced potential-method structures for later.
  • Geometric Series — the bound behind every "geometrically rare" argument.
  • Big-O, Big-Omega, Big-Theta — amortized results are still written in asymptotic notation.