Amortized
Amortized
• Aggregate analysis:
– (Total cost of n operations /n)
• Accounting method:
– Assign each type of operation an (different) amortized cost
– overcharge some operations,
– store the overcharge as credit on specific objects,
– then use the credit for compensation for some later operations.
• Potential method:
– Same as accounting method
– But store the credit as “potential energy” and as a whole.
•Aggregate analysis:
• Stack operations:
– PUSH(S,x), O(1)
– POP(S), O(1)
– MULTIPOP(S,k), min(s,k)
do POP(S)
k=k-1
• Let us consider a sequence of n PUSH, POP,
MULTIPOP.
– The worst case cost for MULTIPOP in the sequence is
O(n), since the stack size is at most n.
– thus the cost of the sequence is O(n2). Correct, but not
tight.
In fact, although a single MULTIPOP operation can be
expensive, any sequence of n PUSH, POP,
and MULTIPOP operations on an initially empty stack can cost
at most O(n). Why?
• Cursory analysis:
– A single execution of INCREMENT takes O(k) in the
worst case (when A contains all 1s)
– So a sequence of n executions takes O(nk) in worst case
(suppose initial counter is 0).
– This bound is correct, but not tight.
• The tight bound is O(n) for n executions.
Amortized (Aggregate) Analysis of INCREMENT(A)
• Idea:
– Assign differing charges to different operations.
– The amount of the charge is called amortized cost.
– amortized cost is more or less than actual cost.
– When amortized cost > actual cost, the difference is saved in
specific objects as credits.
– The credits can be used by later operations whose amortized
cost < actual cost.
• As a comparison, in aggregate analysis, all operations
have same amortized costs.
Accounting Method (cont.)
Ci = actual cost ,
ci’ = Amortized cost
• Actual costs:
– PUSH :1
– POP :1
– MULTIPOP: min(s,k).
• Let $1 represent each unit of cost (i.e., the flip of one bit).
– PUSH:
•Potential change: Φ(Di)- Φ(Di-1) =(s+1)-s =1.
•Amortized cost: ci' = ci + Φ(Di) - Φ(Di-1)=1+1=2.
– POP:
•Potential change: Φ(Di)- Φ(Di-1) =(s-1) –s= -1.
•Amortized cost: ci' = ci + Φ(Di) - Φ(Di-1)=1+(-1)=0.
– MULTIPOP(S,k): k'=min(s,k)
•Potential change: Φ(Di)- Φ(Di-1) = (s–k’) - s = -k’.
•Amortized cost: ci' = ci + Φ(Di) - Φ(Di-1)=k'+(-k')=0.
The amortized cost for each of the three operations is O(1) and the total
amortized cost for a sequence of n operations is O(n).
Potential method: binary counter
• Define the potential of the counter after the ith INCREMENT is Φ
(Di) =bi, the number of 1’s. clearly, Φ(Di)≥0.
• Let us compute amortized cost of an operation
– Suppose the ith operation resets ti bits.
– Actual cost ci of the operation is at most ti +1.
– If bi=0, then the ith operation resets all k bits, so bi-1=ti=k.
– If bi>0, then bi=bi-1-ti+1
– In either case, bi≤bi-1-ti+1.
– So potential change is Φ(Di) - Φ(Di-1) ≤bi-1-ti+1-bi-1=1-ti.
– So amortized cost is: ci' = ci + Φ(Di) - Φ(Di-1) ≤ ti +1+1-ti=2.
• The total amortized cost of n operations is O(n).
• Thus worst case cost is O(n).
Potential method: binary counter