Algorithms · Amortized Analysis and Counting Operations in Loops
Why This Chapter Exists
In the chapter Algorithm Analysis and Asymptotic Notation you learned to bound the cost of a single run of an algorithm. But many data structures have operations that are usually cheap and occasionally very expensive. A single push into a dynamic array is most of the time and once in a while. If you ask "what is the worst-case cost of one operation?" you get ; if you ask "what is the cost of a sequence of n operations, divided by n?" you get . That second, honest answer is the amortized cost.
This chapter does two things:
- Teaches the three standard methods of amortized analysis (aggregate, accounting, potential) and how they differ from average-case analysis.
- Drills the loop-counting and recursion-counting questions that GATE, UGC NET, ISRO and BARC love, deriving every answer from an explicit sum.
Both halves are really the same skill: write down the total work as a sum, then evaluate the sum. Amortized analysis is just "sum the work over a sequence of operations and divide."
Amortized vs Average-Case Analysis
These two are constantly confused. They are not the same.
| Feature | Average-case analysis | Amortized analysis |
|---|---|---|
| Input model | Assumes a probability distribution over inputs | No probability at all; worst case over any sequence |
| Question answered | Expected cost of one operation | Worst-case total cost of a sequence, per operation |
| Depends on randomness? | Yes (must state the distribution) | No — it is a deterministic guarantee |
| Typical example | Expected time of Quick Sort | Cost per operation of a stack with multipop |
Key sentence to memorise: Amortized cost is a worst-case guarantee about a sequence, not an average over random inputs. If an operation has amortized cost , then any sequence of m operations costs in the worst case — no probability assumed.
Notation: if a sequence of operations costs total, the amortized cost per operation is