Unformatted text preview:

Amortized Analysis (chap. 17)Three Methods of Amortized AnalysisExample for amortized analysisAggregate AnalysisAnother example: increasing a binary counterAnalysis of INCREMENT(A)Amortized (Aggregate) Analysis of INCREMENT(A)Amortized Analysis of INCREMENT(A)Amortized Analysis: Accounting MethodAccounting Method (cont.)Accounting Method: Stack OperationsAccounting method: binary counterThe Potential MethodThe Potential Method (cont.)Slide 15Potential method: stack operationPotential method: binary counterAmortized analyses: dynamic tableDynamic tableDynamic table: expansion with insertionSlide 21Aggregate analysisAccounting analysisPotential methodSlide 25Slide 26Expansion and contractionObvious strategySimple solutionPotential functionintuitionAmortized cost for each operationSplay treeSplay tree (cont.)Slide 35Slide 36SummaryAmortized Analysis (chap. 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.Three Methods of Amortized Analysis•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.Example for amortized analysis•Stack operations:–PUSH(S,x), O(1)–POP(S), O(1)–MULTIPOP(S,k), min(s,k)•while not STACK-EMPTY(S) and k>0• 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.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•Binary counter of length k, A[0..k-1] of bit array.•INCREMENT(A)1. i02. while i<k and A[i]=13. do A[i]0 (flip, reset)4. ii+15. if i<k6. 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.•The tight bound is O(n) for n executions.Amortized (Aggregate) Analysis of INCREMENT(A)Observation: The running time determined by #flips but not all bits flip each time INCREMENT is called.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.Thus total #flips is i=0k-1 n/2i < ni=0 1/2i =2n.Amortized Analysis of INCREMENT(A)•Thus the worst case running time is O(n) for a sequence of n INCREMENTs.•So the amortized cost per operation is O(1).Amortized Analysis: Accounting Method•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.)•Conditions: –suppose actual cost is ci for the ith operation in the sequence, and amortized cost is ci', – i=1n ci' i=1n ci should hold.•since we want to show the average cost per operation is small using amortized cost, we need the total amortized cost is an upper bound of total actual cost.•holds for all sequences of operations.–Total credits is i=1n ci' - i=1n ci , which should be nonnegative, •Moreover, i=1t ci' - i=1t ci ≥0 for any t>0.Accounting Method: Stack Operations•Actual costs:–PUSH :1, POP :1, MULTIPOP: min(s,k).•Let assign the following amortized costs:–PUSH:2, POP: 0, MULTIPOP: 0.•Similar to a stack of plates in a cafeteria.–Suppose $1 represents a unit cost.–When pushing a plate, use one dollar to pay the actual cost of the push and leave one dollar on the plate as credit.–Whenever POPing a plate, the one dollar on the plate is used to pay the actual cost of the POP. (same for MULTIPOP).–By charging PUSH a little more, do not charge POP or MULTIPOP.•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.Accounting method: binary counter•Let $1 represent each unit of cost (i.e., the flip of one bit).•Charge an amortized cost of $2 to set a bit to 1.•Whenever a bit is set, use $1 to pay the actual cost, and store another $1 on the bit as credit.•When a bit is reset, the stored $1 pays the cost.•At any point, a 1 in the counter stores $1, the number of 1’s is never negative, so is the total credits.•At most one bit is set in each operation, so the amortized cost of an operation is at most $2.•Thus, total amortized cost of n operations is O(n), and average is O(1).The Potential Method•Same as accounting method: something prepaid is used later.•Different from accounting method–The prepaid work not as credit, but as “potential energy”, or “potential”.–The potential is associated with the data structure as a whole rather than with specific objects within the data structure.The Potential Method (cont.)•Initial data structure D0, •n operations, resulting in D0, D1,…, Dn with costs c1, c2,…, cn. •A


View Full Document

IUPUI CS 580 - Amortized Analysis

Download Amortized Analysis
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Amortized Analysis and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Amortized Analysis 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?