SlideShare a Scribd company logo
1 of 16
Amortized Analysis
    By Waqas Shehzad
    Fast NU Pakistan
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!
Operations on Data
Structures
 A data structure has a set of operations
  associated with it.
 Example: A stack with
   push(), pop() and MultiPop(k).
 Often some operations may be slow
  while others are fast.
 push() and pop() are fast.
 MultiPop(k) may be slow.
 Sometimes the time of a single
  operation can vary
Amortized Analysis
Aggregate Method: we determine an upper
 bound T(n) on the total sequence of n
 operations. The cost of each will then be
 T(n)/n.
Accounting Method: we overcharge some
 operations early and use them to as prepaid
 charge later.
Potential Method: we maintain credit as
 potential energy associated with the structure
 as a whole.
Aggregate Method
We show that for n operation, the sequence
 takes worst case T(n)
We use as an example, a stack with a new
 operation MULTIPOP (S,k)
If we consider PUSH and POP to be elementary
 operations, then Multipop takes O(n) in the worst
 case.
Stack ops
Stack - aggregate
 analysis
Push and Pop are O(1) (to move 1 data element)
Multipop is O(min(s, k)) where
     s is the size of the stack and
     k the number of elements to pop.
Assume a sequence of n Push, Pop and Multipop
operations
      Multipop(S, k)
                       while not empty(S) and k>0 do
                             Pop(S);
                             k:=k-1
                       end while
                                                       7
Aggregate Method
• Sequence of n push, pop, Multipop operations
• Worst-case cost of Multipop is O(n)
• Have n operations
• Therefore, worst-case cost of sequence is
 O(n2)
However, since an object can be only popped
 once (whether by pop or multipop) for every
 time it is pushed, the total number of Pop
 operations is at most the total number of Push
 operations (which is at most n)
Stack - aggregate
analysis
A sequence of n Push and Pop
 operations is therefore O(n) and the
 amortized cost of each is
                 O(n)/n=O(1)
Accounting Method
We assign different charges to different
 operations, with some operations charged
 more or less than they actually cost.
The amount we charge an operation is its
 amortized cost.
When an operation’s amortized cost exceeds
 its actual cost, the difference is assigned to
 specific objects in the data structure as credit.
Accounting Method
Credit can be used later to pay the cost of
 operations whose actual cost is greater than
 its amortized cost.
The total credit stored must always be non-
 negative at all times.
We must ensure that
            n           n
           ∑ ci ≤ ∑ ci
                    ˆ
           i =1        i =1
Amortized cost   ĉi
Accounting Method: Stack
Thing about this: when we push a plate onto
 a stack, we use $1 to pay actual cost of the
 push and we leave $1 on the plate.
At any point, every plate on the stack has a
 dollar on top of it.
When we execute a pop operation, we
 charge it nothing and pay its cost with the
 dollar that is on top of it.
A Simple Example: Accounting



3 ops:


                Push(S,x)   Pop(S)   Multi-pop(S,k)
•Assigned
                   2          0            0
cost:
•Actual cost:      1          1        min(|S|,k)
Potential Method
Instead of representing prepaid work as credit
 stored with specific objects, the potential method
 represents the prepaid work as potential energy
 than can be released to pay for future
 operations.
Potential is associated with the data structure as
 a whole rather than with specific objects.
Potential Method
 We start with an initial data structure D0 on which
  n operations are performed.
 Let ci be the cost the ith operations and Di be the
  data structure that results after applying the ith
  operation to data structure Di-1
 A potential function Φ maps Di to a real number
  Φ(Di) which is the potential associated with Di
Applications of
      amortized analysis
Vectors/ tables
Disjoint sets
Priority queues
Heaps, Binomial heaps, Fibonacci heaps
Hashing

More Related Content

What's hot

Infix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackInfix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackSoumen Santra
 
Array operations
Array operationsArray operations
Array operationsZAFAR444
 
Computational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesComputational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesAntonis Antonopoulos
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
Rabin karp string matching algorithm
Rabin karp string matching algorithmRabin karp string matching algorithm
Rabin karp string matching algorithmGajanand Sharma
 
