0% found this document useful (0 votes)
3 views31 pages

Amortized

Amortized analysis averages the time required for a sequence of data-structure operations, ensuring that the average cost remains low despite potentially expensive individual operations. It employs methods such as aggregate analysis, accounting, and potential methods to determine amortized costs, providing guarantees on performance in worst-case scenarios. Examples include analyzing stack operations and binary counters, demonstrating that the average cost per operation can be maintained at O(1).

Uploaded by

Shuhaib Cp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views31 pages

Amortized

Amortized analysis averages the time required for a sequence of data-structure operations, ensuring that the average cost remains low despite potentially expensive individual operations. It employs methods such as aggregate analysis, accounting, and potential methods to determine amortized costs, providing guarantees on performance in worst-case scenarios. Examples include analyzing stack operations and binary counters, demonstrating that the average cost per operation can be maintained at O(1).

Uploaded by

Shuhaib Cp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Amortized analysis

• In amortized analysis, the time required to perform a


sequence of data-structure operations is averaged
over all the operations performed.

• Amortized analysis can be used to show that the


average cost of an operation is small, even though a
single operation within the sequence may be
expensive.

• Amortized analysis guarantees the average


performance of each operation in the worst case.
•Best Case − Minimum time required for program execution.

•Average Case − Average time required for program execution.

•Worst Case − Maximum time required for program execution.


Three Methods of Amortized Analysis

• Aggregate analysis:
– (Total cost of n operations /n)
• Accounting method:
– Assign each type of operation an (different) amortized cost
– overcharge some operations,
– store the overcharge as credit on specific objects,
– then use the credit for compensation for some later operations.
• Potential method:
– Same as accounting method
– But store the credit as “potential energy” and as a whole.
•Aggregate analysis:

The aggregate method, where the total running time


for a sequence of operations is analyzed.
Example : Multipop Stack

• Stack operations:
– PUSH(S,x), O(1)

– POP(S), O(1)

– MULTIPOP(S,k), min(s,k)

• while not STACK-EMPTY(S) and k>0

do POP(S)

k=k-1
• Let us consider a sequence of n PUSH, POP,
MULTIPOP.
– The worst case cost for MULTIPOP in the sequence is
O(n), since the stack size is at most n.
– thus the cost of the sequence is O(n2). Correct, but not
tight.
In fact, although a single MULTIPOP operation can be
expensive, any sequence of n PUSH, POP,
and MULTIPOP operations on an initially empty stack can cost
at most O(n). Why?

Each object can be popped at most once for each time it is


pushed. Therefore, the number of times that POP can be called
on a nonempty stack, including calls within MULTIPOP, is at
most the number of PUSH operations, which is at most n.

Number of POP is most equal to no: of PUSHs which is atmost n.


For any value of n, any sequence of n PUSH, POP,
and MULTIPOP operations takes a total of O(n) time.

Thus the average cost of an operation is O(n)/n = O(1).

Amortized cost in aggregate analysis is defined to be average cost


Another example: increasing a binary counter

Binary counter of length k, A[0..k-1] of bit array.


INCREMENT(A)
1. i🡨0
2. while i<k and A[i]=1
3. do A[i]🡨0 (flip, reset)
4. i🡨i+1
5. if i<k
6. then A[i]🡨1 (flip, set)
Analysis of INCREMENT(A)

• Cursory analysis:
– A single execution of INCREMENT takes O(k) in the
worst case (when A contains all 1s)
– So a sequence of n executions takes O(nk) in worst case
(suppose initial counter is 0).
– This bound is correct, but not tight.
• The tight bound is O(n) for n executions.
Amortized (Aggregate) Analysis of INCREMENT(A)

The running time determined by flips but


Observation: not all bits flip each time INCREMENT is
called.

A[0] flips every time, total n times.


A[1] flips every other time, ⎣n/2⎦ times.
A[2] flips every forth time, ⎣n/4⎦ times.
….
for i=0,1,…,k-1, A[i] flips ⎣n/2i⎦ times.
Amortized Analysis of INCREMENT(A)

• Thus the worst case running time is O(n) for a


sequence of n INCREMENTs.
• So the amortized cost per operation is O(1).
Amortized Analysis : Accounting Method

