Download presentation

Presentation is loading. Please wait.

Published byRiley Simpson Modified over 4 years ago

1
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.1 Introduction to Algorithms LECTURE 11 Amortized Analysis Dynamic tables Aggregate method Accounting method Potential method

2
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.2 How large should a hash table be? Goal: Make the table as small as possible, but large enough so that it wont overflow (or otherwise become inefficient). Problem: What if we dont know the proper size in advance? Solution:Dynamic tables. IDEA: Whenever the table overflows, grow it by allocating (via malloc or new) a new, larger table. Move all items from the old table into the new one, and free the storage for the old table.

3
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.3 Example of a dynamic table 1. INSERT 2. INSERT overflow

4
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.4 Example of a dynamic table 1. INSERT 2. INSERT overflow

5
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.5 Example of a dynamic table 1. INSERT 2. INSERT

6
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.6 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT overflow

7
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.7 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT overflow

8
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.8 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT

9
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.9 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 4. INSERT

10
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.10 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 4. INSERT 5. INSERT overflow

11
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.11 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 4. INSERT 5. INSERT overflow

12
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.12 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 4. INSERT 5. INSERT

13
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.13 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 4. INSERT 5. INSERT 6. INSERT 7. INSERT

14
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.14 Worst-case analysis Consider a sequence of n insertions. The worst-case time to execute one insertion is Θ(n). Therefore, the worst-case time for n insertions is n·Θ(n) = Θ(n2). WRONG! In fact, the worst-case cost for n insertions is only Θ(n) Θ(n2). Lets see why.

15
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.15 Tighter analysis Let c i = the cost of the i th insertion if i –1 is an exact power of 2, otherwise.

16
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.16 Tighter analysis Let c i = the cost of the i th insertion if i –1 is an exact power of 2, otherwise.

17
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.17 Tighter analysis (continued) Cost of n insertions. Thus, the average cost of each dynamic-table operation is Θ(n)/n= Θ(1).

18
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.18 Amortized analysis An amortized analysis is any 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. Even though were taking averages, however, probability is not involved! An amortized analysis guarantees the average performance of each operation in the worst case.

19
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.19 Types of amortized analyses Three common amortization arguments: the aggregate method, the accounting method, the potential method. Weve just seen an aggregate analysis. The aggregate method, though simple, lacks the precision of the other two methods. In particular, the accounting and potential methods allow a specific amortized cost to be allocated to each operation.

20
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.20 Accounting method Charge i th operation a fictitious amortized cost ĉ i, where $1 pays for 1 unit of work (i.e., time). This fee is consumed to perform the operation. Any amount not immediately consumed is stored in the bank for use by subsequent operations. The bank balance must not go negative! We must ensure that for all n. Thus, the total amortized costs provide an upper bound on the total true costs.

21
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.21 Accounting analysis of dynamic tables Charge an amortized cost of ĉ i = $3 for the i th insertion. $1 pays for the immediate insertion. $2 is stored for later table doubling. When the table doubles, $1 pays to move a recent item, and $1 pays to move an old item. Example: overflow

22
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.22 Accounting analysis of dynamic tables Charge an amortized cost of ĉ i = $3 for the i th insertion. $1 pays for the immediate insertion. $2 is stored for later table doubling. When the table doubles, $1 pays to move a recent item, and $1 pays to move an old item. Example: overflow

23
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.23 Accounting analysis of dynamic tables Charge an amortized cost of ĉ i = $3 for the i th insertion. $1 pays for the immediate insertion. $2 is stored for later table doubling. When the table doubles, $1 pays to move a recent item, and $1 pays to move an old item. Example:

24
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.24 Accounting analysis (continued) Key invariant: Bank balance never drops below 0. Thus, the sum of the amortized costs provides an upper bound on the sum of the true costs. *Okay, so I lied. The first operation costs only $2, not $3.

25
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.25 Potential method IDEA: View the bank account as the potential energy (àlaphysics) of the dynamic set. Framework: Start with an initial data structure D 0. Operation itransforms D i–1 to D i. The cost of operation i is c i. Define a potential function Φ: {Di} R, such that Φ(D 0 ) = 0 and Φ(D i ) 0 for all i. The amortized cost ĉ i with respect to Φ is defined to be ĉ i = c i + Φ(D i ) –Φ(D i–1 ).

26
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.26 Understanding potentials potential difference ΔΦ i If ΔΦi> 0, then ĉi> ci. Operation i stores work in the data structure for later use. If ΔΦi< 0, then ĉi< ci. The data structure delivers up stored work to help pay for operation i.

27
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.27 The amortized costs bound the true costs The total amortized cost of n operations is Summing both sides.

28
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.28 The amortized costs bound the true costs The total amortized cost of n operations is The series telescopes.

29
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.29 The amortized costs bound the true costs The total amortized cost of n operations is

30
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.30 Potential analysis of table doubling Define the potential of the table after the ith insertion by (Assume that Note: Example: accounting method )

31
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.31 Calculation of amortized costs The amortized cost of the i th insertion is

32
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.32 Calculation of amortized costs The amortized cost of the i th insertion is if i –1is an exact power of 2, otherwise;

33
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.33 Calculation of amortized costs The amortized cost of the i th insertion is if i –1is an exact power of 2, otherwise; if i –1is an exact power of 2, otherwise;

34
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.34 Calculation Case 1: i –1 is an exact power of 2.

35
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.35 Calculation Case 1: i –1 is an exact power of 2.

36
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.36 Calculation Case 1: i –1 is an exact power of 2.

37
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.37 Calculation Case 1: i –1 is an exact power of 2.

38
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.38 Calculation Case 1: i –1 is an exact power of 2. Case 2: i –1 is not an exact power of 2.

39
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.39 Calculation Case 1: i –1 is an exact power of 2. Case 2: i –1 is not an exact power of 2.

40
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.40 Calculation Case 1: i –1 is an exact power of 2. Case 2: i –1 is not an exact power of 2. Therefore, n insertions cost Θ(n) in the worst case.

41
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.41 Calculation Case 1: i –1 is an exact power of 2. Case 2: i –1 is not an exact power of 2. Therefore, n insertions cost Θ(n) in the worst case. Exercise: Fix the bug in this analysis to show that the amortized cost of the first insertion is only 2.

42
October 31, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL13.42 Conclusions Amortized costs can provide a clean abstraction of data-structure performance. Any of the analysis methods can be used when an amortized analysis is called for, but each method has some situations where it is arguably the simplest or most precise. Different schemes may work for assigning amortized costs in the accounting method, or potentials in the potential method, sometimes yielding radically different bounds.

Similar presentations

OK

1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.

1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on timing diagram of 8085 microprocessor Simple ppt on global warming Free ppt on etiquettes Ppt on indian textile industries in pakistan Ppt on indian herbs and spices Ppt on wireless networking technology Ppt on wireless power transmission download Ppt on water pollution problems and solutions Ppt on endangered species tiger Ppt on online library management system