3.1.5Complexity Analysis

Amortized analysis — aggregate, accounting, potential methods

2,000 words9 min readdifficulty · medium6 backlinks

There are three methods to find c^\hat{c}. All give the same total bound; they differ in how you account.


1. Aggregate method — "just add it all up"


2. Accounting (banker's) method — "prepay and save credit"


3. Potential method — "energy stored in the structure"

Figure — Amortized analysis — aggregate, accounting, potential methods

Recall Feynman: explain to a 12-year-old

Imagine a piggy bank. Every time you do a small chore you put in 3 coins but the chore only "costs" 1 coin of effort. The extra 2 coins pile up. Once in a while a HUGE chore appears (cleaning the whole room). Instead of crying, you smash the piggy bank — and surprise, you saved exactly enough coins to pay for it! So even though one chore looked giant, on average every chore only ever cost you 3 coins. That averaged-but-guaranteed price is the amortized cost, and the piggy bank is the potential function.


Flashcards

Amortized cost is which kind of average — over inputs or guaranteed over a sequence?
Guaranteed worst-case over any sequence of operations; there is NO probability/randomness involved.
Aggregate method in one line
Compute total cost T(m)T(m) of mm ops directly, then amortized cost =T(m)/m= T(m)/m (same for every op).
Why is a dynamic array push O(1)O(1) amortized despite O(n)O(n) copies?
Copies happen geometrically rarely; total copy work =1+2++2k<2m=1+2+\dots+2^k<2m, so total T(m)<3mT(m)<3m.
Accounting method validity condition
Total stored credit (c^ici)\sum(\hat c_i-c_i) must stay 0\ge 0 at all times (bank never goes negative).
Master equation of the potential method
c^i=ci+Φ(Di)Φ(Di1)\hat c_i = c_i + \Phi(D_i)-\Phi(D_{i-1}) (actual cost plus change in potential).
Why does the potential method give a valid upper bound?
Telescoping: c^i=ci+Φ(Dm)Φ(D0)\sum\hat c_i=\sum c_i+\Phi(D_m)-\Phi(D_0); if Φ(Dm)Φ(D0)\Phi(D_m)\ge\Phi(D_0) then c^ici\sum\hat c_i\ge\sum c_i.
Two requirements for a good potential function
Φ(D0)=0\Phi(D_0)=0 and Φ(Di)0\Phi(D_i)\ge0 for all ii.
Potential function used for table doubling
Φ=2ns\Phi=2n-s (n = #elements, s = capacity), giving c^=3\hat c=3 per push.
Amortized cost of incrementing a binary counter
O(1)O(1) — charge $2: \backslash1 to set a bit, \1 stored on it to pay its future 101\to0 flip.
Difference: amortized vs average-case complexity
Amortized = deterministic guarantee over a sequence; average-case = expectation over a random distribution of inputs.

Connections

  • Dynamic Arrays / Table Doubling — the canonical amortized example.
  • Big-O, Big-Omega, Big-Theta — amortized bounds are still expressed in asymptotic notation.
  • Disjoint Set Union (Union-Find) — path compression + union by rank analyzed via potential.
  • Splay TreesO(logn)O(\log n) amortized via a potential function.
  • Fibonacci Heaps — decrease-key O(1)O(1) amortized using potential of marked nodes.
  • Geometric Series — the summation engine behind the aggregate method.

Concept Map

guarantees

differs from

reports

must satisfy

3 methods

3 methods

3 methods

computes

stores surplus as

example

example

copies form

yields

Amortized analysis

Worst-case avg per op

Average-case with randomness

Amortized cost c-hat

Sum c-hat >= Sum actual cost

Aggregate

Accounting banker

Potential

Total T of m over m

Credit >= 0 always

Dynamic array doubling

Geometric series < 2m

O(1) per push

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, amortized analysis ka basic idea simple hai: kuch operations kabhi-kabhi bahut mehenge hote hain, par mostly sasta. Jaise dynamic array me push — normally O(1)O(1), lekin jab array full ho jaye to wo double hoke saare elements copy karta hai, jo O(n)O(n) lagta hai. Agar hum sirf us ek mehenge case se dar ke "push is O(n)O(n)" bol de, to ye galat hai. Kyunki wo mehenga case sirf kabhi-kabhi aata hai. Amortized analysis poori sequence pe average nikaalta hai — aur ye guarantee hota hai, koi probability nahi (yahi average-case se difference hai).

Teen methods hain, yaad rakho A-A-P. Aggregate: total cost T(m)T(m) nikaalo, divide by mm. Array me copies 1+2+4+<2m1+2+4+\dots < 2m, total <3m< 3m, to per push 3=O(1)\le 3 = O(1). Accounting (banker's): har push pe $3 \text{ charge karo jabki real cost }1 hai. Extra \2 ko credit ki tarah elements pe store karo — jab copy ka time aaye, wo stored credit se pay ho jaata hai, user se extra nahi maangte.

Potential method sabse powerful aur reusable hai. Ek function Φ\Phi define karo jo data structure me "stored energy" represent kare. Formula: c^=c+ΔΦ\hat c = c + \Delta\Phi — actual cost plus potential ka change. Cheap operations Φ\Phi ko badhate hain (saving), aur mehenga operation Φ\Phi ko gira deta hai (pay out), to c^\hat c chhota reh jaata hai. Array ke liye Φ=2ns\Phi = 2n - s lo — har case me c^=3\hat c = 3 aata hai. Teeno methods ka answer same: O(1)O(1).

Ye matter karta hai kyunki real interviews aur real systems me (DSU, splay trees, Fibonacci heaps) tum worst-single-case se bound loge to bahut loose answer milega. Amortized se tight aur honest bound milta hai — "average per operation, guaranteed". Piggy bank wala example yaad rakho: chhote chores me coins jama karo, bada chore aaye to gullak phod ke pay kar do.

Go deeper — visual, from zero

Test yourself — Complexity Analysis

Connections