ENع
→Amortised cost
الدرس 02 · Measuring cost

Amortised analysis: the accounting and potential methods

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةBeginner
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 2 · تفاعلي
01 / 10

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. Appends 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: 6
  3. One 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)
  4. 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 < 3n
  5. Accounting: 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 → 3
  6. The 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 append
  7. Cheap 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 9
  8. Potential: 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
  9. Φ 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)
  10. 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

اختبار يتاح بعد الخطوة الأخيرة.