47.04.01 · theoretical-cs / algorithms-analysis

Amortized Analysis: Aggregate, Accounting, and Potential-Function Methods

shipped3 tiersLean: none

Anchor (Master): CLRS 2022 Introduction to Algorithms 4e (MIT Press) §17.1-17.4; Tarjan 1985 Amortized Computational Complexity (SIAM J. Computing) (the potential-function framework, application to splay trees and union-find)

Intuition Beginner

Think about a notebook that holds 10 pages. You write entries one by one. Most of the time, you just write on the next blank page. But when the notebook fills up, you need to buy a new notebook with twice as many pages and copy all your old entries over. That copy takes a lot of time — much more than a single write.

Does this mean writing an entry is slow? Not really. The copy happens only once in a while. If you wrote 100 entries, the copy happened maybe once or twice. Averaged over all 100 writes, each one cost about as much as a single write plus a small fraction of the copy.

This is amortized analysis. Instead of asking "how much does the worst single operation cost?" it asks "how much do operations cost in total, per operation on average?" The worst-case cost of a single operation might be large (the notebook copy), but the amortized cost over a sequence is small.

There are three ways to compute the amortized cost. The aggregate method simply totals the work and divides. The accounting method (or token method) assigns a fixed charge to each operation, saves the surplus from cheap operations as credits, and spends credits on expensive ones. The potential method generalises the accounting method by defining a potential energy stored in the data structure that is released when expensive operations happen.

Visual Beginner

A dynamic array growing by doubling:

Insert into array of capacity 4 (size 3):
[1][2][3][ ]  →  [1][2][3][4]  ← cheap: O(1)

Insert into array of capacity 4 (size 4, full):
[1][2][3][4]  →  [1][2][3][4][5][ ][ ][ ]  ← expensive: allocate + copy
                new capacity 8

Insert into array of capacity 8 (size 5-7):
[1][2][3][4][5][6][ ]  ← cheap: O(1) each

Insert into array of capacity 8 (size 8, full):
→ allocate 16, copy 8 items  ← expensive again
Operation Actual cost Credits saved Credits spent Amortized cost
Insert (not full) 1 +2 0 3
Insert (triggers resize) 0 3
Insert (not full) 1 +2 0 3

The accounting method charges 3 tokens per insert. Cheap inserts bank 2 tokens. The resize spends the banked tokens on copying.

Worked example Beginner

A dynamic array starts with capacity 1 and doubles when full. Each insert into a non-full slot costs 1 (write the element). Each insert that triggers a resize costs (copy old elements, write the new one, where is the current size before doubling).

Let us total the cost of insertions starting from an empty array. The resizes happen at sizes 1, 2, 4, 8, ..., up to the largest power of 2 below . The total copy cost is (a geometric series). The writes each cost 1. So the total cost is at most .

The amortized cost per insertion is . Each insert costs amortized, even though individual inserts can cost .

Check your understanding Beginner

Formal definition Intermediate+

Let a data structure undergo a sequence of operations with actual costs . The total actual cost is .

Aggregate method. Compute directly and report the amortized cost as .

Accounting method. Assign an amortized cost to each operation . Require that the credit balance never goes negative: for all , . Then , so the amortized costs upper-bound the actual total cost.

Potential method. Let be a potential function mapping the state of the data structure after operation to a non-negative real number, with for the initial state. The amortized cost of operation is:

The total amortized cost is:

since and .

Counterexamples to common slips

  • "Amortized means every operation is ." No. Individual operations can be expensive (). The amortized bound says the average over a sequence is , not that each operation is.

  • "The accounting method and potential method are different analyses." The accounting method is a special case of the potential method where the potential equals the total credit balance. The potential method is more flexible because can be any function of the data structure state.

  • "Amortized analysis works for worst-case inputs." It does: the bound holds for any sequence of operations, not just for average-case or random inputs. The averaging is over operations in the sequence, not over probability distributions.

Key theorem with proof Intermediate+

Theorem (dynamic table: amortized insertion). Consider a dynamic table that doubles its capacity when full. Starting from an empty table with capacity 1, the amortized cost of each insertion is .

Proof (potential method). Define , where is the number of elements and is the allocated array size. Note always (size never exceeds capacity) and .

Case 1: Insertion without resize (size capacity before insertion). Actual cost . After insertion, size increases by 1:

Case 2: Insertion with resize (size capacity before insertion). Actual cost (copy elements, write new element, capacity becomes ). After insertion, new size is , new capacity is :

In both cases .

Bridge. The potential method is the foundational reason amortized analysis works as a rigorous tool: the potential captures the "stored work" in the data structure, and the amortized cost formula ensures that total amortized cost upper-bounds total actual cost. This builds toward the analysis of red-black trees in 47.04.02, where rotations during rebalancing have amortized cost, and it appears again in 47.04.03 where augmenting paths in network flow are charged against a potential that decreases with each augmentation. The central insight is that expensive operations consume potential built up by cheap ones, so the potential function serves as a "prepayment" scheme, and putting these together gives tight bounds on data structure performance that worst-case analysis alone cannot achieve.

