Skip to content
Home Β» Amortized Analysis

Amortized Analysis


πŸ“˜ Amortized Analysis (Introduction)

Amortized Analysis is a method used to analyze the average performance of each operation in a sequence of operations in the worst case.

πŸ‘‰ It measures the long-term average cost of operations, even if some individual operations are expensive.

Why Not Average-Case?

Average-case analysis requires probability distribution of inputs.
Amortized analysis does not rely on probability.

Instead, it assumes:

  • A sequence of operations
  • Some operations may be expensive
  • Over time, the expensive operations occur rarely
  • So, amortized cost becomes small

πŸ“Œ When is Amortized Analysis Used?

Common in data structures where:

  • Most operations are cheap
  • Few operations are expensive but rare

Examples:

  1. Dynamic Array (ArrayList / Vector) resizing
  2. Stack operations with multipop
  3. Union-Find (Disjoint Set Union) with path compression
  4. Splay trees
  5. Incrementing a binary counter

πŸ“˜ Why Amortized Analysis is Important?

  • More realistic than worst-case
  • Avoids needing probability assumptions (unlike probabilistic analysis)
  • Shows how some DS achieve β€œaverage O(1)” operations
  • Important for competitive programming and interviews

πŸ“˜ Three Methods of Amortized Analysis


1. Aggregate Method (Simplest)

Calculate the total cost of n operations and divide by n.

Example: Dynamic Array Insertion

When an array is full, it doubles its size.

Some operations:

  • Most insertions: O(1)
  • Expansion (copying all items): O(n)

If we perform n insertions:

  • Total cost = n (regular inserts) + n (for resizing steps)
    = 2n
    Amortized cost = (2n)/n = O(1) per operation

βœ” Even though resizing is expensive, it happens rarely
βœ” So average over time = constant


2. Accounting Method (Banker’s Method)

Assign a charged cost to each operation:

  • Some cost used now
  • Extra “saved” for future expensive operations

Example: Dynamic Array

Actual costs:

  • Normal insert = 1 unit
  • Resize insert = n units (copy all elements)

We assign:

  • Charge = 3 units for every insert
    • 1 used for actual insert
    • 2 saved in β€œbank”

These saved units are used to pay the future expensive resize operations.

Result: Amortized = 3 units = O(1)


3. Potential Method (Physicist’s Method)

Define a potential function that stores energy (credit) in the data structure.

Amortized cost =
[
\text{Actual cost} + (\text{Potential after} – \text{Potential before})
]

Potential increases during cheap operations and is used up during expensive ones.

Example: Dynamic Array

Define potential Ξ¦ = number of unused slots.
When array is full, Ξ¦ = 0
Expanding increases Ξ¦ to large number.

Using this method, amortized cost again becomes O(1).


πŸ“˜ Classic Examples of Amortized Analysis


βœ” Example 1: Dynamic Array (Vector) Insertion

  • Regular insert β†’ O(1)
  • Resize (double array) β†’ O(n)

Amortized time = O(1)


βœ” Example 2: Stack Multipop Operation

Stack supports:

  • push(x): O(1)
  • pop(): O(1)
  • multipop(k): pop up to k elements

Worst-case multipop = O(n)

But total number of pops ≀ total pushes.

For n operations:
Total cost ≀ O(n)
Amortized = O(1)


βœ” Example 3: Incrementing a Binary Counter

Binary counter increments like:

0111 + 1 = 1000

Worst-case increment = O(n)
But most increments = O(1)

Total operations for n increments = 2n
Amortized = O(1)


βœ” Example 4: Union-Find with Path Compression

Union-Find operations:

  • find
  • union

With path compression + union by rank:

Amortized time = O(Ξ±(n))

Where Ξ±(n) = Inverse Ackermann function
(which grows extremely slowly, almost constant)


πŸ“˜ Key Differences: Worst Case vs Amortized vs Average Case

TypeMeaningDepends on Probability?
Worst-caseMaximum time for any inputNo
Average-caseExpected time over all inputsYes
AmortizedAverage time over sequence of operationsNo

πŸ“˜ Short Summary (Exam Notes)

  • Amortized Analysis β†’ long-term average cost of operations
  • Used when occasional expensive operations occur
  • Methods:
    βœ” Aggregate
    βœ” Accounting
    βœ” Potential
  • Applications:
    βœ” Dynamic array resizing
    βœ” Stack multipop
    βœ” Binary counter increment
    βœ” Union-Find
  • Amortized time often O(1) for many operations
  • Does NOT use probability β†’ not same as average-case