SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Downloaden Sie, um offline zu lesen
Design & Analysis of Algorithm 
Amortized Analysis 
Kamalesh Karmakar 
Assistant Professor 
Department of C.S.E. 
MeghnadSahaInstitute of Technology 
Kolkata.
Design & Analysis of Algorithm 
Inanamortizedanalysis,thetimerequiredtoperformasequenceofdata- structureoperationsisaveragedoveralltheoperationsperformed.Amortizedanalysiscanbeusedtoshowthattheaveragecostofanoperationissmall,ifoneaveragesoverasequenceofoperations,eventhoughasingleoperationwithinthesequencemightbeexpensive. 
Amortizedanalysisdiffersfromaverage-caseanalysisinthatprobabilityisnotinvolved;anamortizedanalysisguaranteestheaverageperformanceofeachoperationintheworstcase. 
A.Aggregateanalysis 
B.Theaccountingmethod 
C.Thepotentialmethod 
D.Dynamictables 
Amortized Analysis
Design & Analysis of Algorithm 
InAggregateAnalysis,weshowthatforalln,asequenceofnoperationstakesworst-casetimeT(n)intotal. 
Intheworstcase,theaveragecost,oramortizedcost,peroperationisthereforeT(n)/n. 
Notethatthisamortizedcostappliestoeachoperation,evenwhenthereareseveraltypesofoperationsinthesequence. 
Amortized Analysis
Design & Analysis of Algorithm 
Example:Incrementingabinarycounter 
As another example of aggregate analysis, consider the problem of implementing a k-bit binary counter that counts upward from 0. We use an array A[0 . . k −1] of bits, where length[A] = k, as the counter. A binary number x that is stored in the counter has its lowest-order bit in A[0] and its highest-order bit in A[k −1], so that 
í‘„= 푖=0 푘−1퐮푖.2푖 
Initially, x = 0, and thus A[i] = 0 for i= 0, 1, . . . , k −1. To add 1(modulo 2k) to the value in the counter, we use the following procedure. 
Amortized Analysis
Design & Analysis of Algorithm 
Amortized Analysis 
 The cost of each INCREMENT 
operation is linear in the number of 
bits flipped. 
 A single execution of INCREMENT 
takes time (k) in the worst case, in 
which array A contains all 1’s. 
 Thus, a sequence of n INCREMENT 
operations on an initially zero 
counter takes time O(nk) in the worst 
case.
Design & Analysis of Algorithm 
Amortized Analysis 
Wecantightenouranalysistoyieldaworst-casecostofO(n)forasequenceofnINCREMENT’sbyobservingthatnotallbitsflipeachtimeINCREMENTiscalled. AsFigureshows,A[0]doesflipeachtimeINCREMENTiscalled. 
Thenext-highest-orderbit,A[1],flipsonlyeveryothertime:asequenceofnINCREMENToperationsonaninitiallyzerocountercausesA[1]toflip푛/2times. 
Similarly,bitA[2]flipsonlyeveryfourthtime,or푛/4timesinasequenceofnINCREMENT’s.Ingeneral,fori=0,1,...,lg푛/2,bitA[i]flips푛/2푖timesinasequenceofnINCREMENToperationsonaninitiallyzerocounter. 
Fori>푙푔푛,bitA[i]neverflipsatall.Thetotalnumberofflipsinthesequenceisthus 
Theworst-casetimeforasequenceofnINCREMENToperationsonaninitiallyzerocounteristhereforeO(n).Theaveragecostofeachoperation,andthereforetheamortizedcostperoperation,isO(n)/n=O(1).
Design & Analysis of Algorithm 
Amortized Analysis 
Intheaccountingmethodofamortizedanalysis,weassigndifferingchargestodifferentoperations,withsomeoperationschargedmoreorlessthantheyactuallycost. 
Theamountwechargeanoperationiscalleditsamortizedcost. 
Whenanoperation’samortizedcostexceedsitsactualcost,thedifferenceisassignedtospecificobjectsinthedatastructureascredit. 
Creditcanbeusedlaterontohelppayforoperationswhoseamortizedcostislessthantheiractualcost.Thus,onecanviewtheamortizedcostofanoperationasbeingsplitbetweenitsactualcostandcreditthatiseitherdepositedorusedup. 
Thismethodisverydifferentfromaggregateanalysis,inwhichalloperationshavethesameamortizedcost.
Design & Analysis of Algorithm 
Amortized Analysis 
 One must choose the amortized costs of operations carefully. If we want analysis 
