+ Some data structures have an operation that is usually trivial and + occasionally enormous. Appending to a dynamic array normally writes one + slot, but when the buffer fills it allocates a bigger one and copies + everything across. Quoting the worst case for that operation gives + O(n), which is technically correct and deeply misleading — it suggests + that building a list of a million items costs a trillion operations, + when it actually costs about two million. +
++ Amortized analysis measures the average cost per operation across a + worst-case sequence of operations. Instead of asking "how bad + can one append be?", it asks "how bad can n appends be, in + total?" — and then divides. When the expensive cases are + structurally guaranteed to be rare, that average is the honest number. +
++ A dynamic array keeps a buffer with some capacity. Appending writes one + element; if the buffer is full, it first allocates one of double the + capacity and copies every existing element across: +
++ The picture contains the entire argument. The spikes double in height — + but they also double in spacing. Each resize is twice as expensive as + the last and happens half as often, and those two effects cancel + exactly. +
++ The simplest method: add up the cost of the whole sequence, then + divide by the number of operations. For n appends, the writes cost n, + and the copies happen at sizes 1, 2, 4, 8, … up to n: +
++ Running this for real confirms it: the total cost of n appends divided + by n settles at about 2.05 and stays there, whether n is a thousand or + a million. The bound of 3 is comfortable, and the important point is + that it is a constant — it does not creep upward with n. +
++ A second classic. Incrementing a binary counter flips a trailing run of + 1s to 0 and then one 0 to 1, so a single increment can flip Θ(log n) + bits. Watch what actually happens over a sequence: +
++ After 8 increments only 15 bits have flipped, not 8 × 4. The reason is + visible in the table: bit 0 flips every time, bit 1 flips every second + increment, bit 2 every fourth, and so on: +
++ Measured over 100,000 increments, the ratio is 1.99998 flips per + increment — converging on 2 exactly as the series predicts. +
+ ++ Also called the banker's method. You invent a charge for + each operation — its amortized cost — which may be more or less than + what the operation really costs. The surplus is stored as{" "} + credit on the data structure and later spent on expensive + operations. +
++ One rule makes the method valid: the credit balance must never go + negative. If it never does, the total amount charged is an upper + bound on the total real cost, which is exactly what an amortized bound + claims. +
+For the dynamic array, charge 3 units per append and spend them as:
++ When a resize of a size-n buffer happens, the n elements to be copied + are covered: the n/2 elements added since the last resize carry 2 units + each, which is enough for themselves and for one older element apiece. + The balance never goes negative, so append is O(1) amortized. +
++ The method's advantage over aggregate analysis is that different + operations can carry different charges. For a stack supporting push, + pop and multipop(k), charge 2 for a push (1 to push, 1 banked for the + eventual pop) and 0 for pop and multipop. Every pop is then paid for by + the credit its push left behind — so even a multipop that removes a + thousand elements is free, because those thousand pushes already paid. +
++ The physicist's method, and the most powerful of the three. + Instead of tracking credit on individual elements, define a{" "} + potential function Φ that maps the whole state of the data + structure to a number — the stored-up work it represents. +
++ Summed over a sequence, the Φ terms telescope: every intermediate value + appears once positive and once negative, leaving only Φ(end) − Φ(start). + So as long as Φ never drops below its starting value — usually + arranged by setting Φ(D₀) = 0 and keeping Φ ≥ 0 — the total amortized + cost is an upper bound on the total real cost. +
++ Binary counter. Let Φ be the number of 1 bits. An increment that + flips k trailing 1s to 0 and one 0 to 1 has actual cost k + 1, and + changes the bit count by 1 − k: +
++ The k vanishing is the whole trick. An expensive increment is expensive + precisely because it destroys many 1 bits, and destroying them releases + exactly the potential needed to pay for the work. +
++ Dynamic array. Let Φ = 2·size − capacity. Immediately after a + resize, size is half of capacity so Φ = 0; as appends fill the buffer, + Φ climbs to equal capacity by the time the next resize is due — having + accumulated precisely enough potential to fund the copy. Working through + both cases gives an amortized cost of 3 per append, agreeing with the + accounting method. +
++ All three are provably equivalent in power — any bound one can + establish, the others can too. Aggregate analysis is the one to reach + for when every operation is the same kind. The accounting method suits + structures with several operation types. The potential method is the + standard choice for anything genuinely difficult, because once Φ is + chosen the rest is mechanical. +
++ These two are constantly conflated, and the distinction matters: +
++ Union-Find is the most striking entry. With union by rank and path + compression, a sequence of m operations costs O(m·α(n)), where α is the + inverse Ackermann function — a function that grows so slowly it is below + 5 for any n that could be written down in this universe. The + per-operation worst case is still logarithmic; only the amortized + analysis reveals that the structure is effectively constant-time. +
++ Amortized analysis answers "how much work in total?". Some + systems need to ask "how long will this one call + take?", and for them the average is the wrong statistic: +
++ The engineering response is not to abandon the structure but to spread + the cost deliberately — incremental or background resizing, or + preallocating capacity up front when the final size is known. Both + convert a rare large stall into a small predictable overhead on every + operation. +
++ {faq.q} +
+{faq.a}
+