Algorithms · Amortized Analysis and Counting Operations in Loops
The Three Methods
3.1 Aggregate Method
Idea. Bound the total cost of a sequence of operations directly, then divide by . Every operation in the sequence is assigned the same amortized cost (even if some individual operations are much cheaper or much dearer).
Steps.
- Identify the expensive operation and how often it can occur in operations.
- Sum the total work over the whole sequence.
- Divide by .
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 (which may differ from its true cost ). 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:
Equivalently: credit never goes negative. If you can exhibit charges satisfying this, then bounds the true total cost, and the amortized cost per operation is (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 on the whole data structure state, not on individual objects. Let be the structure after operation , and define the amortized cost of operation as
Here is the true cost, is the change in potential (the credit earned or spent). Summing over telescopes:
So if (usually we choose and always), then
The potential method is the most powerful because you can design to make the algebra clean, and you never have to argue about individual credits.
3.4 Which Method to Use
| Method | Best when | GATE usage |
|---|---|---|
| Aggregate | The expensive operation's frequency is easy to count | Most common; use for counters, stacks, dynamic arrays |
| Accounting | You can attach credit to objects naturally | Good for stacks, dynamic arrays |
| Potential | You need a clean algebraic proof, or the structure is complex | Rarely 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 elements; cost = number actually popped.
Question. A sequence of 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 cost = (cost of pushes) + (cost of pops) . Therefore amortized cost per operation .
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 , so amortized cost per operation is 2 = .
Potential argument. Let = number of elements on the stack. Then:
PUSH: , , so .POP: , , so .MULTIPOP(k)popping elements: , , so .
Every operation has , so total . Amortized . ✔
Worked Example 2: Binary Counter
Structure. A -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 increments, where cost = number of bit flips.
Aggregate argument. Count how often each bit flips in increments:
- Bit 0 (least significant) flips on every increment: times.
- Bit 1 flips every 2 increments: times.
- Bit 2 flips every 4 increments: times.
- Bit flips times.
Total flips:
So , and amortized cost per increment . ✔
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 . ✔
Potential argument. Let = number of 1-bits currently set. In an increment that flips trailing 1s to 0 and one 0 to 1: , , so . Amortized 2. ✔