Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… ·...
Transcript of Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… ·...
![Page 1: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/1.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.1
Introduction to Algorithms6.046J/18.401J
LECTURE 13Amortized Analysis• Dynamic tables• Aggregate method• Accounting method• Potential method
Prof. Charles E. Leiserson
![Page 2: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/2.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.2
How large should a hash table be?
Goal: Make the table as small as possible, but large enough so that it won’t overflow (or otherwise become inefficient).Problem: What if we don’t know the proper size in advance?
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.
Solution: Dynamic tables.
![Page 3: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/3.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.3
Example of a dynamic table
1. INSERT 1
2. INSERT overflow
![Page 4: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/4.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.4
Example of a dynamic table
1. INSERT 11
2. INSERT overflow
![Page 5: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/5.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.5
Example of a dynamic table
1. INSERT 11
2. INSERT 2
![Page 6: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/6.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.6
Example of a dynamic table
1. INSERT 11
2. INSERT 22
3. INSERT overflow
![Page 7: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/7.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.7
Example of a dynamic table
1. INSERT21
2. INSERT3. INSERT overflow
![Page 8: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/8.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.8
Example of a dynamic table
1. INSERT 1
2. INSERT 2
3. INSERT
![Page 9: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/9.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.9
Example of a dynamic table
1. INSERT2. INSERT3. INSERT4. INSERT 4
321
![Page 10: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/10.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.10
Example of a dynamic table
1. INSERT 1
2. INSERT 233. INSERT
4. INSERT5. INSERT
4
overflow
![Page 11: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/11.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.11
Example of a dynamic table
1. INSERT2. INSERT
4321
3. INSERT4. INSERT5. INSERT overflow
![Page 12: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/12.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.12
Example of a dynamic table
1. INSERT2. INSERT
1233. INSERT
4. INSERT5. INSERT
4
![Page 13: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/13.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.13
Example of a dynamic table
1. INSERT2. INSERT3. INSERT4. INSERT
6. INSERT 65. INSERT 5
4321
77. INSERT
![Page 14: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/14.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.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 ninsertions is n ·Θ(n) = Θ(n2).
WRONG! In fact, the worst-case cost for n insertions is only Θ(n) ≪ Θ(n2).
Let’s see why.
![Page 15: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/15.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson 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 1 2 3 4 5 6 7 8 9 10sizei 1 2 4 4 8 8 8 8 16 16
ci 1 2 3 1 5 1 1 1 9 1
![Page 16: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/16.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.16
Tighter analysis
Let ci = the cost of the i th insertion
= i if i – 1 is an exact power of 2,1 otherwise.
i 1 2 3 4 5 6 7 8 9 10sizei 1 2 4 4 8 8 8 8 16 16
1 1 1 1 1 1 1 1 1 11 2 4 8
ci
![Page 17: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/17.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.17
Tighter analysis (continued)
⎣ ⎦
)(3
2
)1lg(
0
1
nn
n
c
n
j
j
n
ii
Θ=≤
+≤
=
∑
∑−
=
=Cost of n insertions
.
Thus, the average cost of each dynamic-table operation is Θ(n)/n = Θ(1).
![Page 18: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/18.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.18
Amortized analysisAn 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 we’re taking averages, however, probability is not involved!• An amortized analysis guarantees the
average performance of each operation in the worst case.
![Page 19: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/19.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.19
Types of amortized analysesThree common amortization arguments:• the aggregate method,• the accounting method,• the potential method.We’ve 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.
![Page 20: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/20.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.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
∑∑==
≤n
ii
n
ii cc
11ˆ
for all n.• Thus, the total amortized costs provide an upper
bound on the total true costs.
![Page 21: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/21.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.21
Accounting analysis of dynamic tables
$0$0 $0$0 $0$0 $0$0 $2$2 $2$2
Example:$2 $2
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.
overflow
![Page 22: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/22.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.22
Accounting analysis of dynamic tables
Example:
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.
overflow
$0$0 $0$0 $0$0 $0$0 $0$0 $0$0 $0$0 $0$0
![Page 23: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/23.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.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.
Example:
When the table doubles, $1 pays to move a recent item, and $1 pays to move an old item.
$0$0 $0$0 $0$0 $0$0 $0$0 $0$0 $0$0 $0$0 $2 $2 $2
![Page 24: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/24.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.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.
isizei
ciĉi
banki
1 2 3 4 5 6 7 8 9 101 2 4 4 8 8 8 8 16 161 2 3 1 5 1 1 1 9 12 3 3 3 3 3 3 3 3 31 2 2 4 2 4 6 8 2 4*
*Okay, so I lied. The first operation costs only $2, not $3.
![Page 25: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/25.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.25
Potential methodIDEA: 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 Di–1 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) – Φ(Di–1).
![Page 26: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/26.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.26
Understanding potentialsĉi = ci + Φ(Di) – Φ(Di–1)
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.
![Page 27: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/27.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.27
The amortized costs bound the true costs
The total amortized cost of n operations is
( )∑∑=
−=
Φ−Φ+=n
iiii
n
ii DDcc
11
1)()(ˆ
Summing both sides.
![Page 28: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/28.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.28
The amortized costs bound the true costs
The total amortized cost of n operations is
( )
)()(
)()(ˆ
01
11
1
DDc
DDcc
n
n
ii
n
iiii
n
ii
Φ−Φ+=
Φ−Φ+=
∑
∑∑
=
=−
=
The series telescopes.
![Page 29: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/29.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.29
The amortized costs bound the true costs
The total amortized cost of n operations is
( )
∑
∑
∑∑
=
=
=−
=
≥
Φ−Φ+=
Φ−Φ+=
n
ii
n
n
ii
n
iiii
n
ii
c
DDc
DDcc
1
01
11
1
)()(
)()(ˆ
since Φ(Dn) ≥ 0 andΦ(D0 ) = 0.
![Page 30: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/30.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.30
Potential analysis of table doubling
Define the potential of the table after the ithinsertion by Φ(Di) = 2i – 2⎡lg i⎤. (Assume that 2⎡lg 0⎤ = 0.)Note:• Φ(D0 ) = 0,• Φ(Di) ≥ 0 for all i.Example:
•• •• •• •• •• •• Φ = 2·6 – 23 = 4
$0$0 $0$0 $0$0 $0$0 $2$2 $2$2 accounting method)(
![Page 31: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/31.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.31
Calculation of amortized costs
The amortized cost of the i th insertion isĉi = ci + Φ(Di) – Φ(Di–1)
![Page 32: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/32.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.32
Calculation of amortized costs
The amortized cost of the i th insertion isĉi = ci + Φ(Di) – Φ(Di–1)
i if i – 1 is an exact power of 2,1 otherwise;
+ (2i – 2⎡lg i⎤) – (2(i –1) – 2⎡lg (i–1)⎤)
=
![Page 33: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/33.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.33
Calculation of amortized costs
The amortized cost of the i th insertion isĉi = ci + Φ(Di) – Φ(Di–1)
i if i – 1 is an exact power of 2,1 otherwise;=
+ (2i – 2⎡lg i⎤) – (2(i –1) – 2⎡lg (i–1)⎤)i if i – 1 is an exact power of 2,1 otherwise;=
+ 2 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤ .
![Page 34: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/34.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.34
CalculationCase 1: i – 1 is an exact power of 2.
ĉi = i + 2 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤
![Page 35: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/35.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.35
CalculationCase 1: i – 1 is an exact power of 2.
ĉi = i + 2 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤
= i + 2 – 2(i – 1) + (i – 1)
![Page 36: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/36.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.36
CalculationCase 1: i – 1 is an exact power of 2.
ĉi = i + 2 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤
= i + 2 – 2(i – 1) + (i – 1)= i + 2 – 2i + 2 + i – 1
![Page 37: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/37.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.37
CalculationCase 1: i – 1 is an exact power of 2.
ĉi = i + 2 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤
= i + 2 – 2(i – 1) + (i – 1)= i + 2 – 2i + 2 + i – 1= 3
![Page 38: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/38.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.38
CalculationCase 1: i – 1 is an exact power of 2.
ĉi = i + 2 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤
= 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 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤
![Page 39: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/39.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.39
CalculationCase 1: i – 1 is an exact power of 2.
ĉi = i + 2 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤
= 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 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤
= 3 (since 2⎡lg i⎤ = 2⎡lg (i–1)⎤ )
![Page 40: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/40.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.40
CalculationCase 1: i – 1 is an exact power of 2.
ĉi = i + 2 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤
= 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 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤
= 3Therefore, n insertions cost Θ(n) in the worst case.
![Page 41: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/41.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.41
CalculationCase 1: i – 1 is an exact power of 2.
ĉi = i + 2 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤
= 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 – 2⎡lg i⎤ + 2⎡lg (i–1)⎤
= 3Therefore, 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.
![Page 42: Introduction to Algorithmsmy.fit.edu/~pbernhar/Teaching/AnalysisOfAlgorithms/Leiserson Slide… · October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson](https://reader035.fdocuments.net/reader035/viewer/2022062921/5f049a4e7e708231d40ec932/html5/thumbnails/42.jpg)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.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.