with amortized costs to show that in the worst case the average cost per 
operation is small, the total amortized cost of a sequence of operations must be 
an upper bound on the total actual cost of the sequence. Moreover, as in 
aggregate analysis, this relationship must hold for all sequences of operations. If 
we denote the actual cost of the ith operation by ci and the amortized cost of the 
ith operation by , we require 
for all sequences of n operations. The total credit stored in the data structure is the 
difference between the total amortized cost and the total actual cost, or 
 By inequality the total credit associated with the data structure must be 
nonnegative at all times. If the total credit were ever allowed to become negative, 
then the total amortized costs incurred at that time would be below the total 
actual costs incurred; for the sequence of operations up to that time, the total 
amortized cost would not be an upper bound on the total actual cost. Thus, we 
must take care that the total credit in the data structure never becomes negative.
Design & Analysis of Algorithm 
Amortized Analysis 
Example:Incrementingabinarycounter 
Asanotherillustrationoftheaccountingmethod,weanalyzetheINCREMENToperationonabinarycounterthatstartsatzero.Asweobservedearlier,therunningtimeofthisoperationisproportionaltothenumberofbitsflipped,whichweshalluseasourcostforthisexample.Letusonceagainuseadollarbilltorepresenteachunitofcost. 
Fortheamortizedanalysis,letuschargeanamortizedcostof2dollarstosetabitto1. 
Whenabitisset,weuse1dollartopaytoflipthebitbackto0. 
TheamortizedcostofINCREMENTcannowbedetermined.Thecostofresettingthebitswithinthewhileloopispaidforbythedollarsonthebitsthatarereset. 
Thenumberof1’sinthecounterisnevernegative,andthustheamountofcreditisalwaysnonnegative.Thus,fornINCREMENToperations,thetotalamortizedcostisO(n),whichboundsthetotalactualcost.
Design & Analysis of Algorithm 
Amortized Analysis 
 Instead of representing prepaid work as credit stored with specific objects in the 
data structure, the potential method of amortized analysis represents the 
prepaid work as “potential energy,” or just “potential,” that can be released to pay 
for future operations. The potential is associated with the data structure as a 
whole rather than with specific objects within the data structure. 
 The potential method works as follows. We start with an initial data structure D0 
on which n operations are performed. For each i = 1, 2, . . . , n, we let ci be the 
actual cost of the ith operation and Di be the data structure that results after 
applying the ith operation to data structure Di−1. 
 A potential function ф maps each data structure Di to a real number ф(Di ), 
which is the potential associated with data structure Di. The amortized cost of 
the ith operation with respect to potential function ф is defined by
Design & Analysis of Algorithm 
Amortized Analysis 
 The amortized cost of each operation is therefore its actual cost plus the increase 
in potential due to the operation.The total amortized cost of the n operations is 
 If we can define a potential function ф so that (Dn) ≄ (D0), then the total 
amortized cost is an upper bound on the total actual cost 
 In practice, we do not always know how many operations might be performed. 
Therefore, if we require that ф(Di) ≄ ф(D0) for all i , then we guarantee, as in the 
accounting method, that we pay in advance. It is often convenient to define ф(D0) 
to be 0 and then to show that ф(Di ) ≄ 0 for all i . 
 Intuitively, if the potential difference ф(Di ) − ф(Di−1) of the ith operation is 
positive, then the amortized cost ci represents an overcharge to the ith operation, 
and the potential of the data structure increases. If the potential difference is 
negative, then the amortized cost represents an undercharge to the ith operation, 
and the actual cost of the operation is paid by the decrease in the potential.
Design & Analysis of Algorithm 
Amortized Analysis 
Example: Incrementing a binary counter 
 As another example of the potential method, we again look at incrementing a 
binary counter. This time, we define the potential of the counter after the ith 
INCREMENT operation to be bi , the number of 1’s in the counter after the ith 
operation. Let us compute the amortized cost of an INCREMENT operation. 
Suppose that the ith INCREMENT operation resets ti bits. The actual cost of the 
operation is therefore at most ti + 1, since in addition to resetting ti bits, it sets at 
most one bit to 1. 
 If bi = 0, then the ith operation resets all k bits, and so bi−1 = ti = k. 
 If bi > 0, then bi = bi−1 −ti +1. 
 In either case, bi ≀ bi−1 −ti +1, and the potential difference is 
 The amortized cost is therefore
Design & Analysis of Algorithm 
Amortized Analysis 
 If the counter starts at zero, then ф(D0) = 0. Since ф(Di ) ≄ 0 for all i , the total 
amortized cost of a sequence of n INCREMENT operations is an upper bound on 
the total actual cost, and so the worst-case cost of n INCREMENT operations is 
O(n). 
 The potential method gives us an easy way to analyze the counter even when it 
does not start at zero. There are initially b0 1’s, and after n INCREMENT 
operations there are bn 1’s, where 0 ≀ b0, bn ≀ k. (Recall that k is the number of 
bits in the counter.)We can rewrite equation as 
 We have for all 1 ≀ i ≀ n. Since ф(D0) = b0 and ф(Dn) = bn, the total actual 
cost of n INCREMENT operations is 
 Note in particular that since b0 ≀ k, as long as k = O(n), the total actual cost is 
O(n). In other words, if we execute at least n = (k) INCREMENT operations, the 
total actual cost is O(n), no matter what initial value the counter contains.

Weitere Àhnliche Inhalte

Was ist angesagt?

unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programminghodcsencet
 
Closest pair problems (Divide and Conquer)
Closest pair problems (Divide and Conquer)Closest pair problems (Divide and Conquer)
Closest pair problems (Divide and Conquer)Gem WeBlog
 
Introduction to data structures (ss)
Introduction to data structures (ss)Introduction to data structures (ss)
Introduction to data structures (ss)Madishetty Prathibha
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of AlgorithmMuhammad Muzammal
 
Lecture 3 parallel programming platforms
Lecture 3   parallel programming platformsLecture 3   parallel programming platforms
Lecture 3 parallel programming platformsVajira Thambawita
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Ra'Fat Al-Msie'deen
 
Operating Systems - Processor Management
Operating Systems - Processor ManagementOperating Systems - Processor Management
Operating Systems - Processor ManagementDamian T. Gordon
 
QUEUEING NETWORKS
QUEUEING NETWORKSQUEUEING NETWORKS
QUEUEING NETWORKSRohitK71
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy AlgorithmsAmrinder Arora
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithmsChankey Pathak
 
Database ,11 Concurrency Control
Database ,11 Concurrency ControlDatabase ,11 Concurrency Control
Database ,11 Concurrency ControlAli Usman
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C ProgramsKandarp Tiwari
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesFellowBuddy.com
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
First Come First Serve
First Come First ServeFirst Come First Serve
First Come First ServeKavya Kapoor
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-exportFAO
 

Was ist angesagt? (20)

unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 
Closest pair problems (Divide and Conquer)
Closest pair problems (Divide and Conquer)Closest pair problems (Divide and Conquer)
Closest pair problems (Divide and Conquer)
 
Introduction to data structures (ss)
Introduction to data structures (ss)Introduction to data structures (ss)
Introduction to data structures (ss)
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Lecture 3 parallel programming platforms
Lecture 3   parallel programming platformsLecture 3   parallel programming platforms
Lecture 3 parallel programming platforms
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
EDA-Unit 1.pdf
EDA-Unit 1.pdfEDA-Unit 1.pdf
EDA-Unit 1.pdf
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
Operating Systems - Processor Management
Operating Systems - Processor ManagementOperating Systems - Processor Management
Operating Systems - Processor Management
 
QUEUEING NETWORKS
QUEUEING NETWORKSQUEUEING NETWORKS
QUEUEING NETWORKS
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
 
Database ,11 Concurrency Control
Database ,11 Concurrency ControlDatabase ,11 Concurrency Control
Database ,11 Concurrency Control
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
First Come First Serve
First Come First ServeFirst Come First Serve
First Come First Serve
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-export
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 

Ähnlich wie 09. amortized analysis

