Explain why some expensive steps average out to a small cost per operation.
Amortised cost is the total cost of a whole sequence of operations divided by their number, a guarantee for every sequence with no randomness.
Use it when most operations are cheap and a few are expensive, and you want the cost of a whole run. Signal words: amortised O(1), doubling, resize, occasionally expensive.
Start: fix a sequence of n operations, and decide what you count: copies, flips, steps.
Step: add up the cost of every operation in the run, the cheap ones and the expensive ones.
Stop: divide the total by n. Check the claim with an argument, such as the sum 1 + 2 + 4 + ... or a credit that stays at or above 0.
It does not bound one operation, and it does not use probability. A single push can still copy half of everything.
Cost: one sum over the run. Both templates below take about log n steps.
def copies_for_pushes(pushes):
# [1] The buffer starts with room for 1 item, and no copies have been made.
capacity = 1
copies = 0
# [2] A push finds the buffer full while the pushes outnumber the capacity.
while pushes > capacity:
# [3] The resize copies every item in the buffer, which is capacity items.
copies += capacity
# [4] The new buffer has twice the room.
capacity *= 2
# [5] Return the total number of copies.
return copiespublic long copiesForPushes(int pushes) {
// [1] The buffer starts with room for 1 item, and no copies have been made.
long capacity = 1;
long copies = 0;
// [2] A push finds the buffer full while the pushes outnumber the capacity.
while (pushes > capacity) {
// [3] The resize copies every item in the buffer, which is capacity items.
copies += capacity;
// [4] The new buffer has twice the room.
capacity *= 2;
}
// [5] Return the total number of copies.
return copies;
}long long copiesForPushes(int pushes) {
// [1] The buffer starts with room for 1 item, and no copies have been made.
long long capacity = 1;
long long copies = 0;
// [2] A push finds the buffer full while the pushes outnumber the capacity.
while (pushes > capacity) {
// [3] The resize copies every item in the buffer, which is capacity items.
copies += capacity;
// [4] The new buffer has twice the room.
capacity *= 2;
}
// [5] Return the total number of copies.
return copies;
}function copiesForPushes(pushes) {
// [1] The buffer starts with room for 1 item, and no copies have been made.
let capacity = 1;
let copies = 0;
// [2] A push finds the buffer full while the pushes outnumber the capacity.
while (pushes > capacity) {
// [3] The resize copies every item in the buffer, which is capacity items.
copies += capacity;
// [4] The new buffer has twice the room.
capacity *= 2;
}
// [5] Return the total number of copies.
return copies;
}def flips_for_increments(n):
# [1] No flips have been counted yet.
total = 0
step = 1
# [2] A bit that flips every step increments flips n divided by step times in n increments.
while step <= n:
total += n // step
# [3] The next bit flips half as often.
step *= 2
# [4] Return the total number of flips.
return totalpublic long flipsForIncrements(int n) {
// [1] No flips have been counted yet.
long total = 0;
long step = 1;
// [2] A bit that flips every step increments flips n divided by step times in n increments.
while (step <= n) {
total += n / step;
// [3] The next bit flips half as often.
step *= 2;
}
// [4] Return the total number of flips.
return total;
}long long flipsForIncrements(int n) {
// [1] No flips have been counted yet.
long long total = 0;
long long step = 1;
// [2] A bit that flips every step increments flips n divided by step times in n increments.
while (step <= n) {
total += n / step;
// [3] The next bit flips half as often.
step *= 2;
}
// [4] Return the total number of flips.
return total;
}function flipsForIncrements(n) {
// [1] No flips have been counted yet.
let total = 0;
let step = 1;
// [2] A bit that flips every step increments flips n divided by step times in n increments.
while (step <= n) {
total += Math.floor(n / step);
// [3] The next bit flips half as often.
step *= 2;
}
// [4] Return the total number of flips.
return total;
}This version says each push may copy n items, so 1,000 pushes cost 1,000 × 513 steps.
This version says growing the buffer by 10 slots is as good as doubling, because both resize only when full.
This version charges every push 2 units, one for the write and one saved, and says the credit pays for the copies.
This version says an amortised O(1) push never takes long.
A system with a hard deadline for every append cannot use the amortised cost. One append in 1,000 still copied 512 items.
systemdesignmastery.dev/dsa/cheatsheets/amortised-cost