Dynamic arrays and amortized cost
Explain how a dynamic array doubles its capacity when it is full, and show that n appends copy fewer than 2n elements in all, so each append is O(1) amortized, while an insert at the front costs Θ(n).
It opens on
A full array has room for 4 items and holds 4. We append a fifth, and the array is rebuilt bigger. Will the new array have room for 5, or for 8?
Why it matters
Python's list, Java's ArrayList and C++'s vector all grow this way, and the argument that a single append may copy the whole array yet n appends stay cheap in all is the model for judging any operation whose cost is large only now and then.
Good to know first
- Lists: indexing and mutation: a list holds items by index and append adds one at the end
- Analyzing loops: the number of runs of a line is counted and sums such as 1 + 2 + ... + n are used
- Big-O, Big-Omega and Big-Theta: O and Θ keep the fastest term and drop constants
Step by step
Append and the resize
Describe an append to a dynamic array with room and to a full one, and give the cost of each.
We append 5 to the full array 7, 2, 9, 4. The new array has capacity 2 × 4 = 8, the loop copies 4 items, and then 5 is written, so this append costs 4 + 1 = 5 steps.
A common mix-up
Every append copies the array.
In fact: An append copies only when the array is full, so most appends cost 1 step.
Fewer than 2n copies
Show that n appends from an empty array of capacity 1 copy fewer than 2n elements in all, so the cost of n appends is less than 3n and an append is O(1) amortized.
Append 1 finds room, so it costs 1. Append 2 finds the array full with 1 item, copies it, and the capacity becomes 2, so it costs 2. Append 3 finds 2 items in 2 cells, copies 2, capacity 4, cost 3. Append 4 finds room, cost 1.
the first four rows
A common mix-up
Amortized O(1) means an append is O(1) on average over random inputs, or every append is O(1).
In fact: It is a guarantee on the total cost of any n appends, and single appends still cost up to n.
Growing by a fixed amount
Show that growing the capacity by a fixed 10 each time makes n appends cost Θ(n²) in all, against fewer than 2n copies for doubling.
The resizes happen at sizes 10, 20, 30, and so on, and they copy 10, 20, 30, and so on. For n = 100 the copies are 10 + 20 + ... + 90 = 450, against 127 for doubling.
the row for n = 100
A common mix-up
Any growth rule gives O(1) amortized appends.
In fact: Adding a fixed amount makes the total Θ(n²), and only multiplying the capacity by a fixed factor keeps it O(n).
Insert at the front
Show that inserting at the front of an array moves every item and costs Θ(n) each time, so n front inserts cost Θ(n²) in all.
7 moves from cell 2 to cell 3, then 2 moves from cell 1 to cell 2, then 5 moves from cell 0 to cell 1, and then 9 is written into cell 0. That is 3 moves for 3 items.
the array 9, 5, 2, 7
A common mix-up
Insert at the front is O(1) amortized like append.
In fact: An append is only now and then expensive, but an insert at the front moves every item every time, so it is Θ(n) each.
Try it
An array of capacity 8 holds 8 items. What does the next append cost, and what is the capacity after it?Answer
9 steps, with capacity 16
Hint: Copy 8, then write 1
How many copies in all do 40 appends make to a doubling array of capacity 1?Answer
63, from resizes at sizes 1, 2, 4, 8, 16 and 32
Hint: 1 + 2 + 4 + 8 + 16 + 32
A growth of 4 starting at capacity 4: how many copies do 20 appends make?Answer
40
Hint: Resizes at sizes 4, 8, 12 and 16
How many items move when 9 is inserted at index 0 of 5, 2, 7, 1, 6?Answer
5
Hint: Every item shifts
Appends 6, 7 and 8 come next. What does each cost, and what does the ninth append do?Answer
Each costs 1 step, a plain write into a free cell; the ninth finds the array full with 8 items, copies all 8 into an array of capacity 16 and costs 9.
