SlideShare una empresa de Scribd logo
1 de 42
Descargar para leer sin conexión
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.1
Introduction to Algorithms
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.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.
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
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.4
Example of a dynamic table
1. INSERT 1
1
2. INSERT overflow
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.5
Example of a dynamic table
1. INSERT 1
1
2. INSERT 2
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.6
Example of a dynamic table
1. INSERT 1
1
2. INSERT 2
2
3. INSERT overflow
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.7
Example of a dynamic table
1. INSERT
2
1
2. INSERT
3. INSERT overflow
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
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.9
Example of a dynamic table
1. INSERT
2. INSERT
3. INSERT
4. INSERT 4
3
2
1
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 2
3
3. INSERT
4. INSERT
5. INSERT
4
overflow
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.11
Example of a dynamic table
1. INSERT
2. INSERT
4
3
2
1
3. INSERT
4. INSERT
5. INSERT overflow
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.12
Example of a dynamic table
1. INSERT
2. INSERT
1
2
3
3. INSERT
4. INSERT
5. INSERT
4
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.13
Example of a dynamic table
1. INSERT
2. INSERT
3. INSERT
4. INSERT
6. INSERT 6
5. INSERT 5
4
3
2
1
7
7. INSERT
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
4(n). Therefore, the worst-case time for n
insertions is n · 4(n) = 4(n2).
WRONG! In fact, the worst-case cost for
n insertions is only 4(n) ҋ 4(n2).
Let’’s see why.
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.15
Tighter analysis
Let ci = the cost of the ith insertion
=
i if i –– 1 is an exact power of 2,
1 otherwise.
i 1 2 3 4 5 6 7 8 9 10
sizei 1 2 4 4 8 8 8 8 16 16
ci 1 2 3 1 5 1 1 1 9 1
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.16
Tighter analysis
Let ci = the cost of the ith insertion
=
i if i –– 1 is an exact power of 2,
1 otherwise.
i 1 2 3 4 5 6 7 8 9 10
sizei 1 2 4 4 8 8 8 8 16 16
1 1 1 1 1 1 1 1 1 1
1 2 4 8
ci
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.17
Tighter analysis (continued)
¬ ¼
)
(
3
2
)
1
lg(
0
1
n
n
n
c
n
j
j
n
i
i
4
d

d ¦
¦

Cost of n insertions
.
Thus, the average cost of each dynamic-table
operation is 4(n)/n = 4(1).
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.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 we’’re 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.19
Types of amortized analyses
Three 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.
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.20
Accounting method
•• Charge ith 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
¦
¦ d
n
i
i
n
i
i c
c
1
1
ˆˆ
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.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 ith
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
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 ith
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
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 ith
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
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.
i
sizei
ci
ƙi
banki
1 2 3 4 5 6 7 8 9 10
1 2 4 4 8 8 8 8 16 16
1 2 3 1 5 1 1 1 9 1
2 3 3 3 3 3 3 3 3 3
1 2 2 4 2 4 6 8 2 4
*
*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 L13.25
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 Di––1 to Di.
•• The cost of operation i is ci.
•• Define a potential function ) : {Di} o R,
such that )(D0 ) = 0 and )(Di ) t 0 for all i.
•• The amortized cost ƙi with respect to ) is
defined to be ƙi = ci + )(Di) –– )(Di––1).
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.
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
i
i
i
i
n
i
i D
D
c
c
1
1
1
)
(
)
(
ˆˆ
Summing both sides.
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
)
(
)
(
)
(
)
(
ˆˆ
0
1
1
1
1
D
D
c
D
D
c
c
n
n
i
i
n
i
i
i
i
n
i
i
)

)

)

)

¦
¦
¦ 
The series telescopes.
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
¦
¦
¦
¦ 
t
)

)

)

)

