Amortised analysis: the accounting and potential methods
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A growing array usually appends in one step, but now and then it must copy everything into a bigger array, so one append can cost n. Watch nine appends to a doubling table, then prove three ways that any n appends cost at most 3n: add up the whole run (aggregate), charge 3 coins per append and keep a bank (accounting), and score the table with Φ = 2n − cap (potential). It builds on "Comparing growth rates: O, Omega and Theta", the Software lesson "How fast is fast? Big O", and the Maths lesson "Logarithms and exponents" for why there are only about log₂ n doublings.
A table that grows
A dynamic table is an array that can grow. append(x) writes x into the next free slot. When every slot is full, it makes a new array twice as big and copies each item across. Cost rule: each item written into a slot costs 1.
cost model: 1 unit per item written into a slot 1 def append(t, x): 2 if t.n == t.cap: 3 t.cap = max(1, 2 * t.cap) 4 new = [None] * t.cap 5 for i in range(t.n): 6 new[i] = t.a[i] 1 per run 7 t.a = new 8 t.a[t.n] = x 1 per run 9 t.n += 1 n = items stored, cap = slots in the array the table starts empty: n = 0, cap = 0
اقرأ الدرس خطوة بخطوة
A table that grows
A dynamic table is an array that can grow. append(x) writes x into the next free slot. When every slot is full, it makes a new array twice as big and copies each item across. Cost rule: each item written into a slot costs 1.
cost model: 1 unit per item written into a slot 1 def append(t, x): 2 if t.n == t.cap: 3 t.cap = max(1, 2 * t.cap) 4 new = [None] * t.cap 5 for i in range(t.n): 6 new[i] = t.a[i] 1 per run 7 t.a = new 8 t.a[t.n] = x 1 per run 9 t.n += 1 n = items stored, cap = slots in the array the table starts empty: n = 0, cap = 0Appends 1 to 3
Append 1 makes one slot and writes: cost 1. Append 2 finds it full, doubles to 2 slots and copies 1 item: cost 2. Append 3 doubles to 4 slots and copies 2: cost 3.
append 1: full, cap 0 → 1, copy 0 cost 1 append 2: full, cap 1 → 2, copy 1 cost 2 append 3: full, cap 2 → 4, copy 2 cost 3 total so far: 6One append can cost n
Append 4 has a free slot: cost 1. Append 5 finds all 4 slots full, doubles to 8 and copies all 4 items: cost 5. A single append can cost as much as n, so its worst case is O(n).
append 4: free slot cost 1 append 5: full, cap 4 → 8, copy 4 cost 5 total so far: 12 worst case of one append: n copies + 1 write = O(n)Aggregate: add it all up
Appends 6, 7 and 8 cost 1 each. Eight appends cost 15 in total, not eight worst cases of 8. The copies add up to 1 + 2 + 4 = 7, less than n. Aggregate method: 15 ÷ 8 is under 3 per append.
append 6: free slot cost 1 append 7: free slot cost 1 append 8: free slot cost 1 costs: 1 + 2 + 3 + 1 + 5 + 1 + 1 + 1 = 15 writes 8 + copies 7 = 15 < 3 × 8 = 24 copies are 1 + 2 + 4 + ... < 2n, so n appends cost < 3nAccounting: 3 coins each
Accounting method: charge every append 3 coins, whatever it really costs. Each item written spends 1 coin; the rest waits in the bank. Replay from empty: after appends 1, 2 and 3 the bank holds 2, 3, then 3 coins.
charge 3 coins per append, spend 1 coin per item written append 1: +3 coins − 1 spent bank 0 → 2 append 2: +3 coins − 2 spent bank 2 → 3 append 3: +3 coins − 3 spent bank 3 → 3The bank pays for copies
Append 4 is cheap: the bank grows to 5. Append 5 brings 3 more coins, then spends 4 on copies and 1 on its write: 3 left. The bank never goes below zero, so 3 coins cover every cost.
append 4: +3 coins − 1 spent bank 3 → 5 append 5: +3 coins − 5 spent bank 5 → 3 append 5: 5 + 3 = 8 coins, spends 5 bank ≥ 0 after every appendCheap appends save up
Appends 6, 7 and 8 each spend 1 coin and save 2: the bank climbs to 9. Cheap appends prepay the next doubling. So 3 coins per append is the amortised cost: O(1), although one append can cost O(n).
append 6: +3 coins − 1 spent bank 3 → 5 append 7: +3 coins − 1 spent bank 5 → 7 append 8: +3 coins − 1 spent bank 7 → 9 spent 15 of 24 coins: total cost ≤ 3n the next append must copy 8 items: the bank holds 9Potential: a score Φ
Potential method: no coins to track, just a score for the table, Φ = 2n − cap. Here n = 8 and cap = 8, so Φ = 8: two units for each item past the half-way mark. Φ never drops below 0.
Φ = 2n − cap after append 1: n 1, cap 1 Φ = 1 after append 2: n 2, cap 2 Φ = 2 after append 3: n 3, cap 4 Φ = 2 after append 4: n 4, cap 4 Φ = 4 after append 5: n 5, cap 8 Φ = 2 after append 6: n 6, cap 8 Φ = 4 after append 7: n 7, cap 8 Φ = 6 after append 8: n 8, cap 8 Φ = 8 after a doubling the table is half full, so Φ ≥ 0Φ pays for the doubling
Amortised cost = actual cost + change in Φ. Append 9 copies 8 items and writes 1: actual 9. With 16 slots, Φ falls from 8 to 2. Amortised: 9 + (2 − 8) = 3. A cheap append: 1 + 2 = 3.
amortised = actual + (Φ after − Φ before) append 1: 1 + (1 − 0) = 2 append 2: 2 + (2 − 1) = 3 append 3: 3 + (2 − 2) = 3 append 4: 1 + (4 − 2) = 3 append 5: 5 + (2 − 4) = 3 append 6: 1 + (4 − 2) = 3 append 7: 1 + (6 − 4) = 3 append 8: 1 + (8 − 6) = 3 append 9: 9 + (2 − 8) = 3 every append: 3 (the first: 2)Amortised O(1)
Both methods give at most 3 per append, so any n appends from empty cost at most 3n: amortised O(1). Here 9 appends cost 24, under 27. Amortised is a promise about every sequence, not an average over lucky inputs.
costs: 1 + 2 + 3 + 1 + 5 + 1 + 1 + 1 + 9 = 24 24 ≤ 3 × 9 = 27 sum of actual = sum of amortised − Φ(end) + Φ(start) ≤ 3n − Φ(end) + 0 ≤ 3n so one append is O(1) amortised, O(n) worst case
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01The table holds 8 items in 8 slots. What does the next append cost under the lesson's rule (1 per item written)?
The table is full, so append makes 16 slots, copies all 8 items (8 units) and then writes the new item (1 unit): 9 in total.
02Starting from empty, what do the first 8 appends cost in total?
The costs are 1, 2, 3, 1, 5, 1, 1, 1. That is 8 writes plus 1 + 2 + 4 = 7 copies: 15, under the 3 × 8 = 24 bound.
03In the accounting method, every append is charged 3 coins. What must stay true for 3 to be a valid amortised cost?
Coins may be saved by cheap appends and spent by expensive ones. As long as the bank never goes negative, the coins charged cover all the real work, so n appends cost at most 3n.
04With Φ = 2n − cap, a full table of 4 items (cap 4) gets one more append. What is the amortised cost, actual + ΔΦ?
Actual cost: 4 copies + 1 write = 5. Before, Φ = 2·4 − 4 = 4; after, n = 5 and cap = 8, so Φ = 2. Amortised: 5 + (2 − 4) = 3.
05Python's list.append is described as amortised O(1). What does that promise?
Amortised bounds hold for every sequence of operations. An occasional append still has to move every item, but the total over n appends stays proportional to n.
المسرد
- Dynamic table
- An array that grows when it is full by making a bigger array and copying the items across. Python's list works this way.
- Capacity (cap)
- How many slots the current array has. The number of items stored, n, is never more than cap.
- Worst case
- The most one operation can cost. For an append to a doubling table it is n copies plus 1 write: O(n).
- Amortised cost
- A cost per operation that, added up over any sequence, is at least the real total. Here it is 3 per append, so O(1).
- Aggregate method
- Add up the real cost of a whole sequence of n operations, then divide by n.
- Accounting method
- Charge each operation a fixed price, save what it does not use as credit, and show the credit never goes negative.
- Potential (Φ)
- A number computed from the data structure's state, like stored-up credit. Amortised cost = actual cost + the change in Φ.
الملاحظات والمصادر
- Cost model: 1 unit for each item written into a slot, either a copy or the new item. Making the empty new array is not counted; counting it changes the constants, not the O(1) result.
- The aggregate, accounting and potential methods follow the MIT 6.046 notes on amortization and CLRS (§17.3 and §17.4 in the 2nd edition, where the potential of a dynamic table is 2·num − size).
- The very first append costs 1 and is charged 3, so the bank always holds one coin more than Φ. Its amortised cost under the potential method is 2: 1 + (1 − 0).
- Python's own list uses the same idea with milder growth (CPython's list sizes go 0, 4, 8, 16, 24, 32, 40, 52, ...), and append is still amortised O(1).
- Amortised is not average-case. An average over random inputs can be unlucky; an amortised bound holds for every sequence of operations.
- MIT 6.046J Design and Analysis of Algorithms (Spring 2015), Lecture 5 notes: Amortization
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 2 notes: Data Structures (dynamic arrays, amortized cost)
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 2nd ed., §17.3 The potential method and §17.4 Dynamic tables (cited in Wikipedia: Potential method)
- Sedgewick and Wayne, Algorithms 4th ed., §1.3 Bags, Queues, and Stacks (resizing arrays)
- Sedgewick and Wayne, Algorithms 4th ed., §1.4 Analysis of Algorithms (amortized analysis)
- Python documentation: Built-in Types, mutable sequence methods (append)
- CPython source: Objects/listobject.c (list over-allocation)
اختبار يتاح بعد الخطوة الأخيرة.