Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Amortized Analysis - II

Potential Method

Instructor: Ashok Singh Sairam


Amortized Analysis: Review
• Consider not just one but a sequence of operations on
a given data structure.
▪ Average cost over a sequence of operations.
• Probabilistic analysis:
– Average case running time: average over all possible inputs
for one algorithm (operation).
• Amortized analysis:
– No involvement of probability
– Average performance on a sequence of operations, even
some operation is expensive.
– Guarantee average performance of each operation among
the sequence in worst case

MA512: Data Structures and Algorithms


2
Amortized Analysis: Review
• 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.
– for any sequence of operations the amortized cost should be
non-negative

MA512: Data Structures and Algorithms


3
Exercise - 1
• A sequence of n operations is performed on a data
structure. The ith operation costs i if i is an exact
power of 2, and 1 otherwise. Use aggregate
analysis to determine the amortized cost per
operation.

MA512: Data Structures and Algorithms


4
Exercise - 2
• A sequence of n operations is performed on a data
structure. The ith operation costs i if i is an exact
power of 2, and 1 otherwise. Use accounting
method to determine the amortized cost per
operation.

MA512: Data Structures and Algorithms


5
The Potential Method
• Accounting method represents prepaid work as
credit stored with specific objects in data structure
• Potential method represents the prepaid work as
potential energy or just potential
• Potential associated with the data structure as a
whole rather than with specific objects within the
data structure

MA512: Data Structures and Algorithms


6
Symbols use
D0 : Initial data structure on which we perform n opns
Ci: the actual cost of the i-th operation
Di: data structure that results after applying i-th
operation to Di−1
 : potential function that maps each data structure Di
to a real number  (Di)
 (Di): the potential associated with data structure Di
𝐶෡𝑖 : amortized cost of the i-th operation w.r.t. function 

MA512: Data Structures and Algorithms


7
Amortized cost
• Amortized cost of the i-th operation
Cˆ i = Ci +  ( Di ) −  ( Di −1 )
actual cost increase in potential due to the operation
• The total amortized cost of n operations is
n n

 i  i
Cˆ = (C +  ( D ) −  ( D ))
i =1
i
i =1
i −1

n
=  Ci +  ( Dn ) −  ( D0 ) (17.3)
i =1

MA512: Data Structures and Algorithms


8
No negative potential
• We require  (Di)   (D0) then total amortized cost is
an upper bound on the total actual cost
▪ We ensure that we pay in advance (as in accounting
method)

• That is,  (Dn)   (D0) should hold for all possible n


since, in practice, we do not always know n in
advance

MA512: Data Structures and Algorithms


9
The potential method
• If  (Di) − (Di−1) > 0, then the amortized cost Ĉi represents
• an overcharge to the i-th operation and
• the potential of the data structure increases

• If  (Di) −  (Di−1) < 0, then the amortized cost Ĉi represents


• an undercharge to the i-th operation and
• the actual cost of operation paid by decrease in potential

• Different potential functions may yield different amortized


costs which are still upper bounds for the actual costs
▪ The best potential fn. to use depends on the desired time
bounds
MA512: Data Structures and Algorithms
10
The Potential Method:
Stack Operations
• Define  (S)=| S |, the number of objects in the stack
• For the initial empty stack, we have  (D0) = 0
• Since |S|  0, stack Di that results after ith operation
has nonnegative potential for all i, that is
 (Di)  0 =  (D0) for all i
• total amortized cost is an upper bound on total actual cost
• Let us compute the amortized costs of stack
operations where ith operation is performed on a
stack with s objects

MA512: Data Structures and Algorithms


11
The Potential Method:
Stack Operations – contd.
PUSH (S):  (Di) −  (Di−1) = (s +1) − (s) = 1
Cˆi = Ci +  (Di) − (Di−1) = 1 + 1 = 2

MULTIPOP(S, k):  (Di) − (Di−1) = − k' = − min{s, k}


Cˆi = Ci +  (Di) − (Di−1) = k' − k' = 0
POP (S):  (Di) − (Di−1) = − 1
Cˆi = Ci +  (Di) − (Di−1) = 1 − 1 = 0
• Amortized cost of each operation is O(1), thus total
amortized cost of a sequence of n operations is O(n)

MA512: Data Structures and Algorithms


12
The Potential Method:
Incrementing a Binary Counter
• Define  (Di) =bi, number of 1s in counter after the
i-th operation
• We want to compute amortized cost of INCREMENT
opn. wrt 
• Suppose the i-th INCREMENT resets ti bits
▪ Actual cost Ci will be at most ti +1 (since other than
resetting, at most one bit may be set)

MA512: Data Structures and Algorithms


13
The Potential Method:
Incrementing a Binary Counter
• The number of 1s in the counter after the i-th
operation is
▪ If 𝑏𝑖 = 0, then i-th opn reset all bits; 𝑏𝑖−1 = 𝑡𝑖 = 𝑘
▪ If 𝑏𝑖 > 0, then 𝑏𝑖 = 𝑏𝑖−1 − 𝑡𝑖 + 1
• In either case, 𝑏𝑖 ≤ 𝑏𝑖−1 − 𝑡𝑖 + 1
𝜙 𝐷𝑖 − 𝜙 𝐷𝑖−1 ≤ 𝑏𝑖−1 − 𝑡𝑖 + 1-𝑏𝑖−1 = 1 − t i

• The amortized cost is therefore


𝐶෡𝑖 = Ci +  (Di) − (Di−1)  (ti +1) + (1− ti) = 2

MA512: Data Structures and Algorithms


14
• Case 1: Counter starts at zero
 (D0) = 0,
• Since  (Di)  0 for all i the total amortized cost is
an upper bound on the total actual cost
• Hence, the worst-case cost of n operations is O(n)

MA512: Data Structures and Algorithms


15
• Case 2: counter does not start at zero, i.e., b00
• Then, after n INCREMENT operations the number of
1s is bn, where 0  b0, bn  k
• Rewriting Eq. 17.3
 Ci =  𝐶መ𝑖 −  (Dn) +  (D0)   2−bn+b0
 2n−bn+b0
• Since b0 k, if we execute at least n =  (k)
INCREMENT operations the total actual cost is O(n)
• No matter what initial value the counter contains
MA512: Data Structures and Algorithms
16
Exercise
• A sequence of n operations is performed on a data
structure. The ith operation costs i if i is an exact
power of 2, and 1 otherwise. Use potential
method of analysis to determine the amortized
cost per operation.

MA512: Data Structures and Algorithms


17

You might also like