Amortized Analysis

What Is Amortized Cost? Definition With an Example

Amortized cost is the average cost of an operation over a sequence of operations. Here is a definition with a dynamic array example and a stack example.

On this page
  1. Definition
  2. Example 1: a dynamic array with doubling
  3. Example 2: a stack with multipop
  4. Amortized cost in accounting
  5. When the idea helps

Definition

The amortized cost of an operation is its share of the total cost of a sequence of operations. There are two common ways to find it:

  • Aggregate method. Add up the actual cost of n operations and divide by n.
  • Accounting method. Charge each operation a fixed amount, which may be more than its actual cost. The extra is saved as credit to pay for later expensive steps.

Both give the same kind of answer: a per-operation cost that describes the sequence as a whole.

Example 1: a dynamic array with doubling

A dynamic array starts with room for one item. When it is full, it allocates twice the space and copies the items over. Here are eight insertions, with each step’s cost:

Insertion Cost
1 1
2 2 (resize, copy 1 item, then write)
3 3 (resize, copy 2 items, then write)
4 1
5 5 (resize, copy 4 items, then write)
6, 7, 8 1 each

The total is 15, so the aggregate amortized cost is 15 divided by 8, about 1.9 per insertion.

The accounting method gives the same kind of answer with a simpler story. Charge each insertion 3 units: 1 unit pays for writing the item, and 2 units are saved as credit. When the array later resizes, the saved credit pays for copying the items. Because the charge is the same for every insertion, the amortized cost is a constant, 3. The charge is an upper bound, not the exact cost of each insertion.

Example 2: a stack with multipop

A stack supports three operations: push, pop, and multipop. Push adds one item. Pop removes one item. Multipop removes up to k items, and its actual cost is the number of items it removes.

Each item is pushed once and can be popped at most once, whether by pop or by multipop. Over any sequence of n operations, the total pushes are at most n, and the total pops are at most the total pushes. The total cost is therefore at most 2n, which makes the amortized cost at most 2 per operation. The cost of a single multipop can still be large, but the average over the whole sequence stays constant.

Amortized cost in accounting

The phrase “amortized cost” also appears in finance and accounting, where it refers to the balance of a loan or bond after its premium or discount has been spread over time. That is a separate meaning. In computer science, amortized cost always refers to the average cost of operations over a sequence.

When the idea helps

Amortized cost is useful for judging how a data structure performs over many operations, such as a long series of inserts or a busy stack. It is less useful when a single call must finish within a fixed time, because one operation in the sequence can still be expensive.

The complete guide to amortized analysis

What Amortized Means in Computer Science
Read the guide

Keep exploring

Guides from other topics

All topics →

AVL Trees in Data Structures

An AVL tree is a self-balancing binary search tree. Learn how balance factors and rotations keep search, insert, and delete at O(log n) in the worst case.

AVL Trees 4 min read