Exercises Intermediate+

Lean formalization Intermediate+

Mathlib has no amortized analysis framework. Data structures in Mathlib (lists, arrays) are analysed by worst-case cost, and the potential-function method, accounting method, and aggregate method are absent as formal objects. A Codex.Algorithms.Amortized module with the potential method framework and a proof that dynamic table insertion has amortized cost would be the foundational target; this unit ships without it.

Advanced results Master

The potential method is the most powerful and general framework for amortized analysis. Two advanced applications demonstrate its range.

Theorem 1 (splay tree amortized analysis). Every operation on a splay tree (access, insert, delete, join, split) takes amortized time, where is the number of nodes [Tarjan 1985]. The potential function is , where is the size of the subtree rooted at . The access lemma shows that splaying a node to the root changes the potential by at most , which bounds the amortized cost. The static optimality theorem shows that splay trees perform within a constant factor of the optimal static binary search tree for any access sequence.

Theorem 2 (union-find with path compression). The union-find data structure with union by rank and path compression has amortized cost per operation, where is the inverse Ackermann function [Tarjan 1985]. The analysis uses a potential function based on the "level" and "index" of each node in the union-find forest. The amortized cost of a find operation is because each step along the find path either decreases the potential (a "good" step) or is one of at most "bad" steps per node. Since for all practical , the amortized cost is effectively constant.

Synthesis. The potential method is the foundational reason amortized analysis extends beyond simple averaging to give tight bounds on sophisticated data structures: the potential encodes the structural state of the data structure as a numerical quantity, and the amortized cost formula turns the analysis into an accounting exercise. The central insight is that the potential serves as a "savings account" built during cheap operations and spent during expensive ones, and putting these together with the right potential function for each data structure (subtree sizes for splay trees, levels for union-find, load factor for dynamic tables) yields tight or amortized bounds. This builds toward the analysis of network flow in 47.04.03 where augmenting paths are charged against a distance potential, and it appears again in 47.04.02 where red-black tree rebalancing is amortized per update.

Full proof set Master

Proposition 1 (dynamic table doubling: amortized). insertions into a dynamic table that doubles when full cost total.

Proof. The table doubles at sizes where is the smallest power of 2 at least . The copy costs are . The insertions each cost 1 to write. Total: .

Proposition 2 (binary counter: amortized per increment). increments of a binary counter starting from 0 flip bits total.

Proof. Use number of 1-bits. Each increment flips bits (where is the number of trailing 1s). The potential decreases by : ones become zeros and one zero becomes a one. Amortized cost: . Total: .

Proposition 3 (multipop stack: amortized). A stack supporting PUSH, POP, and MULTIPOP() has amortized cost per operation using the potential (stack size).

Proof. PUSH: actual cost 1, , amortized . POP: actual cost 1, , amortized . MULTIPOP(): actual cost , , amortized . Since and , total amortized cost total actual cost. Total amortized for operations: at most .

Connections Master

  • 50.03.01 — Big-O notation and complexity analysis provide the language for expressing amortized bounds; the distinction between worst-case and amortized cost parallels the distinction between worst-case and average-case analysis.

  • 01.01.01 — Fields and basic algebra underpin the arithmetic of potential functions; the requirement and uses ordered field properties.

  • 47.04.02 — Red-black tree rebalancing uses amortized rotations per update; the potential method applies to the analysis of tree height invariants.

  • 47.04.03 — Network flow algorithms use amortized arguments to bound the total work across all augmenting path iterations.

Historical & philosophical context Master

Amortized analysis was introduced by Robert Tarjan in his 1985 paper "Amortized Computational Complexity" [Tarjan 1985]. Tarjan developed the potential-function framework to analyse splay trees, which he had introduced with Daniel Sleator in 1983. The key insight was that splay trees, despite having no explicit balance condition, achieve amortized performance — a result invisible to worst-case analysis, since individual operations can trigger long chains of rotations.

The term "amortized" comes from finance, where an expense is spread over multiple periods. Tarjan's contribution was to make this intuition mathematically precise via the potential function, turning a financial metaphor into a proof technique. The accounting method was known informally before Tarjan's paper, but the potential method unified and generalised it, enabling the analysis of union-find, splay trees, and dynamic tables within a single framework.

Bibliography Master

@book{clrs2022,
  author    = {Cormen, T. H. and Leiserson, C. E. and Rivest, R. L. and Stein, C.},
  title     = {Introduction to Algorithms},
  edition   = {4th},
  publisher = {MIT Press},
  year      = {2022},
}
@article{tarjan1985amortized,
  author  = {Tarjan, Robert Endre},
  title   = {Amortized Computational Complexity},
  journal = {SIAM Journal on Algebraic and Discrete Methods},
  volume  = {6},
  number  = {2},
  pages   = {306--318},
  year    = {1985},
}
@article{sleator1985self,
  author  = {Sleator, Daniel D. and Tarjan, Robert Endre},
  title   = {Self-Adjusting Binary Search Trees},
  journal = {Journal of the ACM},
  volume  = {32},
  number  = {3},
  pages   = {652--686},
  year    = {1985},
}