Data encryption standard
Data encryption standardData encryption standard
Data encryption standardVasuki Ramasamy
 
Routing and routing algorithms
Routing and routing algorithmsRouting and routing algorithms
Routing and routing algorithmsBhatt Aadil
 
Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...
Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...
Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...endokayle
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Ridhima Chowdhury
 
KMP Pattern Matching algorithm
KMP Pattern Matching algorithmKMP Pattern Matching algorithm
KMP Pattern Matching algorithmKamal Nayan
 
Public Key Cryptography and RSA algorithm
Public Key Cryptography and RSA algorithmPublic Key Cryptography and RSA algorithm
Public Key Cryptography and RSA algorithmIndra97065
 
EULER AND FERMAT THEOREM
EULER AND FERMAT THEOREMEULER AND FERMAT THEOREM
EULER AND FERMAT THEOREMankita pandey
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocationkiran Patel
 

What's hot (20)

Infix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackInfix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using Stack
 
Array operations
Array operationsArray operations
Array operations
 
Ch03
Ch03Ch03
Ch03
 
Computational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesComputational Complexity: Complexity Classes
Computational Complexity: Complexity Classes
 
DES
DESDES
DES
 
Heap sort
Heap sortHeap sort
Heap sort
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Elliptic curve cryptography
Elliptic curve cryptographyElliptic curve cryptography
Elliptic curve cryptography
 
Rabin karp string matching algorithm
Rabin karp string matching algorithmRabin karp string matching algorithm
Rabin karp string matching algorithm
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
pushdown automata
pushdown automatapushdown automata
pushdown automata
 
Data encryption standard
Data encryption standardData encryption standard
Data encryption standard
 
RSA Algorithm
RSA AlgorithmRSA Algorithm
RSA Algorithm
 
Routing and routing algorithms
Routing and routing algorithmsRouting and routing algorithms
Routing and routing algorithms
 
Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...
Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...
Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)
 
KMP Pattern Matching algorithm
KMP Pattern Matching algorithmKMP Pattern Matching algorithm
KMP Pattern Matching algorithm
 
Public Key Cryptography and RSA algorithm
Public Key Cryptography and RSA algorithmPublic Key Cryptography and RSA algorithm
Public Key Cryptography and RSA algorithm
 
EULER AND FERMAT THEOREM
EULER AND FERMAT THEOREMEULER AND FERMAT THEOREM
EULER AND FERMAT THEOREM
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 

Similar to Amortized

DOC-20230417-WA0013.-7-27.pptx
DOC-20230417-WA0013.-7-27.pptxDOC-20230417-WA0013.-7-27.pptx
DOC-20230417-WA0013.-7-27.pptx03Shrishti
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysisajmalcs
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysisajmalcs
 
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
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 

