1. Gyanastra
  2. Algorithms
  3. Amortized Analysis and Counting Operations in Loops
  4. Why This Chapter Exists
Free lesson, no account needed 4 min read

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 O(1)O(1) most of the time and O(n)O(n) once in a while. If you ask "what is the worst-case cost of one operation?" you get O(n)O(n); if you ask "what is the cost of a sequence of n operations, divided by n?" you get O(1)O(1). That second, honest answer is the amortized cost.

This chapter does two things:

  1. Teaches the three standard methods of amortized analysis (aggregate, accounting, potential) and how they differ from average-case analysis.
  2. 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.

FeatureAverage-case analysisAmortized analysis
Input modelAssumes a probability distribution over inputsNo probability at all; worst case over any sequence
Question answeredExpected cost of one operationWorst-case total cost of a sequence, per operation
Depends on randomness?Yes (must state the distribution)No — it is a deterministic guarantee
Typical exampleExpected time of Quick SortCost 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 O(1)O(1), then any sequence of m operations costs O(m)O(m) in the worst case — no probability assumed.

Notation: if a sequence of mm operations costs T(m)T(m) total, the amortized cost per operation is

amortized cost=T(m)m.\text{amortized cost} = \frac{T(m)}{m}.


Pages 1–2 of 2