Professional Documents
Culture Documents
Lec13 PDF
Lec13 PDF
Lec13 PDF
6.046J/18.401J LECTURE 13
Amortized Analysis Dynamic tables Aggregate method Accounting method Potential method Prof. Charles E. Leiserson
October 31, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.1
L13.3
L13.4
L13.5
L13.6
L13.7
L13.8
L13.9
L13.10
L13.11
L13.12
L13.13
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.
L13.14
Tighter analysis
Let ci = the cost of the i th insertion i if i 1 is an exact power of 2, = 1 otherwise.
i sizei ci 1 1 1 2 2 2 3 4 3 4 4 1 5 8 5 6 8 1 7 8 1 8 8 1 9 9 10 1
16 16
L13.15
Tighter analysis
Let ci = the cost of the i th insertion i if i 1 is an exact power of 2, = 1 otherwise.
i sizei ci 1 1 1 2 2 1 1 3 4 1 2 4 4 1 5 8 1 4 6 8 1 7 8 1 8 8 1 9 1 8 10 1
16 16
L13.16
n+
lg( n 1)
2j
j =0
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.
October 31, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.18
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 n n i ci c
for all n. Thus, the total amortized costs provide an upper bound on the total true costs.
October 31, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.20
i =1
i =1
L13.21
$0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $2 $2 $2 $0 $0
October 31, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.23
16 16
2* 3
*Okay, so I lied. The first operation costs only $2, not $3.
October 31, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson
Potential method
IDEA: View the bank account as the potential energy ( la physics) of the dynamic set. Framework: Start with an initial data structure D0. Operation i transforms Di1 to Di. The cost of operation i is ci. Define a potential function : {Di} R, such that (D0 ) = 0 and (Di ) 0 for all i. The amortized cost i with respect to is defined to be i = ci + (Di) (Di1).
October 31, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.25
Understanding potentials
i = ci + (Di) (Di1)
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.
October 31, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.26
L13.27
L13.28
i = (ci + ( Di ) ( Di 1 ) ) c
i =1
= ci + ( Dn ) ( D0 ) ci
i =1 i =1 n
i =1 n
$0 $0 $0 $0 $0 $0 $2 $2 $2 $2 $0 $0
L13.31
L13.32
Calculation
Case 1: i 1 is an exact power of 2. i = i + 2 2lg i + 2lg (i1)
L13.34
Calculation
Case 1: i 1 is an exact power of 2. i = i + 2 2lg i + 2lg (i1) = i + 2 2(i 1) + (i 1)
L13.35
Calculation
Case 1: i 1 is an exact power of 2. i = i + 2 2lg i + 2lg (i1) = i + 2 2(i 1) + (i 1) = i + 2 2i + 2 + i 1
L13.36
Calculation
Case 1: i 1 is an exact power of 2. i = i + 2 2lg i + 2lg (i1) = i + 2 2(i 1) + (i 1) = i + 2 2i + 2 + i 1 =3
L13.37
Calculation
Case 1: i 1 is an exact power of 2. i = i + 2 2lg i + 2lg (i1) = i + 2 2(i 1) + (i 1) = i + 2 2i + 2 + i 1 =3 Case 2: i 1 is not an exact power of 2. i = 1 + 2 2lg i + 2lg (i1)
L13.38
Calculation
Case 1: i 1 is an exact power of 2. i = i + 2 2lg i + 2lg (i1) = i + 2 2(i 1) + (i 1) = i + 2 2i + 2 + i 1 =3 Case 2: i 1 is not an exact power of 2. i = 1 + 2 2lg i + 2lg (i1) =3 (since 2lg i = 2lg (i1) )
L13.39
Calculation
Case 1: i 1 is an exact power of 2. i = i + 2 2lg i + 2lg (i1) = i + 2 2(i 1) + (i 1) = i + 2 2i + 2 + i 1 =3 Case 2: i 1 is not an exact power of 2. i = 1 + 2 2lg i + 2lg (i1) =3 Therefore, n insertions cost (n) in the worst case.
L13.40
Calculation
Case 1: i 1 is an exact power of 2. i = i + 2 2lg i + 2lg (i1) = i + 2 2(i 1) + (i 1) = i + 2 2i + 2 + i 1 =3 Case 2: i 1 is not an exact power of 2. i = 1 + 2 2lg i + 2lg (i1) =3 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.
October 31, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.41
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.
October 31, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.42