Amortized analysis introduction and motivation [00SW]
- November 17, 2026
Binary counter incrementing [00SX]
- November 17, 2026
Consider the following problem.
Input: an array B[0\dots ] representing a binary number n. (Each B[i] is a single bit representing the coefficient of 2^i. Assume B is sufficiently large so we don’t have to worry about problems with overflow.)
Output: Increment the binary number, that is, modify B so that it represents the number n+1.
Algorithm Incrementing a binary counter [00SY]
- November 17, 2026
Here is an algorithm to solve the binary counter problem.
Binary counter increment analysis [00SZ]
- November 17, 2026
How long does this algorithm take?
- The best case is \Theta (1): if the last bit of n is 0 then all we have to do is flip it. The while loop never executes at all.
- The worst case is when n is of the form 2^k - 1, that is, all 1 bits, and to increment it we have to flip all the 1’s to 0s and then set the next 0 bit to 1. Since n requires \Theta (\lg n) bits to represent it, this means that incrementing n takes \Theta (\lg n) in the worst case.
Therefore, a sequence of n increment operations takes \Omega (n) time in the best case, and O(n \lg n) in the worst case. However, we don't typically do a bunch of random increment operations; more realistically, we start a counter at 0 and repeatedly increment up to n. We can observe that many such operations will be best cases, and that the worst case happens only infrequently. Can we say something more precise about how long it takes to perform a n increment operations (and thus how long a single increment operation takes on average)?
Amortized analysis in general [00T0]
- November 17, 2026
This is a common situation: we have some operation that we want to do repeatedly, and the operation does not always take the same amount of time. We can easily give an upper bound and a lower bound, but we want to be able to say something more precise about how long the entire sequence of operations will take. For this we turn to amortized analysis. The idea of “amortization” is to spread out big one-time costs more evenly (e.g. repaying a car loan or a mortgage in monthly installments).
Approaching increment cost via a table [00T1]
- November 17, 2026
The first step is almost always to just try small examples, play around, make a table, and notice patterns to come up with a guess. Let’s try it for incrementing a binary counter. We’ll make a table with the counter value n and the cumulative number of bit flips on the right-hand side. Remember we don’t need a formula for the number of bit flips (though it turns out in this case it is possible to come up with one!); we are just trying to figure out how fast it grows in relation to n.
| n | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| flips | 1 | 2 | 1 | 3 | 1 | 2 | 1 | 4 | 1 | 2 | 1 | 3 | 1 | 2 | 1 | 5 | |
| total | 0 | 1 | 3 | 4 | 7 | 8 | 10 | 11 | 15 | 16 | 18 | 19 | 22 | 23 | 25 | 26 | 31 |
We notice some patterns: whenever n is a power of two, the total number of bit flips seems to be one less than the next power of two. In fact, in general it seems that the cumulative number of bit flips is never more than 2n. That is, we conjecture that T(n) < 2n for all n > 0. Based on this evidence, we conjecture that a sequence of n increment operations starting from 0 actually takes \Theta (n), the lower bound we derived from the best case, rather than the worst-case upper bound of \Theta (n \lg n). Intuitively, it seems like the “expensive” increment operations happen infrequently enough that they don’t add too much to the total—we can “average out” their cost over the whole sequence.
The average cost of a single increment operation is just the total cost, T(n), divided by the number of operations (n). If it's true that T(n) < 2n, then the average is T(n)/n < 2n/n = 2 = \Theta (1). In this case we say that a single increment operation takes \Theta (1) amortized time: each increment takes \Theta (1) “on average”, even though an individual increment operation could take longer.
So how can we actually prove this? There are (at least) three methods; we will cover two of them.