Foundation · Module 1 · Lesson 4

Amortized analysis

Average cost across a sequence of operations.

15–25 min readLesson 4 of 123

Lesson 4 of 123

1d streak

Detailed theory

Amortized analysis proves a bound on the average cost per operation over every valid sequence of operations. It allows occasional expensive operations without assuming random inputs.

By the end, you can

  • Distinguish amortized cost from average-case and worst-case cost.
  • Derive dynamic-array append with aggregate analysis.
  • Recognize aggregate, accounting, and potential-method proofs.

Vocabulary

Definitions with concrete meaning

Amortized cost

Total cost of a worst-case operation sequence divided across its operations.

Example: Dynamic-array append is Θ(1) amortized even though a resizing append is Θ(n).

Aggregate method

Bound the total cost T(n) of n operations, then divide by n.

Example: n writes plus fewer than n copied elements gives T(n) < 2n.

Accounting method

Charge cheap operations extra credits and spend saved credits on later expensive operations.

Example: Charge a constant amount per append; unused credit pays for future copying.

Potential method

Store prepaid work in a non-negative potential function Φ(state). Amortized cost is actual cost + ΔΦ.

Example: Unused dynamic-array capacity can represent stored potential.

Rules to apply

  1. 1Amortized does not mean average-case: it needs no probability distribution.
  2. 2An individual operation can still hit its Θ(n) worst case.
  3. 3The guarantee applies to the total cost of every valid sequence covered by the proof.
  4. 4Geometric growth is essential: doubling makes 1 + 2 + 4 + … less than the final scale.
  5. 5Growing capacity by one each time would copy Θ(n²) elements across n appends.

Worked example 1

Eight appends to a doubling array

Start with capacity 1. Writing a value costs 1; on a full array, copying each old value also costs 1.

append(x):
  if size == capacity:
    allocate array of capacity * 2
    copy size existing values
  data[size] = x
  size++
  1. 1

    List actual costs

    Append costs are 1, 2, 3, 1, 5, 1, 1, 1 as capacity grows 1 → 2 → 4 → 8.

  2. 2

    Add writes

    Every append writes once, so eight appends contribute 8.

  3. 3

    Add copies

    Resizes copy 1 + 2 + 4 = 7 existing values.

  4. 4

    Average the sequence

    Total cost is 15, so amortized cost is 15 / 8 < 2 per append.

For n appends, copies form a geometric series smaller than n; total cost is Θ(n), so each append is Θ(1) amortized.

Worked example 2

Why “grow by one” fails

Suppose a full array increases capacity from k to k + 1.

  1. 1

    Count resize copies

    Successive expansions copy 1 + 2 + 3 + … + (n−1) values.

  2. 2

    Use the arithmetic sum

    The total is n(n−1)/2 = Θ(n²).

  3. 3

    Divide by n appends

    Θ(n²) / n = Θ(n) amortized per append.

The growth policy—not merely using an array—creates the constant amortized append guarantee.

Comparison table

Do not mix these guarantees

Each column answers a different performance question.

AnalysisWhat is averaged?Probability needed?Append example
Worst-case per operationNothingNoΘ(n) for a resizing append
Average-caseRandom inputs/outcomesYesDepends on a stated distribution
AmortizedA sequence of operationsNoΘ(1) per append over any sequence
ExpectedAlgorithmic randomnessYesDepends on randomized implementation

Check your understanding

1Does Θ(1) amortized mean every append takes constant time?

No. A resizing append takes Θ(n); the total across n appends is Θ(n).

2What probability assumption proves dynamic-array append?

None. The aggregate bound holds for every sequence of appends.

3Why does doubling work?

The copied sizes form a geometric series 1 + 2 + 4 + … whose sum is less than twice its largest term.

Cost Model

Complexity & Trade-offs

Append without resize
Θ(1) actual
Θ(1) new
One value is written into existing capacity.
Append with resize
Θ(n) actual
Θ(n) temporary
The n existing values are copied into a larger allocation.
n appends with doubling
Θ(n) total
Θ(n) retained
Copies sum to 1 + 2 + 4 + … < n.
Append with doubling
Θ(1) amortized
Θ(n) retained
Linear total cost divided by n operations is constant.

When to use it

  • Dynamic arrays, hash-table resizing, and buffered data structures.
  • Stack sequences with multipop and queue implementations using two stacks.
  • Any operation sequence with rare rebuilding or cleanup work.

Common mistakes

  • Calling amortized analysis “average case.”
  • Claiming every operation is constant because the amortized cost is constant.
  • Ignoring the resizing policy or failing to bound the complete operation sequence.

Practice prompts

  • Compute exact copy counts for 16 appends to a doubling array.
  • Compare doubling capacity with growing capacity by one.
  • Prove that n stack operations including multipop take O(n) total time.

Mastery check

Can you derive it instead of memorizing it?

State the invariant before looking at code.
Explain why every transition is safe.
Construct a worst-case input.
Name one alternative and its trade-off.