Amortized Analysis

Amortized Analysis

Definition

Amortized analysis is a method for analyzing the average cost per operation in a sequence by focusing on worst-case scenarios to determine average running time per operation.

The core idea is that costly operations can be offset by subsequent operations to reduce their impact over time.

It is useful when operations are not uniformly distributed in terms of their costs like dynamic arrays.

Differences with Other Analyzes

  • Average-case analysis averages over all possible inputs.
  • Probabilistic analysis averages over all possible random choices.
  • Amortized analysis averages over a sequence of operations.

Amortized analysis typically assumes worst-case inputs and disallow for random choices.

Amortized analysis shows that across a sequence of operations, the average per operation stays small, even though some individual operations are expensive.

Methods of Amortized Analysis

Aggregate Method

If the upper bound on the total cost of a sequence of nn operations is T(n)T(n), then the average cost per operation is:

T(n)n \frac{T(n)}{n}

Accounting Method

Let cic_i be the actual cost of the ithi\text{th} operation, and cic^{\prime}_i be amortized charge of the ithi\text{th} operation, then we require:

ni=1ncii=1nci \forall n \quad \sum^n_{i = 1}{c_i} \le \sum^n_{i = 1}{c^{\prime}_i}

It shows that total amortized cost \ge total actual cost.

So amortized cost provides an upper bound on the true cost.

The Potential Method

Let:

  • DiD_i be data structure after the ithi^{\text{th}} operation.
  • Φ(Di)\Phi(D_i) be the potential of DiD_i.
  • cic_i be the actual cost of the ithi^{\text{th}} operation.

Then the amortized cost is actual cost + potential change.

ci^=ci+Φ(Di)Φ(Di1) \hat{c_i} = c_i + \Phi(D_i) - \Phi(D_{i - 1})

And the total amortized cost for nn operations is:

i=1nci^=i=1nci+Φ(Dn)Φ(D0) \sum^n_{i = 1}{\hat{c_i}} = \sum^n_{i = 1}{c_i} + \Phi(D_n) - \Phi(D_0)

If iΦ(D0)=0\forall i \quad \Phi(D_0) = 0 and Φ(Di)0\Phi(D_i) \ge 0, then:

i=1ncii=1nci^ \sum^n_{i = 1}{c_i} \le \sum^n_{i = 1}{\hat{c_i}}

The key points are:

  • If potential increases — we are overcharging (extra stored for future).
  • If potential decreases — we are undercharging (use stored potential).
  • Potential method ensures costs are “smoothed out” over operations.
Last updated on