Amortized Analysis
Amortized Analysis
17)
Not just consider one operation, but a sequence of operations on a given data structure. Average cost over a sequence of operations. Probabilistic analysis:
Average case running time: average over all possible inputs for one algorithm (operation). If using probability, called expected running time.
Amortized analysis:
No involvement of probability Average performance on a sequence of operations, even some operation is expensive. Guarantee average performance of each operation among the sequence in worst case.
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
In fact, a sequence of n operations on an initially empty stack cost at most O(n). Why?
Each object can be POP only once (including in MULTIPOP) for each time it is PUSHed. #POPs is at most #PUSHs, which is at most n.
Thus the average cost of an operation is O(n)/n = O(1). Amortized cost in aggregate analysis is defined to be average cost.
Another example: increasing a binary counter 1. 2. 3. 4. 5. 6. Binary counter of length k, A[0..k-1] of bit array. INCREMENT(A) i 0 while i<k and A[i]=1 do A[i] 0(flip, reset) i i+1 if i<k then A[i] 1 (flip, set)
Analysis of INCREMENT(A)
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.
A[0] flips every time, total n times. A[1] flips every other time, n/2 times. A[2] flips every forth time, n/4 times. . for i=0,1,,k-1, A[i] flips n/2i times.
The total amortized cost for n PUSH, POP, MULTIPOP is O(n), thus O(1) for average amortized cost for each operation. Conditions hold: total amortized cost total actual cost, and amount of credits never becomes negative.
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) = k'. Amortized cost: ci' = ci + *(Di) - *(Di-1)=k'+(-k')=0.
So amortized cost of each operation is O(1), and total amortized cost of n operations is O(n). Since total amortized cost is an upper bound of actual cost, the worse case cost of n operations is O(n).
The total amortized cost of n operations is O(n). Thus worst case cost is O(n).
Goal:
O(1) amortized cost. Unused space always constant fraction of allocated space.
Dynamic table
Load factor = num/size, where num = # items stored, size = allocated size. If size = 0, then num = 0. Call = 1. Never allow > 1. Keep >a constant fraction goal (2).
Dynamic table: expansion with insertion Table expansion Consider only insertion. When the table becomes full, double its size and reinsert all existing items. Guarantees that 1/2. Each time we actually insert an item into the table, its an elementary insertion.
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 ele. insertion
Aggregate analysis
Running time: Charge 1 per elementary insertion. Count only elementary insertions, since all other costs together are constant per call. ci = actual cost of ith operation
If not full, ci = 1. If full, have i 1 items in the table at the start of the ith operation. Have to copy all i 1 existing items, then insert ith item, ci = i
Cursory analysis: n operations ci = O(n) O(n2) time for n operations. Of course, we dont always expand:
ci = i if i 1 is exact power of 2 , 1 otherwise .
So total cost =i=1n ci n+ i=0log(n) 2i n+2n=3n Therefore, aggregate analysis says amortized cost per operation = 3.
Accounting analysis
Charge $3 per insertion of x.
$1 pays for xs insertion. $1 pays for x to be moved in the future. $1 pays for some other item to be moved.
Suppose weve just expanded, size = m before next expansion, size = 2m after next expansion. Assume that the expansion used up all the credit, so that theres no credit stored after the expansion. Will expand again after another m insertions. Each insertion will put $1 on one of the m items that were in the table just after expansion and will put $1 on the item inserted. Have $2m of credit by next expansion, when there are 2m items to move. Just enough to pay for the expansion, with no credit left over!
Potential method
Potential method *(T ) = 2 num[T ] size[T ] Initially, num = size = 0 * = 0. Just after expansion, size = 2 num * = 0. Just before expansion, size = num * = num have enough potential to pay for moving all items. Need * 0, always. Always have
size num size 2 num size * 0 .
Potential method
Amortized cost of ith operation:
numi = num after ith operation , sizei = size after ith operation , *i = * after ith operation .
If no expansion:
sizei = sizei1 , numi = numi1 +1 , ci = 1 .
Then we have
Ci = ci + *i *i1 = 1 + (2numi sizei ) (2numi1 sizei1) =3.
If expansion:
sizei = 2sizei1 , sizei1 = numi1 = numi 1 , ci = numi1 +1 = numi.
Then we have Ci = ci + *i *i1 = numi + (2numi sizei ) (2numi1 sizei1) = numi + (2numi 2(numi 1)) (2(numi 1) (numi 1)) = numi + 2 (numi 1) = 3
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Still want
bounded from below by a constant, amortized cost per operation = O(1).
Obvious strategy
Double size when inserting into a full table (when = 1, so that after insertion would become <1). Halve size when deletion would make table less than half full (when = 1/2, so that after deletion would become >= 1/2). Then always have 1/2 1. Suppose we fill table.
Then insert double 2 deletes halve 2 inserts double 2 deletes halve Cost of each expansion or contraction is 5(n), so total n operation will be 5(n2).
Problem is that: Not performing enough operations after expansion or contraction to pay for the next one.
Simple solution
Double as before: when inserting with = 1 after doubling, = 1/2. Halve size when deleting with = 1/4 after halving, = 1/2. Thus, immediately after either expansion or contraction, have = 1/2. Always have 1/4 1. Intuition: Want to make sure that we perform enough operations between consecutive expansions/contractions to pay for the change in table size. Need to delete half the items before contraction. Need to double number of items before expansion. Either way, number of operations between expansions/contractions is at least a constant fraction of number of items copied.
Potential function
*(T) = 2num[T] size[T] if size[T]/2 num[T] if < . T empty * = 0. 1/2 num 1/2size 2num size * 0. < 1/2 num < 1/2size * 0.
intuition
measures how far from
= 1/2 we are.
= 1/2 * = 2num2num = 0. = 1 * = 2numnum = num. = 1/4 * = size /2 num = 4num /2 num = num.
Therefore, when we double or halve, have enough potential to pay for moving all num items. Potential increases linearly between = 1/2 and = 1, and it also increases linearly between = 1/2 and = 1/4. Since has different distances to go to get to 1 or 1/4, starting from 1/2, rate of increase differs. For to go from 1/2 to 1, num increases from size /2 to size, for a total increase of size /2. * increases from 0 to size. Thus, * needs to increase by 2 for each item inserted. Thats why theres a coefficient of 2 on the num[T ] term in the formula for when 1/2. For to go from 1/2 to 1/4, num decreases from size /2 to size /4, for a total decrease of size /4. * increases from 0 to size /4. Thus, * needs to increase by 1 for each item deleted. Thats why theres a coefficient of 1 on the num[T ] term in the formula for when < 1/2.
Splay tree
A binary search tree (not balanced) Height may be larger than log n, even n-1. However a sequence of n operations takes O(nlog n). Assumptions: data values are distinct and form a totally order set Operations:
Member(i,S) Insert(i,S) Delete(i,S) Merge(S,S) Split(i,S) All based on
splay(i,S), reorganize tree so that i to be root if iS, otherwise, the new root is either max{k S |k<i} or min{k S |k>i}
Delete(i,S), call Splay(i,S), remove I, then merge(left(i), right(i)). Similar for others. Constant number of splays called.
x is the left (or right) child of y and y is the left (or right) child of z,
rotate(y) and then rotate(x)\
x is the left (or right) child of y and y is the right (or left) child of z,
rotate(x) and then rotate(x)
Lemma:
Each operation splay(x,S) requires no more than 3(Q(S)-Q(x))+1 credits to perform the operation and maintain the credit invariant.
Theorem:
A sequence of m operations involving n inserts takes time O(mlog(n)).
Summary
Amortized analysis
Different from probabilistic analysis