Amortized Analysis: Aggregate, Accounting and Potential Methods
Appending to a Python list is described as O(1), yet now and then an append has to copy the entire list into a bigger block of memory, which takes O(n). Both statements are true. The first is an amortized bound: it describes the average cost per operation over any sequence of operations, with the rare expensive steps paid for by the many cheap ones. This guide shows why the per-operation worst case is misleading here, works the three standard methods of amortized analysis on a dynamic array, and adds a second example.
The problem with per-operation worst case
A dynamic array keeps its items in a fixed-size block with some spare room. When an append finds the block full, it allocates a larger block, copies every existing item across, and then writes the new one. The worst single append on an array of n items therefore costs n + 1 steps.
If you multiply that worst case by n appends, you get an O(n^2) bound for building a list of n items. That bound is true but far too pessimistic, because the expensive appends are rare: with doubling, a resize happens only when the size reaches a power of two. Amortized analysis gives the honest answer, O(n) in total, O(1) per append.
The model
The Amortized Analysis Calculator uses a simple, explicit model, and so will we:
- the array starts with capacity 1;
- writing one item costs 1;
- when an append finds the array full, every existing item is copied (cost 1 per item) and the capacity becomes max(capacity + 1, ceil(capacity x growth)), where growth is a factor above 1 (2 means doubling).
Total cost is n unit writes plus all copies; amortized cost is total divided by n. Real allocators differ in details, but the conclusion does not depend on them as long as the capacity grows by a constant factor.
Method 1: aggregate analysis
The aggregate method adds up the actual cost of the whole sequence and divides by the number of operations.
With doubling and n = 8, the array is full at appends 2, 3 and 5, which copy 1, 2 and 4 items. Total cost is 8 writes + 7 copies = 15, so the amortized cost is 15 / 8 = 1.88 (rounded). For n = 1000 there are 10 resizes copying 1 + 2 + 4 + ... + 512 = 1023 items, a total of 2023 and an amortized cost of 2.02.
The general argument: the copies form a geometric series 1 + 2 + 4 + ... up to the largest power of two below n, and a geometric series with ratio 2 sums to less than twice its last term. So the copies total less than 2n, the whole sequence costs less than 3n, and each append is O(1) amortized. The bound holds for every n, not only powers of two. Just after a resize the average is at its highest: n = 17 costs 48 in total (the 17th append copied 16 items), an amortized 2.82, still under 3.
The growth factor changes the constant, not the class. With growth 1.5 and n = 1000 the calculator reports 16 resizes, total 3120, amortized 3.12; with growth 3, 7 resizes, total 2093, amortized 2.09. A smaller factor wastes less spare memory but resizes more often. What does break the bound is growing by a fixed amount (say, 10 slots at a time): then there are about n / 10 resizes, the copies sum to Theta(n^2), and each append is Theta(n) amortized.
Method 2: the accounting method
The accounting method charges each operation a fixed fee, possibly more than its actual cost, and stores the surplus as credit on the data structure. The analysis is valid if the credit never goes negative, because then the fees cover every real cost.
Charge 3 for every append. One unit pays for writing the new item. The other two are left on the item as credit. Consider a resize from capacity c to 2c. Since the previous resize, c / 2 new items have been appended, each carrying 2 credits, which is c credits in all: exactly enough to pay for copying all c items. After the copy, the credit is spent and the cycle starts again. Credit never goes negative, so n appends cost at most 3n.
Method 3: the potential method
The potential method replaces per-item credit with a single function Phi of the structure's state, its "stored energy". The amortized cost of an operation is its actual cost plus the change in Phi. As long as Phi never ends below its starting value, the total amortized cost is an upper bound on the total actual cost.
For a doubling array, use Phi = 2 x size - capacity.
- An append with no resize costs 1 and raises Phi by 2: amortized cost 3.
- An append that resizes from capacity c (with size c) costs c + 1. Phi goes from 2c - c = c to 2(c + 1) - 2c = 2, a change of 2 - c. Amortized cost: (c + 1) + (2 - c) = 3.
Phi starts at -1 (empty, capacity 1) and is never below 0 after the first append, because a doubling array is always at least half full. So every append costs 3 amortized, the same answer as the other two methods.
All three methods give the same bound here. Aggregate analysis is the quickest when the sequence is easy to sum; the accounting and potential methods are more useful when different operations have different costs.
Amortized is not average case
Both involve "average", but they mean different things:
- Average case is an expectation over a probability distribution of inputs (or of random choices). It can be wrong for a particular input.
- Amortized cost involves no probability. It is a worst-case guarantee on the total cost of any sequence of n operations, divided by n.
"Appends are O(1) amortized" means no sequence of n appends can cost more than O(n). "Hash table lookups are O(1) on average" means typical inputs are fast, while an unlucky set of keys can be slow. See the best, average and worst case guide for the average-case side.
One thing amortized bounds do not promise is that each individual operation is fast. A single append that triggers a resize still takes Theta(n). If you need every operation to finish within a fixed time, an amortized bound is not enough.
Shrinking needs care
If an array also shrinks, the thresholds matter. Halving the capacity as soon as the array drops below half full lets an adversary alternate one append and one pop right at the boundary, forcing a full copy each time, so every operation costs Theta(n). The standard fix is to shrink only when the array falls to a quarter full, which restores O(1) amortized cost for any mix of appends and pops.
A second example: a binary counter
Incrementing a binary counter flips trailing 1s to 0 and then one 0 to 1. A single increment from 0111 to 1000 flips 4 bits, and in a k-bit counter one increment can flip all k bits. But bit 0 flips on every increment, bit 1 on every second, bit 2 on every fourth, and so on, so n increments flip fewer than n + n/2 + n/4 + ... < 2n bits. That is O(1) amortized per increment.
Checking the numbers in Python
Examples use Python 3.10 or later and only the standard library. This simulator follows the same model as the calculator and prints the number of resizes, the total cost and the amortized cost.
import math
def append_cost(n, growth=2.0):
capacity, size, copies, resizes = 1, 0, 0, 0
for _ in range(n):
if size == capacity:
copies += size
capacity = max(capacity + 1, math.ceil(capacity * growth))
resizes += 1
size += 1
total = n + copies
return resizes, total, round(total / n, 2)
for n, growth in [(8, 2), (16, 2), (17, 2), (1000, 2), (1000, 1.5), (1000, 3)]:
print(n, growth, append_cost(n, growth))
Output (n, growth, then resizes, total, amortized):
8 2 (3, 15, 1.88)
16 2 (4, 31, 1.94)
17 2 (5, 48, 2.82)
1000 2 (10, 2023, 2.02)
1000 1.5 (16, 3120, 3.12)
1000 3 (7, 2093, 2.09)
Every line matches the calculator: 15 and 1.88 for n = 8, 2023 and 2.02 for n = 1000, 3120 and 3.12 at growth 1.5. Here is the binary counter, counting flips:
def increment(bits):
flips = 0
i = 0
while i < len(bits) and bits[i] == 1:
bits[i] = 0
flips += 1
i += 1
if i < len(bits):
bits[i] = 1
flips += 1
return flips
for n in [8, 16, 1000]:
bits = [0] * 16
total = sum(increment(bits) for _ in range(n))
print(n, total, round(total / n, 2))
Output (increments, total flips, flips per increment):
8 15 1.88
16 31 1.94
1000 1994 1.99
The totals stay below 2n, as the argument predicts. For how Python's own list uses this idea, and which list operations are not O(1), see the arrays and dynamic arrays guide.
Check yourself
- Why does growing the capacity by 10 slots each time make appends Theta(n) amortized? (There are about n / 10 resizes, and the copies form an arithmetic series that sums to Theta(n^2).)
- Is "amortized O(1)" a statement about random inputs? (No. It holds for every sequence of operations.)