Amortized Analysis

What Is Amortized Complexity in Algorithms?

Amortized complexity bounds the average cost per operation over a sequence, even when single operations can be slow. Learn how it is written and analyzed.

On this page
  1. The short answer
  2. How it is written
  3. Worst-case and amortized side by side
  4. The potential method: a binary counter
  5. Heaps and the time complexity of heap operations
  6. Balanced trees and the contrast with splay trees
  7. Union-find
  8. When amortized is not enough
  9. Key takeaways

The short answer

Amortized complexity is a bound, usually written in Big O notation, on the average cost per operation over any sequence of operations. It lets you state a clean bound even when some single operations are slow. For example, a dynamic array’s append has an amortized complexity of O(1), even though one append can take O(n) time when the array must resize.

How it is written

You will see amortized complexity written in two common ways: “amortized O(1)” or “O(1) amortized.” Both mean the same thing. The bound describes the sequence as a whole, not the worst single call.

Readers who are new to the notation should first review big o data structures, which covers what the O-notation actually measures.

Worst-case and amortized side by side

Structure and operation Worst-case single operation Amortized
Dynamic array append O(n) O(1)
Stack with multipop O(n) O(1)
Splay tree operation O(n) O(log n)

The table shows why the distinction matters. A worst-case bound protects every call. An amortized bound protects the total over a sequence, which can be the right guarantee for a batch of work.

The potential method: a binary counter

A binary counter counts up from 0, one increment at a time. Each increment flips some bits. Suppose the counter has t trailing 1s before an increment. The increment turns those t bits to 0 and turns one 0 into a 1, so it flips t + 1 bits in total.

Define the potential as the number of 1 bits in the counter. The increment changes that number by 1 - t, because t ones become zeros and one zero becomes a one. Adding the actual cost and the change in potential gives:

  • actual cost: t + 1
  • change in potential: 1 - t
  • amortized cost: (t + 1) + (1 - t) = 2

Every increment has an amortized cost of 2, so n increments flip at most 2n bits in total. That is amortized O(1) per increment. A single increment can flip many bits, but never more than the number of bits in the counter.

Heaps and the time complexity of heap operations

Readers asking what is the time complexity of heap operations will find that the answer depends on the heap. A binary heap gives worst-case O(log n) for insert and extract-min. A Fibonacci heap is built to make some operations cheaper on average over a sequence: decrease-key has an amortized O(1) cost, and extract-min has an amortized O(log n) cost. Fibonacci heaps are more complex to implement, which is why binary heaps remain common.

Balanced trees and the contrast with splay trees

A balanced tree such as the avl tree in data structure keeps its shape strictly balanced, so each search, insert, and delete takes O(log n) in the worst case. A splay tree makes no such strict promise. It reshapes itself on each access, so a single operation can take O(n) time, but the amortized cost over a sequence is O(log n).

Union-find

Union-find structures, which track which items belong to the same set, can use two optimizations together: path compression and union by rank. With both, the amortized cost per operation is given by the inverse Ackermann function. That function grows so slowly that it is effectively constant for any input size you will meet in practice.

When amortized is not enough

Amortized bounds are a good fit for throughput, where total work over many operations matters. They are a poor fit when a single call must finish within a fixed time. Real-time control software, audio processing, and interactive user interfaces may need worst-case guarantees, even if the amortized cost is lower.

Key takeaways

  • Amortized complexity is a bound on the average cost per operation over a sequence.
  • It is written as “amortized O(1)” or “O(1) amortized.”
  • It can be lower than the worst-case cost of a single operation, which is why it is useful for batches of work.
  • It does not protect any single call, so it may not suit real-time code.

The complete guide to amortized analysis

What Amortized Means in Computer Science
Read the guide

Keep exploring

Guides from other topics

All topics →