Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Algorithms Amortized Analysis My T. UF.

Similar presentations


Presentation on theme: "Introduction to Algorithms Amortized Analysis My T. UF."— Presentation transcript:

1 Introduction to Algorithms Amortized Analysis My T. Thai @ UF

2 Motivation  Given algorithm A running a sequence of operations op 1, op 2 … op m  Worst case running time of A: the total running time of m operations  Key problem: the running time (cost) of some operations may be high but the average cost per operation is small  Amortized analysis: analyze the average cost of a sequence of operations in the worst case My T. Thai mythai@cise.ufl.edu 2

3 Example  Stack operations:  PUSH(S, x)  POP(S, x)  MULTIPOP(S, k)  What is the running time of n PUSH, POP, MULTIPOP operations? My T. Thai mythai@cise.ufl.edu 3

4  Naive approach:  At most n PUSHs => size of stack is at most n  Running time of MULTIPOP is O(n)  Total running time of n operations is O(n 2 )  Is the analysis tight?  Are the dependences between operations used?  Between PUSHs and POP, MULTIPOP  Between MULTIPOPs My T. Thai mythai@cise.ufl.edu 4

5 Amortized analysis  Concern with the overall cost of a sequence of operations  Use the knowledge of which series of operations are possible  Three most common techniques:  Aggregate analysis  Accounting method  Potential method My T. Thai mythai@cise.ufl.edu 5

6 Aggregate analysis  Show that for all n, a sequence of n operations takes worst-case time T(n) in total  Amortized cost per operation is T(n)/n  E.g. in the sequence of n stack operations:  Each object can be popped only once per time after it is pushed  Have ≤ n PUSHes  ≤ n POPs, including those in MULTIPOP  Total cost: O(n)  O(1) per operation on average My T. Thai mythai@cise.ufl.edu 6

7 Incrementing a binary counter  Given a k-bit binary counter A[0..k-1]  Count upward from 0  A[0] and A[k-1] stores the lowest-order and highest- order bits respectively  INCREMENT(A) increases the value stored in A by 1 My T. Thai mythai@cise.ufl.edu 7

8  Each time the value increases by 1, some bits are flipped  Total cost = total number of bit flips  Is there any pattern in bit flips? My T. Thai mythai@cise.ufl.edu 8

9 Running time of n INCREASEMENTS  Thus the running time: My T. Thai mythai@cise.ufl.edu 9

10 Accounting method  Charge different operations with different amortized costs  If amortized cost ( ) > actual cost ( c i ), store the remained amount on specific objects as credit  If amortized cost < actual cost, credit is used to compensate  Requirement: credit always 0 i.e.  is computed easier My T. Thai mythai@cise.ufl.edu 10

11 Examples  In the sequence of n stack operations:  When an object is pushed, pay 2:  1 is paid for the actual cost of PUSH  1 is stored to spend when the object is POP  Satisfy the requirement Running time: count # of PUSH only, O(n) My T. Thai mythai@cise.ufl.edu 11

12 Incrementing a binary counter  Charge 2 to set a bit to 1 (line 6)  Charge 0 to set a bit to 0 (line 3)  Total amortized cost of n operations: 2n  Running time of n operations: O(n) My T. Thai mythai@cise.ufl.edu 12 Credit equals #1 in the counter, 0

13 Potential method  Idea: consider the credit as potential stored with the entire data structure  Framework:  D 0 = initial data structure  D i = data structure after i th operation  c i = actual cost of i th operation  = amortized cost of i th operation  Find Potential function: s.t.  Set: My T. Thai mythai@cise.ufl.edu 13

14 Bound the actual cost  Due to the definition of potential function, the total actual cost is upper bounded by the total amortized cost My T. Thai mythai@cise.ufl.edu 14

15 The roll of potential function  If > 0, operation i stores credit in the potential function  If < 0, operation i uses credit in the potential function My T. Thai mythai@cise.ufl.edu 15

16 Stack operations  = # of objects in the stack   Total amortized cost: O(n) My T. Thai mythai@cise.ufl.edu 16

17 Incrementing a binary counter   Easy to check  Suppose i th operation resets t i bits to 0  c i ≤ t i + 1 (resets t i bits, sets ≤ 1 bit to 1)  b i ≤ b i−1 − t i + 1 (< when all k bits are set to 0)  Thus: My T. Thai mythai@cise.ufl.edu 17  Total cost: O(n)

18 Summary  An amortized analysis is a strategy for analyzing a sequence of operations to show that the average cost per operation is small, even though a single operation within the sequence might be expensive. My T. Thai mythai@cise.ufl.edu 18

19 Summary (cont)  Aggregate analysis vs Accounting method  In aggregate analysis, all operations have same cost  In the accounting method, different operations can have different costs  Accounting method vs Potential method  Accounting method stores credit with specific objects  Potential method stores potential in the data structure as a whole  Potential method is the most flexible one My T. Thai mythai@cise.ufl.edu 19


Download ppt "Introduction to Algorithms Amortized Analysis My T. UF."

Similar presentations


Ads by Google