DOC-20230417-WA0013.-7-27.pptx
DOC-20230417-WA0013.-7-27.pptxDOC-20230417-WA0013.-7-27.pptx
DOC-20230417-WA0013.-7-27.pptx03Shrishti
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexityparamita30
 
Aoa amortized analysis
Aoa amortized analysisAoa amortized analysis
Aoa amortized analysisSalabat Khan
 
Aad introduction
Aad introductionAad introduction
Aad introductionMr SMAK
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfMemMem25
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxskilljiolms
 
AA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxAA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxswapnilslide2019
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMSTanya Makkar
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESsuthi
 
Data structures using C
Data structures using CData structures using C
Data structures using CPdr Patnaik
 

Ähnlich wie 09. amortized analysis (20)

DOC-20230417-WA0013.-7-27.pptx
DOC-20230417-WA0013.-7-27.pptxDOC-20230417-WA0013.-7-27.pptx
DOC-20230417-WA0013.-7-27.pptx
 
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
 
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
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Amortized complexity
Amortized complexityAmortized complexity
Amortized complexity
 
Aoa amortized analysis
Aoa amortized analysisAoa amortized analysis
Aoa amortized analysis
 
Aad introduction
Aad introductionAad introduction
Aad introduction
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
AA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxAA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptx
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 

Mehr von Onkar Nath Sharma

03. dynamic programming
03. dynamic programming03. dynamic programming
03. dynamic programmingOnkar Nath Sharma
 
02. divide and conquer
02. divide and conquer02. divide and conquer
02. divide and conquerOnkar Nath Sharma
 
01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysisOnkar Nath Sharma
 

Mehr von Onkar Nath Sharma (9)

08. graph traversal
08. graph traversal08. graph traversal
08. graph traversal
 
07. disjoint set
07. disjoint set07. disjoint set
07. disjoint set
 
06. string matching
06. string matching06. string matching
06. string matching
 
05. greedy method
05. greedy method05. greedy method
05. greedy method
 
04. backtracking
04. backtracking04. backtracking
04. backtracking
 
03. dynamic programming
03. dynamic programming03. dynamic programming
03. dynamic programming
 
02. divide and conquer
02. divide and conquer02. divide and conquer
02. divide and conquer
 
01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis
 
Analyzing algorithms
Analyzing algorithmsAnalyzing algorithms
Analyzing algorithms
 

KĂŒrzlich hochgeladen

computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
TechTACÂź CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTACÂź CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTACÂź CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTACÂź CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Gurgaon âœĄïž9711147426✹Call In girls Gurgaon Sector 51 escort service
Gurgaon âœĄïž9711147426✹Call In girls Gurgaon Sector 51 escort serviceGurgaon âœĄïž9711147426✹Call In girls Gurgaon Sector 51 escort service
Gurgaon âœĄïž9711147426✹Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 

KĂŒrzlich hochgeladen (20)

computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
TechTACÂź CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTACÂź CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTACÂź CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTACÂź CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Gurgaon âœĄïž9711147426✹Call In girls Gurgaon Sector 51 escort service
Gurgaon âœĄïž9711147426✹Call In girls Gurgaon Sector 51 escort serviceGurgaon âœĄïž9711147426✹Call In girls Gurgaon Sector 51 escort service
Gurgaon âœĄïž9711147426✹Call In girls Gurgaon Sector 51 escort service
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 