Similar to Amortized (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 analysis
Amortized analysisAmortized analysis
Amortized analysis
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
 
Bellmon Ford Algorithm
Bellmon Ford AlgorithmBellmon Ford Algorithm
Bellmon Ford Algorithm
 
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
 
AA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxAA_Unit 1_part-I.pptx
AA_Unit 1_part-I.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 analysis
Amortized analysisAmortized analysis
Amortized analysis
 
Aoa amortized analysis
Aoa amortized analysisAoa amortized analysis
Aoa amortized analysis
 
parallel
parallelparallel
parallel
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 

More from 8neutron8

String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,8neutron8
 
Cloud computing by amazon
Cloud computing by amazonCloud computing by amazon
Cloud computing by amazon8neutron8
 
Max flow problem and push relabel algorithem
Max flow problem and push relabel algorithemMax flow problem and push relabel algorithem
Max flow problem and push relabel algorithem8neutron8
 
Mobile generation presentation
Mobile generation presentationMobile generation presentation
Mobile generation presentation8neutron8
 
Cloud computing vs grid computing
Cloud computing vs grid computingCloud computing vs grid computing
Cloud computing vs grid computing8neutron8
 
Mobile os by waqas
Mobile os by waqasMobile os by waqas
Mobile os by waqas8neutron8
 

More from 8neutron8 (8)

String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,
 
Cloud computing by amazon
Cloud computing by amazonCloud computing by amazon
Cloud computing by amazon
 
Max flow problem and push relabel algorithem
Max flow problem and push relabel algorithemMax flow problem and push relabel algorithem
Max flow problem and push relabel algorithem
 
Mobile generation presentation
Mobile generation presentationMobile generation presentation
Mobile generation presentation
 
Cloud computing vs grid computing
Cloud computing vs grid computingCloud computing vs grid computing
Cloud computing vs grid computing
 
Mobile os by waqas
Mobile os by waqasMobile os by waqas
Mobile os by waqas
 
Qos in wlan
Qos in wlanQos in wlan
Qos in wlan
 
QoS in WLAN
QoS in WLANQoS in WLAN
QoS in WLAN
 

Recently uploaded

Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 

Recently uploaded (20)

Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

Amortized

  • 1. Amortized Analysis By Waqas Shehzad Fast NU Pakistan
  • 2. 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!
  • 3. Operations on Data Structures A data structure has a set of operations associated with it. Example: A stack with push(), pop() and MultiPop(k). Often some operations may be slow while others are fast. push() and pop() are fast. MultiPop(k) may be slow. Sometimes the time of a single operation can vary
  • 4. Amortized Analysis Aggregate Method: we determine an upper bound T(n) on the total sequence of n operations. The cost of each will then be T(n)/n. Accounting Method: we overcharge some operations early and use them to as prepaid charge later. Potential Method: we maintain credit as potential energy associated with the structure as a whole.
  • 5. Aggregate Method We show that for n operation, the sequence takes worst case T(n) We use as an example, a stack with a new operation MULTIPOP (S,k) If we consider PUSH and POP to be elementary operations, then Multipop takes O(n) in the worst case.
  • 7. Stack - aggregate analysis Push and Pop are O(1) (to move 1 data element) Multipop is O(min(s, k)) where s is the size of the stack and k the number of elements to pop. Assume a sequence of n Push, Pop and Multipop operations Multipop(S, k) while not empty(S) and k>0 do Pop(S); k:=k-1 end while 7
  • 8. Aggregate Method • Sequence of n push, pop, Multipop operations • Worst-case cost of Multipop is O(n) • Have n operations • Therefore, worst-case cost of sequence is O(n2) However, since an object can be only popped once (whether by pop or multipop) for every time it is pushed, the total number of Pop operations is at most the total number of Push operations (which is at most n)
  • 9. Stack - aggregate analysis A sequence of n Push and Pop operations is therefore O(n) and the amortized cost of each is O(n)/n=O(1)
  • 10. Accounting Method We assign different charges to different operations, with some operations charged more or less than they actually cost. The amount we charge an operation is its amortized cost. When an operation’s amortized cost exceeds its actual cost, the difference is assigned to specific objects in the data structure as credit.
  • 11. Accounting Method Credit can be used later to pay the cost of operations whose actual cost is greater than its amortized cost. The total credit stored must always be non- negative at all times. We must ensure that n n ∑ ci ≤ ∑ ci ˆ i =1 i =1 Amortized cost ĉi
  • 12. Accounting Method: Stack Thing about this: when we push a plate onto a stack, we use $1 to pay actual cost of the push and we leave $1 on the plate. At any point, every plate on the stack has a dollar on top of it. When we execute a pop operation, we charge it nothing and pay its cost with the dollar that is on top of it.
  • 13. A Simple Example: Accounting 3 ops: Push(S,x) Pop(S) Multi-pop(S,k) •Assigned 2 0 0 cost: •Actual cost: 1 1 min(|S|,k)
  • 14. Potential Method Instead of representing prepaid work as credit stored with specific objects, the potential method represents the prepaid work as potential energy than can be released to pay for future operations. Potential is associated with the data structure as a whole rather than with specific objects.
  • 15. Potential Method We start with an initial data structure D0 on which n operations are performed. Let ci be the cost the ith operations and Di be the data structure that results after applying the ith operation to data structure Di-1 A potential function Φ maps Di to a real number Φ(Di) which is the potential associated with Di
  • 16. Applications of amortized analysis Vectors/ tables Disjoint sets Priority queues Heaps, Binomial heaps, Fibonacci heaps Hashing