Amortized Analysis
Amortized Analysis
By
Why: Amortized analysis can be used to show that the average cost of an
operation is small, if one averages over a sequence of operations, even though a
single operation might be expensive.
How does it differ from average case analysis: It differs from average case
analysis in that probability is not involved.
Common techniques:
Aggregate method
Accounting method
Potential method
Aggregate Method
Here, we show that if a sequence of n operations takes worst case time T(n) in
total. In the worst case, the average cost or amortized cost, per operation is
therefore T(n) / n.
Algorithm MULTIPOP(S, k)
An Illustration:
Figure 1: (a). The top 4 objects are popped by MULTIPOP(S, 4), whose result is
shown in (b). The next operation is MULTIPOP(S, 7), which empties the stack
shown in (c) since there were fewer than 7 objects remaining.
So, for any sequence of n PUSH, POP, and MULTIPOP operations takes a total
of O(n) time.
The average cost of an operation is O(n)/n = O(1).
Algorithm INCREMENT(A)
1: i = 0
2: while i < length[A]) and A[i] == 1
3: A[i] = 0
4: i=i+1
5: end while
6: if i < length[A] then
7: A[i] = 1
8: end if
Aggregate Analysis: Consider the following figure:
n/2i
POTENTIAL METHOD
Illustration
Stack Operations:
Exercise 2:
Ex. 17.2-2: A sequence of n operations is performed on a data structure. The ith
operation costs i if i is an exact power of 2, and 1 otherwise. Use Account method
to determine the amortized cost per operation.
Solution: