Introduction to Amortized Analysis (original) (raw)

Last Updated : 25 Feb, 2026

Amortized analysis studies the average cost of operations over a sequence, rather than focusing on the worst-case of a single operation.

**Example: In a dynamic array, inserting elements is usually cheap (O(1)), but occasionally the array needs resizing, which is costly. By spreading the cost of resizing over all insertions, the amortized cost per insertion remains O(1).

Techniques of Amortized Analysis

There are three standard methods to perform amortized analysis:

**1. Aggregate Method

**2. Accounting Method

**3. Potential Method

Amortized Analysis of Dynamic Sized Array Insertions

When designing a dynamic sized array (vector in C++, list in Python and ArrayList in Java), we must decide on the size. There is a trade-off between space and time. If the size is large, we waste space.

The solution is to increase the table size whenever it's full. Here are the steps:

If space is available, insert a new item.

1

**Time Complexity: If simple analysis is used, the worst-case insertion cost is O(n). Therefore, the worst-case cost for n insertions is n * O(n), which is O(n^2). However, this analysis provides an upper bound that isn't tight for n insertions, as not all insertions take O(n) time.

amortized_cost

Amortized Analysis proves the Dynamic Table scheme has O(1) insertion time, vital in hashing.

**Note: Amortized Analysis doesn't involve probability. Randomized Analysis analyzes algorithms using randomization, such as Randomized Quick Sort, Quick Select, and Hashing.

Amortized Analysis of Insertion in a Red-Black Tree

Amortized analysis of Red-Black Tree insertions uses the Potential Method, where a new node is inserted at a leaf. A potential function φ assigns a non-negative value to the tree, and each operation may change this potential to calculate the amortized cost.

Let us define the potential function φ:

g(n) = 0, if n is red.

1, if n is black with no red children.

0, if n is black with one red child.

2, if n is black and has two red children.

where n is a node of the Red-Black Tree. The potential function φ = ∑ g(n), over all nodes.

Amortized time = c + Δφ(h)

Δφ(h) = φ(h') - φ(h)

where h and h' are the states before and after the operation, respectively, and c is the actual cost.

The change in potential should be positive for low-cost operations and negative for high-cost operations.

420851428

The insertions and their amortized analysis can be represented as:

420851429

This insertion starts by recoloring the parent and sibling. Then, the grandparent and uncle are considered, resulting in amortized costs of -1 (red grandparent), -2 (black uncle and grandparent), or +1 (red uncle and black grandparent). The insertion can be shown as:

420851430

420851431

In this insertion, the node is inserted without changes because the black depth remains the same. This occurs when the leaf has a black sibling or no sibling, as the null node is black.

So, the amortized cost of this insertion is 0.

420851432

In this insertion, we cannot recolor the leaf node, its parent, and the sibling such that the black depth stays the same as before. So, we need to perform a Left- Left rotation.

After rotation, no changes occur when the grandparent is black or red. The insertion is completed with an amortized cost of +2, as shown below:

420851433

After calculating amortized costs at the leaf level, we can analyze the overall insertion cost.

This results in a net loss of at most 1 in the potential function.

Since each unit of potential compensates for one operation, we conclude that: Δφ(h) ≤ n, where n represents the total number of nodes in the Red-Black Tree.

Applications

Amortized Analysis is useful for designing efficient algorithms for data structures such as:

  1. Dynamic Arrays
  2. Priority Queues
  3. Disjoint-Set Data Structures and many more.