BITS Pilani
CS F364: Design and Analysis of Algorithms

Amortized Analysis

Lecture 20 |2026-06-12

Amortized Analysis

  • Worst-case analysis: Determine worst-case running time of a single operation as a function of input size n.
  • Amortized analysis: Determine worst-case running time of a sequence of n operations.
  • Example: Resizing a Dynamic Array

  • A dynamic array starts at size 1 and doubles when full.

  • Most insertions cost 1, but an insertion into a full array costs \Theta(k) to copy k elements into a new array of size 2k.

  • Yet n insertions cost O(n) total — copies are rare (\log_2 n times), so the average cost per operation is O(1).

Three Methods

Three techniques for amortized analysis:

  1. Aggregate method — sum total cost of all operations, divide by n.
  2. Accounting method — overcharge cheap ops, store credits to pay for expensive ones.
  3. Potential method — define a potential function; amortized cost = actual cost + change in potential.

All three give the same result; pick whichever is most intuitive.

Binary Counter

Goal: Increment a k-bit binary counter

(mod 2^k).

Cost model: Number of bits flipped per INCREMENT.

Worst-case: \le k flips per increment \Rightarrow O(nk) for n ops.

But not all increments flip k bits — can we do better?

Binary Counter: Aggregate Method

Aggregate Method: Sum up total cost of sequence, divide by n.

Starting from zero, in n INCREMENT operations:

  • Bit 0 flips every time: n flips.
  • Bit 1 flips every 2^{\text{nd}} time: \lfloor n/2 \rfloor flips.
  • Bit 2 flips every 4^{\text{th}} time: \lfloor n/4 \rfloor flips.
  • \dots
  • Bit j flips every 2^j time: \lfloor n/2^j \rfloor flips.

Total flips in n INCREMENTs = \displaystyle\sum_{j=0}^{k-1} \left\lfloor \frac{n}{2^j} \right\rfloor < 2n = O(n).

Binary Counter: Aggregate Method

Proof. \sum_{j=0}^{k-1} \left\lfloor \frac{n}{2^j} \right\rfloor < n \sum_{j=0}^{\infty} \frac{1}{2^j} = 2n

So the amortized cost per operation is O(1) — much better than the worst-case O(k) per operation!

Accounting Method (Banker’s Method)

  • Assign different amortized costs (charges) to each operation.
  • c_i = actual cost of i^{\text{th}} operation.
  • \hat{c}_i = amortized cost (what we charge).
  • When \hat{c}_i > c_i, we store credits in the data structure.
  • When \hat{c}_i < c_i, we consume credits.

Credit invariant: Total credits in the data structure at any point \ge 0.

\sum_{i=1}^{t} \hat{c}_i - \sum_{i=1}^{t} c_i \ge 0 \quad \forall t

Binary Counter: Accounting Method

One credit pays for one bit flip.

Accounting scheme:

  • Flipping a bit from 0 \to 1: charge 2 credits.
    • Use 1 credit to pay for this flip.
    • Store 1 credit in the bit for future.
  • Flipping a bit from 1 \to 0: pay with the credit stored in that bit.

Analysis: Each INCREMENT flips at most one bit from 0 \to 1. So amortized cost per INCREMENT \le 2.

Total actual cost of n operations \le total amortized cost \le 2n = O(n). ✓

Potential Method (Physicist’s Method)

  • Define a potential function \Phi(D_i) mapping data structure D_i to a real number.

Requirements:

  • \Phi(D_0) = 0.
  • \Phi(D_i) \ge 0 for all i.

Amortized cost: \hat{c}_i = c_i + \Phi(D_i) - \Phi(D_{i-1}) The amortized cost = actual cost + change in potential.

Total amortized cost: \sum_{i=1}^{n} \hat{c}_i = \sum_{i=1}^{n} c_i + \Phi(D_n) - \Phi(D_0)

Binary Counter: Potential Method

Potential: \Phi(D_i) = b_i, the number of 1’s in the counter after i^{\text{th}} increment.

  • \Phi(D_0) = 0 (zero counter), and \Phi(D_i) \ge 0 for all i. ✓

Analysis: Suppose i^{\text{th}} INCREMENT resets t_i bits (from 1 \to 0) and sets one bit 0 \to 1.

  • Actual cost: c_i \le t_i + 1
  • Potential change: \Phi(D_i) - \Phi(D_{i-1}) \le 1 - t_i (one new 1, t_i fewer 1’s)

Amortized cost: \hat{c}_i = c_i + \Phi(D_i) - \Phi(D_{i-1}) \le (t_i + 1) + (1 - t_i) = 2

Thus total amortized cost of n INCREMENTs is O(n). ✓

Summary

Method Key Idea Binary Counter Result
Aggregate \frac{1}{n}\sum c_i 2n total, O(1) per op
Accounting Overcharge cheap ops, save credits 2 per INCREMENT
Potential \hat{c}_i = c_i + \Delta\Phi \Phi = \#\text{ones}, \hat{c} \le 2

All three methods show: n INCREMENT operations on a binary counter cost O(n) total — O(1) amortized per operation.