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

I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...vikas dhakane
 
Randomizing quicksort algorith with example
Randomizing quicksort algorith with exampleRandomizing quicksort algorith with example
Randomizing quicksort algorith with examplemaamir farooq
 
backtracking algorithms of ada
backtracking algorithms of adabacktracking algorithms of ada
backtracking algorithms of adaSahil Kumar
 
Multi armed bandit
Multi armed banditMulti armed bandit
Multi armed banditJie-Han Chen
 
pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"
pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"
pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"YeChan(Paul) Kim
 
Network telnet ssh
Network telnet sshNetwork telnet ssh
Network telnet sshStefan Fodor
 
KNN Algorithm using C++
KNN Algorithm using C++KNN Algorithm using C++
KNN Algorithm using C++Afraz Khan
 
Attacks in MANET
Attacks in MANETAttacks in MANET
Attacks in MANETSunita Sahu
 
AI - Introduction to Bellman Equations
AI - Introduction to Bellman EquationsAI - Introduction to Bellman Equations
AI - Introduction to Bellman EquationsAndrew Ferlitsch
 
All-Reduce and Prefix-Sum Operations
All-Reduce and Prefix-Sum Operations All-Reduce and Prefix-Sum Operations
All-Reduce and Prefix-Sum Operations Syed Zaid Irshad
 
Reinforcement learning, Q-Learning
Reinforcement learning, Q-LearningReinforcement learning, Q-Learning
Reinforcement learning, Q-LearningKuppusamy P
 
03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic AnalysisAndres Mendez-Vazquez
 
ML_Unit_1_Part_C
ML_Unit_1_Part_CML_Unit_1_Part_C
ML_Unit_1_Part_CSrimatre K
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and ComplexityRajandeep Gill
 

What's hot (20)

I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
I.INFORMED SEARCH IN ARTIFICIAL INTELLIGENCE II. HEURISTIC FUNCTION IN AI III...
 
Randomizing quicksort algorith with example
Randomizing quicksort algorith with exampleRandomizing quicksort algorith with example
Randomizing quicksort algorith with example
 
backtracking algorithms of ada
backtracking algorithms of adabacktracking algorithms of ada
backtracking algorithms of ada
 
Multi armed bandit
Multi armed banditMulti armed bandit
Multi armed bandit
 
Fuzzy neural networks
Fuzzy neural networksFuzzy neural networks
Fuzzy neural networks
 
pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"
pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"
pycon2018 "RL Adventure : DQN 부터 Rainbow DQN까지"
 
AI: Logic in AI
AI: Logic in AIAI: Logic in AI
AI: Logic in AI
 
Comet Cloud
Comet CloudComet Cloud
Comet Cloud
 
Network telnet ssh
Network telnet sshNetwork telnet ssh
Network telnet ssh
 
KNN Algorithm using C++
KNN Algorithm using C++KNN Algorithm using C++
KNN Algorithm using C++
 
Forward checking
Forward checkingForward checking
Forward checking
 
Attacks in MANET
Attacks in MANETAttacks in MANET
Attacks in MANET
 
convex hull
convex hullconvex hull
convex hull
 
AI - Introduction to Bellman Equations
AI - Introduction to Bellman EquationsAI - Introduction to Bellman Equations
AI - Introduction to Bellman Equations
 
All-Reduce and Prefix-Sum Operations
All-Reduce and Prefix-Sum Operations All-Reduce and Prefix-Sum Operations
All-Reduce and Prefix-Sum Operations
 
Reinforcement learning, Q-Learning
Reinforcement learning, Q-LearningReinforcement learning, Q-Learning
Reinforcement learning, Q-Learning
 
03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis
 
ML_Unit_1_Part_C
ML_Unit_1_Part_CML_Unit_1_Part_C
ML_Unit_1_Part_C
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Hebb network
Hebb networkHebb network
Hebb network
 

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 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
 
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
 
09. amortized analysis
09. amortized analysis09. amortized analysis
09. 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

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Recently uploaded (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

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