SlideShare a Scribd company logo
Amortized Algorithms,
Table Doubling, Potential
Method
Lecture 19
L19.2
How large should a hash
table be?
Problem: What if we don’t know the proper size
in advance?
Goal: Make the table as small as possible, but
large enough so that it won’t overflow (or
otherwise become inefficient).
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.
L19.3
Example of a dynamic table
1. INSERT 1
2. INSERT overflow
L19.4
1
Example of a dynamic table
1. INSERT
2. INSERT overflow
L19.5
1
2
Example of a dynamic table
1. INSERT
2. INSERT
L19.6
Example of a dynamic table
1. INSERT
2. INSERT
1
2
3. INSERT overflow
L19.7
Example of a dynamic table
1. INSERT
2. INSERT
3. INSERT
2
1
overflow
L19.8
Example of a dynamic table
1. INSERT
2. INSERT
3. INSERT
2
1
L19.9
Example of a dynamic table
1. INSERT
2. INSERT
3. INSERT
4. INSERT 4
3
2
1
L19.10
Example of a dynamic table
1. INSERT
2. INSERT
3. INSERT
4. INSERT
5. INSERT
4
3
2
1
overflow
L19.11
Example of a dynamic table
1. INSERT
2. INSERT
3. INSERT
4. INSERT
5. INSERT
4
3
2
1
overflow
L19.12
Example of a dynamic table
1. INSERT
2. INSERT
3. INSERT
4. INSERT
5. INSERT
4
3
2
1
L19.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
L19.14
Worst-case analysis
Consider a sequence of n insertions. The
worst-case time to execute one insertion is
Q(n). Therefore, the worst-case time for n
insertions is n · Q(n) = Q(n2).
WRONG! In fact, the worst-case cost for
n insertions is only Q(n) ≪ Q(n2).
Let’s see why.
L19.15
Tighter analysis
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
Let ci = the cost of the ith insertion
=
i if i – 1 is an exact power of 2,
1 otherwise.
L19.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
L19.17
Tighter analysis (continued)
 
)
(
3
2
)
1
lg(
0
1
n
n
n
c
n
j
j
n
i
i
Q










Cost of n insertions
.
Thus, the average cost of each dynamic-table
operation is Q(n)/n = Q(1).
L19.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.
L19.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.
L19.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





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.
L19.21
$0 $0 $0 $0 $2 $2
Example:
$2 $2
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.
When the table doubles, $1 pays to move a
recent item, and $1 pays to move an old item.
overflow
L19.22
Example:
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.
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
L19.23
Example:
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.
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 $2 $2 $2
L19.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 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
ĉi 2 3 3 3 3 3 3 3 3 3
banki 1 2 2 4 2 4 6 8 2 4
*
*Okay, so I lied. The first operation costs only $2, not $3.
L19.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 F : {Di}  R,
such that F(D0 ) = 0 and F(Di )  0 for all i.
• The amortized cost ĉi with respect to F is
defined to be ĉi = ci + F(Di) – F(Di–1).
L19.26
Understanding potentials
ĉi = ci + F(Di) – F(Di–1)
potential difference DFi
• If DFi > 0, then ĉi > ci. Operation i stores
work in the data structure for later use.
• If DFi < 0, then ĉi < ci. The data structure
delivers up stored work to help pay for
operation i.
L19.27
The amortized costs bound
the true costs
The total amortized cost of n operations is
 





F

F


n
i
i
i
i
n
i
i D
D
c
c
1
1
1
)
(
)
(
ˆ
Summing both sides.
L19.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
F

F


F

F









The series telescopes.
L19.29
The amortized costs bound
the true costs
The total amortized cost of n operations is
 










F

F


F

F


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 F(Dn)  0 and
F(D0 ) = 0.
L19.30
Potential analysis of table
doubling
Define the potential of the table after the ith
insertion by F(Di) = 2i – 2lg i. (Assume that
2lg 0 = 0.)
Note:
• F(D0 ) = 0,
• F(Dn)  0 for all i.
Example:
• • • • • • F = 2·6 + 23 = 4
$0 $0 $0 $0 $2 $2 accounting method)
(
L19.31
Calculation of amortized costs
The amortized cost of the ith insertion is
ĉi = ci + F(Di) – F(Di–1)
i + (2i – 2lg i) – (2(i–1) – 2lg (i–1))
if i – 1 is an exact power of 2,
1 + (2i – 2lg i) – (2(i–1) – 2lg (i–1))
otherwise.
=
L19.32
Calculation (Case 1)
Case 1: i – 1 is an exact power of 2.
ĉi = i + (2i – 2lg i) – (2(i–1) – 2lg (i–1))
= i + 2 – (2lg i – 2lg (i–1))
= i + 2 – (2(i – 1) – (i – 1))
= i + 2 – 2i + 2 + i – 1
= 3
L19.33
Calculation (Case 2)
Case 2: i – 1 is not an exact power of 2.
ĉi = 1 + (2i – 2lg i) – (2(i–1) – 2lg (i–1))
= 1 + 2 – (2lg i – 2lg (i–1))
= 3
Therefore, n insertions cost Q(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.
L19.34
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.
• Different schemes may work for assigning
amortized costs in the accounting method, or
potentials in the potential method, sometimes
yielding radically different bounds.

More Related Content

Similar to AA_Unit 1_part-II.pdf

Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
Rajendran
 
DynamicProgramming.ppt
DynamicProgramming.pptDynamicProgramming.ppt
DynamicProgramming.ppt
DavidMaina47
 
Aoa amortized analysis
Aoa amortized analysisAoa amortized analysis
Aoa amortized analysis
Salabat Khan
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
giridaroori
 
DynamicProgramming.pdf
DynamicProgramming.pdfDynamicProgramming.pdf
DynamicProgramming.pdf
ssuser3a8f33
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
abay golla
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
paramita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
paramita30
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Amit Kumar Rathi
 
3.2 Graphs of Functions
3.2 Graphs of Functions3.2 Graphs of Functions
3.2 Graphs of Functions
smiller5
 
3.2 Graphs of Functions
3.2 Graphs of Functions3.2 Graphs of Functions
3.2 Graphs of Functions
smiller5
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
paramita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
paramita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
paramita30
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
Dr. C.V. Suresh Babu
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
paramita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
paramita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
paramita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
paramita30
 

Similar to AA_Unit 1_part-II.pdf (20)

Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
DynamicProgramming.ppt
DynamicProgramming.pptDynamicProgramming.ppt
DynamicProgramming.ppt
 
Aoa amortized analysis
Aoa amortized analysisAoa amortized analysis
Aoa amortized analysis
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
DynamicProgramming.pdf
DynamicProgramming.pdfDynamicProgramming.pdf
DynamicProgramming.pdf
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
3.2 Graphs of Functions
3.2 Graphs of Functions3.2 Graphs of Functions
3.2 Graphs of Functions
 
3.2 Graphs of Functions
3.2 Graphs of Functions3.2 Graphs of Functions
3.2 Graphs of Functions
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 

More from swapnilslide2019

Topic-G-JavaCollections Framework.ppt
Topic-G-JavaCollections    Framework.pptTopic-G-JavaCollections    Framework.ppt
Topic-G-JavaCollections Framework.ppt
swapnilslide2019
 
Collectionsand GenericsInJavaProgramming.ppt
Collectionsand GenericsInJavaProgramming.pptCollectionsand GenericsInJavaProgramming.ppt
Collectionsand GenericsInJavaProgramming.ppt
swapnilslide2019
 
Flow Network Introduction
Flow Network IntroductionFlow Network Introduction
Flow Network Introduction
swapnilslide2019
 
kdtrees.pdf
kdtrees.pdfkdtrees.pdf
kdtrees.pdf
swapnilslide2019
 
AA_Unit_6.pptx
AA_Unit_6.pptxAA_Unit_6.pptx
AA_Unit_6.pptx
swapnilslide2019
 
AA_Unit_4.pptx
AA_Unit_4.pptxAA_Unit_4.pptx
AA_Unit_4.pptx
swapnilslide2019
 
AA_Unit_3.pptx
AA_Unit_3.pptxAA_Unit_3.pptx
AA_Unit_3.pptx
swapnilslide2019
 
AA_Unit_2.pptx
AA_Unit_2.pptxAA_Unit_2.pptx
AA_Unit_2.pptx
swapnilslide2019
 
Programming Laboratory Unit 1.pdf
Programming Laboratory Unit 1.pdfProgramming Laboratory Unit 1.pdf
Programming Laboratory Unit 1.pdf
swapnilslide2019
 
CI.pdf
CI.pdfCI.pdf

More from swapnilslide2019 (12)

Topic-G-JavaCollections Framework.ppt
Topic-G-JavaCollections    Framework.pptTopic-G-JavaCollections    Framework.ppt
Topic-G-JavaCollections Framework.ppt
 
Collectionsand GenericsInJavaProgramming.ppt
Collectionsand GenericsInJavaProgramming.pptCollectionsand GenericsInJavaProgramming.ppt
Collectionsand GenericsInJavaProgramming.ppt
 
Flow Network Introduction
Flow Network IntroductionFlow Network Introduction
Flow Network Introduction
 
kdtrees.pdf
kdtrees.pdfkdtrees.pdf
kdtrees.pdf
 
AA_Unit_6.pptx
AA_Unit_6.pptxAA_Unit_6.pptx
AA_Unit_6.pptx
 
AA_Unit_4.pptx
AA_Unit_4.pptxAA_Unit_4.pptx
AA_Unit_4.pptx
 
AA_Unit_3.pptx
AA_Unit_3.pptxAA_Unit_3.pptx
AA_Unit_3.pptx
 
AA_Unit_2.pptx
AA_Unit_2.pptxAA_Unit_2.pptx
AA_Unit_2.pptx
 
Programming Laboratory Unit 1.pdf
Programming Laboratory Unit 1.pdfProgramming Laboratory Unit 1.pdf
Programming Laboratory Unit 1.pdf
 
PL-I.pdf
PL-I.pdfPL-I.pdf
PL-I.pdf
 
CI.pdf
CI.pdfCI.pdf
CI.pdf
 
PL-I.pdf
PL-I.pdfPL-I.pdf
PL-I.pdf
 

Recently uploaded

Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
PriyankaKilaniya
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
CVCSOfficial
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
bjmsejournal
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
upoux
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
PIMR BHOPAL
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
AjmalKhan50578
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
AI for Legal Research with applications, tools
AI for Legal Research with applications, toolsAI for Legal Research with applications, tools
AI for Legal Research with applications, tools
mahaffeycheryld
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
Paris Salesforce Developer Group
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
Yasser Mahgoub
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
ycwu0509
 
Gas agency management system project report.pdf
Gas agency management system project report.pdfGas agency management system project report.pdf
Gas agency management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
AI for Legal Research with applications, tools
AI for Legal Research with applications, toolsAI for Legal Research with applications, tools
AI for Legal Research with applications, tools
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
 
Gas agency management system project report.pdf
Gas agency management system project report.pdfGas agency management system project report.pdf
Gas agency management system project report.pdf
 

AA_Unit 1_part-II.pdf

  • 1. Amortized Algorithms, Table Doubling, Potential Method Lecture 19
  • 2. L19.2 How large should a hash table be? Problem: What if we don’t know the proper size in advance? Goal: Make the table as small as possible, but large enough so that it won’t overflow (or otherwise become inefficient). 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. L19.3 Example of a dynamic table 1. INSERT 1 2. INSERT overflow
  • 4. L19.4 1 Example of a dynamic table 1. INSERT 2. INSERT overflow
  • 5. L19.5 1 2 Example of a dynamic table 1. INSERT 2. INSERT
  • 6. L19.6 Example of a dynamic table 1. INSERT 2. INSERT 1 2 3. INSERT overflow
  • 7. L19.7 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 2 1 overflow
  • 8. L19.8 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 2 1
  • 9. L19.9 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 4. INSERT 4 3 2 1
  • 10. L19.10 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 4. INSERT 5. INSERT 4 3 2 1 overflow
  • 11. L19.11 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 4. INSERT 5. INSERT 4 3 2 1 overflow
  • 12. L19.12 Example of a dynamic table 1. INSERT 2. INSERT 3. INSERT 4. INSERT 5. INSERT 4 3 2 1
  • 13. L19.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. L19.14 Worst-case analysis Consider a sequence of n insertions. The worst-case time to execute one insertion is Q(n). Therefore, the worst-case time for n insertions is n · Q(n) = Q(n2). WRONG! In fact, the worst-case cost for n insertions is only Q(n) ≪ Q(n2). Let’s see why.
  • 15. L19.15 Tighter analysis 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 Let ci = the cost of the ith insertion = i if i – 1 is an exact power of 2, 1 otherwise.
  • 16. L19.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. L19.17 Tighter analysis (continued)   ) ( 3 2 ) 1 lg( 0 1 n n n c n j j n i i Q           Cost of n insertions . Thus, the average cost of each dynamic-table operation is Q(n)/n = Q(1).
  • 18. L19.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. L19.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. L19.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      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. L19.21 $0 $0 $0 $0 $2 $2 Example: $2 $2 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. When the table doubles, $1 pays to move a recent item, and $1 pays to move an old item. overflow
  • 22. L19.22 Example: 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. 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
  • 23. L19.23 Example: 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. 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 $2 $2 $2
  • 24. L19.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 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 ĉi 2 3 3 3 3 3 3 3 3 3 banki 1 2 2 4 2 4 6 8 2 4 * *Okay, so I lied. The first operation costs only $2, not $3.
  • 25. L19.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 F : {Di}  R, such that F(D0 ) = 0 and F(Di )  0 for all i. • The amortized cost ĉi with respect to F is defined to be ĉi = ci + F(Di) – F(Di–1).
  • 26. L19.26 Understanding potentials ĉi = ci + F(Di) – F(Di–1) potential difference DFi • If DFi > 0, then ĉi > ci. Operation i stores work in the data structure for later use. • If DFi < 0, then ĉi < ci. The data structure delivers up stored work to help pay for operation i.
  • 27. L19.27 The amortized costs bound the true costs The total amortized cost of n operations is        F  F   n i i i i n i i D D c c 1 1 1 ) ( ) ( ˆ Summing both sides.
  • 28. L19.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 F  F   F  F          The series telescopes.
  • 29. L19.29 The amortized costs bound the true costs The total amortized cost of n operations is             F  F   F  F   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 F(Dn)  0 and F(D0 ) = 0.
  • 30. L19.30 Potential analysis of table doubling Define the potential of the table after the ith insertion by F(Di) = 2i – 2lg i. (Assume that 2lg 0 = 0.) Note: • F(D0 ) = 0, • F(Dn)  0 for all i. Example: • • • • • • F = 2·6 + 23 = 4 $0 $0 $0 $0 $2 $2 accounting method) (
  • 31. L19.31 Calculation of amortized costs The amortized cost of the ith insertion is ĉi = ci + F(Di) – F(Di–1) i + (2i – 2lg i) – (2(i–1) – 2lg (i–1)) if i – 1 is an exact power of 2, 1 + (2i – 2lg i) – (2(i–1) – 2lg (i–1)) otherwise. =
  • 32. L19.32 Calculation (Case 1) Case 1: i – 1 is an exact power of 2. ĉi = i + (2i – 2lg i) – (2(i–1) – 2lg (i–1)) = i + 2 – (2lg i – 2lg (i–1)) = i + 2 – (2(i – 1) – (i – 1)) = i + 2 – 2i + 2 + i – 1 = 3
  • 33. L19.33 Calculation (Case 2) Case 2: i – 1 is not an exact power of 2. ĉi = 1 + (2i – 2lg i) – (2(i–1) – 2lg (i–1)) = 1 + 2 – (2lg i – 2lg (i–1)) = 3 Therefore, n insertions cost Q(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.
  • 34. L19.34 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. • Different schemes may work for assigning amortized costs in the accounting method, or potentials in the potential method, sometimes yielding radically different bounds.