Sub topic 2
Potential Method: A Stored-Energy Function
Assume Big-O notation, fixed-size arrays, and the basic operation of a hash table. The potential method assigns stored “energy” to a data structure. Cheap operations add energy, and expensive operations spend it.
Potential Method
Let a dynamic array have size s and capacity C. For a doubling array, use:
Φ = max(0, 2s - C)The amortised cost is:
amortised cost = actual cost + change in potentialAn ordinary append costs 1 to write the new element. Its potential increases by at most 2, so its amortised cost is at most 3.
When a full array grows, an append copies C elements and writes one new element. The capacity becomes 2C. Before the operation, the potential is C. After it, the potential is 2. Therefore:
amortised cost = (C + 1) + (2 - C) = 3The stored potential pays for the copy. Since the potential never becomes negative, the total amortised bound applies to every sequence of appends.
Why Growth Must Be Geometric
If capacity increases by a fixed amount, such as 16, the copies cost:
16 + 32 + 48 + ... Across n appends, this total is O(n²), so the amortised cost per append is O(n). Doubling gives copies of:
1 + 2 + 4 + ... < 2nThe total cost is O(n), making each append O(1) amortised.
Shrinking Without Thrashing
Shrink when the array is at most one-quarter full, and halve its capacity. Grow when it becomes full. These thresholds leave room between growing and shrinking.
| Rule | Action |
|---|---|
| `s == C` | Double capacity |
| `s <= C / 4` | Halve capacity |
This prevents repeated insert-delete operations near one boundary from causing constant resizing. Keep a minimum capacity so the array does not shrink below a useful size.
Chained Hash Tables and the Load Factor
A chained hash table stores collisions in bucket lists. Its load factor is α = n / m, where n is the number of entries and m is the number of buckets. With a good hash function, lookup and insertion take expected O(1 + α) time.
Resize when α exceeds a fixed limit such as 0.75. Doubling the buckets requires rehashing every entry, but geometric resizing makes the total rehashing cost linear over a sequence of insertions. Thus insertion is expected O(1) amortised.
Exercise
Why does doubling capacity keep dynamic-array append O(1) amortised?
Show answer
Each element is copied only at geometrically separated resizes, so the total number of copies over n appends is less than 2n.