09. amortized analysis

  • 1. Design & Analysis of Algorithm Amortized Analysis Kamalesh Karmakar Assistant Professor Department of C.S.E. MeghnadSahaInstitute of Technology Kolkata.
  • 2. Design & Analysis of Algorithm Inanamortizedanalysis,thetimerequiredtoperformasequenceofdata- structureoperationsisaveragedoveralltheoperationsperformed.Amortizedanalysiscanbeusedtoshowthattheaveragecostofanoperationissmall,ifoneaveragesoverasequenceofoperations,eventhoughasingleoperationwithinthesequencemightbeexpensive. Amortizedanalysisdiffersfromaverage-caseanalysisinthatprobabilityisnotinvolved;anamortizedanalysisguaranteestheaverageperformanceofeachoperationintheworstcase. A.Aggregateanalysis B.Theaccountingmethod C.Thepotentialmethod D.Dynamictables Amortized Analysis
  • 3. Design & Analysis of Algorithm InAggregateAnalysis,weshowthatforalln,asequenceofnoperationstakesworst-casetimeT(n)intotal. Intheworstcase,theaveragecost,oramortizedcost,peroperationisthereforeT(n)/n. Notethatthisamortizedcostappliestoeachoperation,evenwhenthereareseveraltypesofoperationsinthesequence. Amortized Analysis
  • 4. Design & Analysis of Algorithm Example:Incrementingabinarycounter As another example of aggregate analysis, consider the problem of implementing a k-bit binary counter that counts upward from 0. We use an array A[0 . . k −1] of bits, where length[A] = k, as the counter. A binary number x that is stored in the counter has its lowest-order bit in A[0] and its highest-order bit in A[k −1], so that í‘„= 푖=0 푘−1퐮푖.2푖 Initially, x = 0, and thus A[i] = 0 for i= 0, 1, . . . , k −1. To add 1(modulo 2k) to the value in the counter, we use the following procedure. Amortized Analysis
  • 5. Design & Analysis of Algorithm Amortized Analysis  The cost of each INCREMENT operation is linear in the number of bits flipped.  A single execution of INCREMENT takes time (k) in the worst case, in which array A contains all 1’s.  Thus, a sequence of n INCREMENT operations on an initially zero counter takes time O(nk) in the worst case.
  • 6. Design & Analysis of Algorithm Amortized Analysis Wecantightenouranalysistoyieldaworst-casecostofO(n)forasequenceofnINCREMENT’sbyobservingthatnotallbitsflipeachtimeINCREMENTiscalled. AsFigureshows,A[0]doesflipeachtimeINCREMENTiscalled. Thenext-highest-orderbit,A[1],flipsonlyeveryothertime:asequenceofnINCREMENToperationsonaninitiallyzerocountercausesA[1]toflip푛/2times. Similarly,bitA[2]flipsonlyeveryfourthtime,or푛/4timesinasequenceofnINCREMENT’s.Ingeneral,fori=0,1,...,lg푛/2,bitA[i]flips푛/2푖timesinasequenceofnINCREMENToperationsonaninitiallyzerocounter. Fori>푙푔푛,bitA[i]neverflipsatall.Thetotalnumberofflipsinthesequenceisthus Theworst-casetimeforasequenceofnINCREMENToperationsonaninitiallyzerocounteristhereforeO(n).Theaveragecostofeachoperation,andthereforetheamortizedcostperoperation,isO(n)/n=O(1).
  • 7. Design & Analysis of Algorithm Amortized Analysis Intheaccountingmethodofamortizedanalysis,weassigndifferingchargestodifferentoperations,withsomeoperationschargedmoreorlessthantheyactuallycost. Theamountwechargeanoperationiscalleditsamortizedcost. Whenanoperation’samortizedcostexceedsitsactualcost,thedifferenceisassignedtospecificobjectsinthedatastructureascredit. Creditcanbeusedlaterontohelppayforoperationswhoseamortizedcostislessthantheiractualcost.Thus,onecanviewtheamortizedcostofanoperationasbeingsplitbetweenitsactualcostandcreditthatiseitherdepositedorusedup. Thismethodisverydifferentfromaggregateanalysis,inwhichalloperationshavethesameamortizedcost.
  • 8. Design & Analysis of Algorithm Amortized Analysis  One must choose the amortized costs of operations carefully. If we want analysis with amortized costs to show that in the worst case the average cost per operation is small, the total amortized cost of a sequence of operations must be an upper bound on the total actual cost of the sequence. Moreover, as in aggregate analysis, this relationship must hold for all sequences of operations. If we denote the actual cost of the ith operation by ci and the amortized cost of the ith operation by , we require for all sequences of n operations. The total credit stored in the data structure is the difference between the total amortized cost and the total actual cost, or  By inequality the total credit associated with the data structure must be nonnegative at all times. If the total credit were ever allowed to become negative, then the total amortized costs incurred at that time would be below the total actual costs incurred; for the sequence of operations up to that time, the total amortized cost would not be an upper bound on the total actual cost. Thus, we must take care that the total credit in the data structure never becomes negative.
  • 9. Design & Analysis of Algorithm Amortized Analysis Example:Incrementingabinarycounter Asanotherillustrationoftheaccountingmethod,weanalyzetheINCREMENToperationonabinarycounterthatstartsatzero.Asweobservedearlier,therunningtimeofthisoperationisproportionaltothenumberofbitsflipped,whichweshalluseasourcostforthisexample.Letusonceagainuseadollarbilltorepresenteachunitofcost. Fortheamortizedanalysis,letuschargeanamortizedcostof2dollarstosetabitto1. Whenabitisset,weuse1dollartopaytoflipthebitbackto0. TheamortizedcostofINCREMENTcannowbedetermined.Thecostofresettingthebitswithinthewhileloopispaidforbythedollarsonthebitsthatarereset. Thenumberof1’sinthecounterisnevernegative,andthustheamountofcreditisalwaysnonnegative.Thus,fornINCREMENToperations,thetotalamortizedcostisO(n),whichboundsthetotalactualcost.
  • 10. Design & Analysis of Algorithm Amortized Analysis  Instead of representing prepaid work as credit stored with specific objects in the data structure, the potential method of amortized analysis represents the prepaid work as “potential energy,” or just “potential,” that can be released to pay for future operations. The potential is associated with the data structure as a whole rather than with specific objects within the data structure.  The potential method works as follows. We start with an initial data structure D0 on which n operations are performed. For each i = 1, 2, . . . , n, we let ci be the actual cost of the ith operation and Di be the data structure that results after applying the ith operation to data structure Di−1.  A potential function ф maps each data structure Di to a real number ф(Di ), which is the potential associated with data structure Di. The amortized cost of the ith operation with respect to potential function ф is defined by
  • 11. Design & Analysis of Algorithm Amortized Analysis  The amortized cost of each operation is therefore its actual cost plus the increase in potential due to the operation.The total amortized cost of the n operations is  If we can define a potential function ф so that (Dn) ≄ (D0), then the total amortized cost is an upper bound on the total actual cost  In practice, we do not always know how many operations might be performed. Therefore, if we require that ф(Di) ≄ ф(D0) for all i , then we guarantee, as in the accounting method, that we pay in advance. It is often convenient to define ф(D0) to be 0 and then to show that ф(Di ) ≄ 0 for all i .  Intuitively, if the potential difference ф(Di ) − ф(Di−1) of the ith operation is positive, then the amortized cost ci represents an overcharge to the ith operation, and the potential of the data structure increases. If the potential difference is negative, then the amortized cost represents an undercharge to the ith operation, and the actual cost of the operation is paid by the decrease in the potential.
  • 12. Design & Analysis of Algorithm Amortized Analysis Example: Incrementing a binary counter  As another example of the potential method, we again look at incrementing a binary counter. This time, we define the potential of the counter after the ith INCREMENT operation to be bi , the number of 1’s in the counter after the ith operation. Let us compute the amortized cost of an INCREMENT operation. Suppose that the ith INCREMENT operation resets ti bits. The actual cost of the operation is therefore at most ti + 1, since in addition to resetting ti bits, it sets at most one bit to 1.  If bi = 0, then the ith operation resets all k bits, and so bi−1 = ti = k.  If bi > 0, then bi = bi−1 −ti +1.  In either case, bi ≀ bi−1 −ti +1, and the potential difference is  The amortized cost is therefore
  • 13. Design & Analysis of Algorithm Amortized Analysis  If the counter starts at zero, then ф(D0) = 0. Since ф(Di ) ≄ 0 for all i , the total amortized cost of a sequence of n INCREMENT operations is an upper bound on the total actual cost, and so the worst-case cost of n INCREMENT operations is O(n).  The potential method gives us an easy way to analyze the counter even when it does not start at zero. There are initially b0 1’s, and after n INCREMENT operations there are bn 1’s, where 0 ≀ b0, bn ≀ k. (Recall that k is the number of bits in the counter.)We can rewrite equation as  We have for all 1 ≀ i ≀ n. Since ф(D0) = b0 and ф(Dn) = bn, the total actual cost of n INCREMENT operations is  Note in particular that since b0 ≀ k, as long as k = O(n), the total actual cost is O(n). In other words, if we execute at least n = (k) INCREMENT operations, the total actual cost is O(n), no matter what initial value the counter contains.