n
i
i
n
n
i
i
n
i
i
i
i
n
i
i
c
D
D
c
D
D
c
c
1
0
1
1
1
1
)
(
)
(
)
(
)
(
ˆˆ
since )(Dn) t 0 and
)(D0 ) = 0.
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 ith
insertion by )(Di) = 2i –– 2ªlg iº. (Assume that
2ªlg 0º = 0.)
Note:
•• )(D0 ) = 0,
•• )(Di) t 0 for all i.
Example:
••
•• ••
•• ••
•• ••
•• ••
•• ••
•• ) = 2·6 –– 23 = 4
$0
$0 $0
$0 $0
$0 $0
$0 $2
$2 $2
$2 accounting method)
(
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 ith insertion is
ƙi = ci + )(Di) –– )(Di––1)
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 ith 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)º)
=
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 ith 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)º.
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.34
Calculation
Case 1: i –– 1 is an exact power of 2.
ƙi = i + 2 –– 2ªlg iº + 2ªlg (i––1)º
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.35
Calculation
Case 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)
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.36
Calculation
Case 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
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.37
Calculation
Case 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
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.38
Calculation
Case 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)º
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.39
Calculation
Case 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)º )
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.40
Calculation
Case 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
Therefore, n insertions cost 4(n) in the worst case.
October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.41
Calculation
Case 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
Therefore, n insertions cost 4(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.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.

Más contenido relacionado

Similar a AmortizedAnalysis

5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-iKrish_ver2
 
Algo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.pptAlgo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.pptHebaSamy22
 
A Reflection of Euler’s Constant and Its Applications
A Reflection of Euler’s Constant and Its ApplicationsA Reflection of Euler’s Constant and Its Applications
A Reflection of Euler’s Constant and Its Applicationsdrboon
 
Review session2
Review session2Review session2
Review session2NEEDY12345
 
Pre-Calculus Midterm ExamScore ______ ______Name _______.docx
Pre-Calculus Midterm ExamScore ______  ______Name _______.docxPre-Calculus Midterm ExamScore ______  ______Name _______.docx
Pre-Calculus Midterm ExamScore ______ ______Name _______.docxChantellPantoja184
 
Lecture1-Architecture
Lecture1-ArchitectureLecture1-Architecture
Lecture1-ArchitectureDanny Albay
 
Pavement Stress Analysis Software University of Florida.docx
Pavement Stress Analysis Software University of Florida.docxPavement Stress Analysis Software University of Florida.docx
Pavement Stress Analysis Software University of Florida.docxdanhaley45372
 
mit 演算法 Lec2
mit 演算法 Lec2 mit 演算法 Lec2
mit 演算法 Lec2 Henry Chang
 
L12-FloatingPoint.ppt
L12-FloatingPoint.pptL12-FloatingPoint.ppt
L12-FloatingPoint.pptDevcond
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree methodRajendran
 
Python tutorialfeb152012
Python tutorialfeb152012Python tutorialfeb152012
Python tutorialfeb152012Shani729
 

Similar a AmortizedAnalysis (20)

5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-i
 
Lec3 Algott
Lec3 AlgottLec3 Algott
Lec3 Algott
 
Digital logic design part1
Digital logic design part1Digital logic design part1
Digital logic design part1
 
fundamentals.ppt
fundamentals.pptfundamentals.ppt
fundamentals.ppt
 
fundamentals.ppt
fundamentals.pptfundamentals.ppt
fundamentals.ppt
 
Algo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.pptAlgo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.ppt
 
A Reflection of Euler’s Constant and Its Applications
A Reflection of Euler’s Constant and Its ApplicationsA Reflection of Euler’s Constant and Its Applications
A Reflection of Euler’s Constant and Its Applications
 
Review session2
Review session2Review session2
Review session2
 
Pre-Calculus Midterm ExamScore ______ ______Name _______.docx
Pre-Calculus Midterm ExamScore ______  ______Name _______.docxPre-Calculus Midterm ExamScore ______  ______Name _______.docx
Pre-Calculus Midterm ExamScore ______ ______Name _______.docx
 
Lecture1-Architecture
Lecture1-ArchitectureLecture1-Architecture
Lecture1-Architecture
 
Pavement Stress Analysis Software University of Florida.docx
Pavement Stress Analysis Software University of Florida.docxPavement Stress Analysis Software University of Florida.docx
Pavement Stress Analysis Software University of Florida.docx
 
Analysis of a building with XC
Analysis of a building with XCAnalysis of a building with XC
Analysis of a building with XC
 
Rand final
Rand finalRand final
Rand final
 
1
11
1
 
mit 演算法 Lec2
mit 演算法 Lec2 mit 演算法 Lec2
mit 演算法 Lec2
 
L12-FloatingPoint.ppt
L12-FloatingPoint.pptL12-FloatingPoint.ppt
L12-FloatingPoint.ppt
 
Ch01
Ch01Ch01
Ch01
 
Ch01
Ch01Ch01
Ch01
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Python tutorialfeb152012
Python tutorialfeb152012Python tutorialfeb152012
Python tutorialfeb152012
 

Último

Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...amitlee9823
 
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...gajnagarg
 
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...amitlee9823
 
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...karishmasinghjnh
 
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...gajnagarg
 
Detecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachDetecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachBoston Institute of Analytics
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...amitlee9823
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...SUHANI PANDEY
 
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men 🔝Ongole🔝 Escorts S...
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men  🔝Ongole🔝   Escorts S...➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men  🔝Ongole🔝   Escorts S...
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men 🔝Ongole🔝 Escorts S...amitlee9823
 
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...amitlee9823
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...amitlee9823
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Researchmichael115558
 

Último (20)

Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
 
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
 
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
 
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
 
Detecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachDetecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning Approach
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men 🔝Ongole🔝 Escorts S...
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men  🔝Ongole🔝   Escorts S...➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men  🔝Ongole🔝   Escorts S...
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men 🔝Ongole🔝 Escorts S...
 
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 

AmortizedAnalysis

  • 1. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.1 Introduction to Algorithms 6.046J/18.401J LECTURE 13 Amortized Analysis •• Dynamic tables •• Aggregate method •• Accounting method •• Potential method Prof. Charles E. Leiserson
  • 2. 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.
  • 3. 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
  • 4. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.4 Example of a dynamic table 1. INSERT 1 1 2. INSERT overflow
  • 5. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.5 Example of a dynamic table 1. INSERT 1 1 2. INSERT 2
  • 6. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.6 Example of a dynamic table 1. INSERT 1 1 2. INSERT 2 2 3. INSERT overflow
  • 7. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.7 Example of a dynamic table 1. INSERT 2 1 2. INSERT 3. INSERT overflow
  • 8. 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
  • 9. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.9 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 4. INSERT 4 3 2 1
  • 10. 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 2 3 3. INSERT 4. INSERT 5. INSERT 4 overflow
  • 11. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.11 Example of a dynamic table 1. INSERT 2. INSERT 4 3 2 1 3. INSERT 4. INSERT 5. INSERT overflow
  • 12. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.12 Example of a dynamic table 1. INSERT 2. INSERT 1 2 3 3. INSERT 4. INSERT 5. INSERT 4
  • 13. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.13 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 4. INSERT 6. INSERT 6 5. INSERT 5 4 3 2 1 7 7. INSERT
  • 14. 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 4(n). Therefore, the worst-case time for n insertions is n · 4(n) = 4(n2). WRONG! In fact, the worst-case cost for n insertions is only 4(n) ҋ 4(n2). Let’’s see why.
  • 15. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.15 Tighter analysis Let ci = the cost of the ith insertion = i if i –– 1 is an exact power of 2, 1 otherwise. i 1 2 3 4 5 6 7 8 9 10 sizei 1 2 4 4 8 8 8 8 16 16 ci 1 2 3 1 5 1 1 1 9 1
  • 16. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.16 Tighter analysis Let ci = the cost of the ith insertion = i if i –– 1 is an exact power of 2, 1 otherwise. i 1 2 3 4 5 6 7 8 9 10 sizei 1 2 4 4 8 8 8 8 16 16 1 1 1 1 1 1 1 1 1 1 1 2 4 8 ci
  • 17. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.17 Tighter analysis (continued) ¬ ¼ ) ( 3 2 ) 1 lg( 0 1 n n n c n j j n i i 4 d d ¦ ¦ Cost of n insertions . Thus, the average cost of each dynamic-table operation is 4(n)/n = 4(1).
  • 18. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.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 we’’re taking averages, however, probability is not involved! •• An amortized analysis guarantees the average performance of each operation in the worst case.
  • 19. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.19 Types of amortized analyses Three 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.
  • 20. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.20 Accounting method •• Charge ith 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 ¦ ¦ d n i i n i i c c 1 1 ˆˆ for all n. •• Thus, the total amortized costs provide an upper bound on the total true costs.
  • 21. 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 ith 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
  • 22. 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 ith 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
  • 23. 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 ith 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
  • 24. 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. i sizei ci ƙi banki 1 2 3 4 5 6 7 8 9 10 1 2 4 4 8 8 8 8 16 16 1 2 3 1 5 1 1 1 9 1 2 3 3 3 3 3 3 3 3 3 1 2 2 4 2 4 6 8 2 4 * *Okay, so I lied. The first operation costs only $2, not $3.
  • 25. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.25 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 Di––1 to Di. •• The cost of operation i is ci. •• Define a potential function ) : {Di} o R, such that )(D0 ) = 0 and )(Di ) t 0 for all i. •• The amortized cost ƙi with respect to ) is defined to be ƙi = ci + )(Di) –– )(Di––1).
  • 26. 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.
  • 27. 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 i i i i n i i D D c c 1 1 1 ) ( ) ( ˆˆ Summing both sides.
  • 28. 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 ) ( ) ( ) ( ) ( ˆˆ 0 1 1 1 1 D D c D D c c n n i i n i i i i n i i ) ) ) ) ¦ ¦ ¦ The series telescopes.
  • 29. 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 ¦ ¦ ¦ ¦ t ) ) ) ) n i i n n i i n i i i i n i i c D D c D D c c 1 0 1 1 1 1 ) ( ) ( ) ( ) ( ˆˆ since )(Dn) t 0 and )(D0 ) = 0.
  • 30. 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 ith insertion by )(Di) = 2i –– 2ªlg iº. (Assume that 2ªlg 0º = 0.) Note: •• )(D0 ) = 0, •• )(Di) t 0 for all i. Example: •• •• •• •• •• •• •• •• •• •• •• •• ) = 2·6 –– 23 = 4 $0 $0 $0 $0 $0 $0 $0 $0 $2 $2 $2 $2 accounting method) (
  • 31. 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 ith insertion is ƙi = ci + )(Di) –– )(Di––1)
  • 32. 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 ith 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)º) =
  • 33. 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 ith 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)º.
  • 34. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.34 Calculation Case 1: i –– 1 is an exact power of 2. ƙi = i + 2 –– 2ªlg iº + 2ªlg (i––1)º
  • 35. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.35 Calculation Case 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)
  • 36. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.36 Calculation Case 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
  • 37. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.37 Calculation Case 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
  • 38. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.38 Calculation Case 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)º
  • 39. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.39 Calculation Case 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)º )
  • 40. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.40 Calculation Case 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 Therefore, n insertions cost 4(n) in the worst case.
  • 41. October 31, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L13.41 Calculation Case 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 Therefore, n insertions cost 4(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, 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.