1. Gyanastra
  2. Algorithms
  3. Amortized Analysis and Counting Operations in Loops
  4. The Three Methods
Free lesson, no account needed 6 min read

Algorithms · Amortized Analysis and Counting Operations in Loops

The Three Methods

3.1 Aggregate Method

Idea. Bound the total cost T(m)T(m) of a sequence of mm operations directly, then divide by mm. Every operation in the sequence is assigned the same amortized cost T(m)/mT(m)/m (even if some individual operations are much cheaper or much dearer).

Steps.

  1. Identify the expensive operation and how often it can occur in mm operations.
  2. Sum the total work over the whole sequence.
  3. Divide by mm.

This is the method GATE almost always expects, because it is the easiest to justify in a few lines.

3.2 Accounting Method

Idea. Assign each operation an amortized charge c^i\hat{c}_i (which may differ from its true cost cic_i). Cheap operations are overcharged; the surplus is stored as credit on specific objects in the data structure. Expensive operations are undercharged and pay for themselves by spending stored credit.

Invariant that must hold at all times:

∑i=1kc^i  ≥  ∑i=1kcifor every k.\sum_{i=1}^{k} \hat{c}_i \;\ge\; \sum_{i=1}^{k} c_i \quad \text{for every } k.

Equivalently: credit never goes negative. If you can exhibit charges satisfying this, then ∑c^i\sum \hat{c}_i bounds the true total cost, and the amortized cost per operation is c^\hat{c} (when charges are uniform).

The credit is a fiction — it is an accounting device, not real memory. But it must be attached to identifiable objects so you can argue it is never negative.

3.3 Potential Method

Idea. Like the accounting method, but the "credit" is stored as a potential function Φ\Phi on the whole data structure state, not on individual objects. Let DiD_{i} be the structure after operation ii, and define the amortized cost of operation ii as

c^i=ci+Φ(Di)−Φ(Di−1).\hat{c}_i = c_i + \Phi(D_i) - \Phi(D_{i-1}).

Here cic_i is the true cost, Φ(Di)−Φ(Di−1)\Phi(D_i)-\Phi(D_{i-1}) is the change in potential (the credit earned or spent). Summing over i=1..mi=1..m telescopes:

∑i=1mc^i=∑i=1mci+Φ(Dm)−Φ(D0).\sum_{i=1}^{m}\hat{c}_i = \sum_{i=1}^{m} c_i + \Phi(D_m) - \Phi(D_0).

So if Φ(Dm)≥Φ(D0)\Phi(D_m)\ge \Phi(D_0) (usually we choose Φ(D0)=0\Phi(D_0)=0 and Φ≥0\Phi\ge 0 always), then

∑i=1mci  ≤  ∑i=1mc^i.\sum_{i=1}^{m} c_i \;\le\; \sum_{i=1}^{m}\hat{c}_i.

The potential method is the most powerful because you can design Φ\Phi to make the algebra clean, and you never have to argue about individual credits.

3.4 Which Method to Use

MethodBest whenGATE usage
AggregateThe expensive operation's frequency is easy to countMost common; use for counters, stacks, dynamic arrays
AccountingYou can attach credit to objects naturallyGood for stacks, dynamic arrays
PotentialYou need a clean algebraic proof, or the structure is complexRarely required in GATE, but appears in theory questions

All three give the same amortized bound when applied correctly. They are three proofs of one fact.


Worked Example 1: Stack with Multipop

Structure. A stack with three operations:

  • PUSH(S, x): cost 1.
  • POP(S): cost 1.
  • MULTIPOP(S, k): pops min⁡(k,∣S∣)\min(k, |S|) elements; cost = number actually popped.

Question. A sequence of mm operations starting from an empty stack — what is the total cost?

Aggregate argument. A MULTIPOP can pop many elements, but each element can be popped at most once for every time it was pushed. So across the whole sequence:

total pops≤total pushes≤m.\text{total pops} \le \text{total pushes} \le m.

Total cost = (cost of pushes) + (cost of pops) ≤m+m=2m\le m + m = 2m. Therefore amortized cost per operation =2m/m=O(1)= 2m/m = O(1).

Accounting argument. Charge PUSH 2 units: 1 pays for the push itself, 1 is stored as credit on the pushed element. Charge POP and MULTIPOP 0 (they spend the credit stored on each element they pop). Credit is never negative because an element must have been pushed (and thus have 1 unit of credit) before it can be popped. Total charge =2m= 2m, so amortized cost per operation is 2 = O(1)O(1).

Potential argument. Let Φ\Phi = number of elements on the stack. Then:

  • PUSH: c=1c=1, ΔΦ=+1\Delta\Phi=+1, so c^=1+1=2\hat c = 1+1 = 2.
  • POP: c=1c=1, ΔΦ=−1\Delta\Phi=-1, so c^=1−1=0\hat c = 1-1 = 0.
  • MULTIPOP(k) popping t=min⁡(k,∣S∣)t=\min(k,|S|) elements: c=tc=t, ΔΦ=−t\Delta\Phi=-t, so c^=t−t=0\hat c = t - t = 0.

Every operation has c^≤2\hat c \le 2, so total ≤2m\le 2m. Amortized O(1)O(1). ✔


Worked Example 2: Binary Counter

Structure. A kk-bit binary counter, initially all zeros. INCREMENT flips a suffix of bits: the trailing 1s become 0, and the first 0 becomes 1.

Question. Cost of nn increments, where cost = number of bit flips.

Aggregate argument. Count how often each bit flips in nn increments:

  • Bit 0 (least significant) flips on every increment: nn times.
  • Bit 1 flips every 2 increments: ⌊n/2⌋\lfloor n/2 \rfloor times.
  • Bit 2 flips every 4 increments: ⌊n/4⌋\lfloor n/4 \rfloor times.
  • Bit jj flips ⌊n/2j⌋\lfloor n/2^j \rfloor times.

Total flips:

T(n)=∑j≥0⌊n2j⌋  ≤  ∑j≥0n2j=n(1+12+14+⋯ )=2n.T(n) = \sum_{j\ge 0}\left\lfloor \frac{n}{2^j}\right\rfloor \;\le\; \sum_{j\ge 0}\frac{n}{2^j} = n\left(1+\tfrac12+\tfrac14+\cdots\right) = 2n.

So T(n)≤2nT(n) \le 2n, and amortized cost per increment =2n/n=2=O(1)= 2n/n = 2 = O(1). ✔

Accounting argument. Charge 2 per increment: 1 pays for setting a 0-bit to 1, 1 is stored as credit on that bit. When a bit is flipped from 1 to 0, it spends its stored credit. Each bit carries at most 1 unit of credit at any time, and a bit must have credit before it can be reset. Total charge =2n= 2n. ✔

Potential argument. Let Φ\Phi = number of 1-bits currently set. In an increment that flips tt trailing 1s to 0 and one 0 to 1: c=t+1c = t+1, ΔΦ=1−t\Delta\Phi = 1 - t, so c^=(t+1)+(1−t)=2\hat c = (t+1)+(1-t) = 2. Amortized 2. ✔


Pages 1–2 of 2