• Idea:
– Assign differing charges to different operations.
– The amount of the charge is called amortized cost.
– amortized cost is more or less than actual cost.
– When amortized cost > actual cost, the difference is saved in
specific objects as credits.
– The credits can be used by later operations whose amortized
cost < actual cost.
• As a comparison, in aggregate analysis, all operations
have same amortized costs.
Accounting Method (cont.)

Since we want to show the average cost per operation


is small using amortized cost, we need the total
amortized cost is an upper bound of total actual cost holds
for all sequences of operations.

Ci = actual cost ,
ci’ = Amortized cost

– Total credits is ∑i=1n ci' - ∑i=1n ci , which should be


nonnegative,
Accounting Method: Stack Operations

• Actual costs:
– PUSH :1
– POP :1
– MULTIPOP: min(s,k).

• Let assign the following amortized costs:


– PUSH:2
– POP: 0
– MULTIPOP: 0.
• The total amortized cost for n PUSH, POP, MULTIPOP is
O(n), thus O(1) for average amortized cost for each
operation.

• Conditions hold: total amortized cost ≥total actual cost,


and amount of credits never becomes negative.
Accounting method: Binary counter

• Let $1 represent each unit of cost (i.e., the flip of one bit).

• Charge an amortized cost of $2 to set a bit to 1.

• Whenever a bit is set, use $1 to pay the actual cost, and


store another $1 on the bit as credit.

• When a bit is reset, the stored $1 pays the cost.


• At any point, a 1 in the counter stores $1, the number
of 1’s is never negative, so is the total credits.
• At most one bit is set in each operation, so the
amortized cost of an operation is at most $2.
• Thus, total amortized cost of n operations is O(n), and
average is O(1).
The Potential Method
• The potential method is similar to the accounting method.
However, instead of thinking about the analysis in terms of cost
and credit, the potential method thinks of work already done
as potential energy that can pay for later operations.

• Unlike the accounting method, however, potential energy is


associated with the data structure as a whole, not with
individual operations.
The potential method works as follows:

• It starts with an initial data structure, D0


• Then n operations are performed, turning the initial data structure into D1 , D2 , D3 ,…….. Dn
• ci will be the cost associated with the ith operation, and
• Di is the data structure resulting from the ith operation.
• - Amortized cost of ith operation
Φ is the potential function which maps the data structure D to a number Φ(D) the
potential associated with that data structure. The amortized
cost of the ith operation is defined by
So, that means that over n operations, the total amortized cost will be

Because this is a telescoping sum (What do you mean by telescoping sum?


A sum in which subsequent terms cancel each other, leaving only initial and final
terms.), it equals
Potential method: stack operation

• Potential for a stack is the number of objects in the stack(s).


• So Φ(D0)=0, and Φ(Di) ≥0
• Amortized cost of stack operations:

– PUSH:
•Potential change: Φ(Di)- Φ(Di-1) =(s+1)-s =1.
•Amortized cost: ci' = ci + Φ(Di) - Φ(Di-1)=1+1=2.
– POP:
•Potential change: Φ(Di)- Φ(Di-1) =(s-1) –s= -1.
•Amortized cost: ci' = ci + Φ(Di) - Φ(Di-1)=1+(-1)=0.

– MULTIPOP(S,k): k'=min(s,k)
•Potential change: Φ(Di)- Φ(Di-1) = (s–k’) - s = -k’.
•Amortized cost: ci' = ci + Φ(Di) - Φ(Di-1)=k'+(-k')=0.

The amortized cost for each of the three operations is O(1) and the total
amortized cost for a sequence of n operations is O(n).
Potential method: binary counter
• Define the potential of the counter after the ith INCREMENT is Φ
(Di) =bi, the number of 1’s. clearly, Φ(Di)≥0.
• Let us compute amortized cost of an operation
– Suppose the ith operation resets ti bits.
– Actual cost ci of the operation is at most ti +1.
– If bi=0, then the ith operation resets all k bits, so bi-1=ti=k.
– If bi>0, then bi=bi-1-ti+1
– In either case, bi≤bi-1-ti+1.
– So potential change is Φ(Di) - Φ(Di-1) ≤bi-1-ti+1-bi-1=1-ti.
– So amortized cost is: ci' = ci + Φ(Di) - Φ(Di-1) ≤ ti +1+1-ti=2.
• The total amortized cost of n operations is O(n).
• Thus worst case cost is O(n).
Potential method: binary counter

• Define the potential of the counter after the ith INCREMENT


is Φ(Di) =bi, the number of 1’s. clearly, Φ(Di)≥0.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy