Professional Documents
Culture Documents
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.
2
Amortized Analysis
3
Amortized Analysis
1. Aggregate Method
4
Amortized Analysis
3 ops:
Multi-
Push(S,x) Pop(S)
pop(S,k)
Worst-case O(min(|S|,k)
O(1) O(1)
cost: = O(n)
Observations
Each object can be popped only once per time that it’s
pushed
Have <= n pushes => <= n pops, including those in
Multipop
Therefore total cost = O(n)
Average over n operations => O(1) per operation on
average
6
Amortized Analysis
2. Accounting Method
Charge i th operation a fictitious amortized cost ĉi,
where $1 pays for 1 unit of work (i.e., time).
Assign different charges (amortized cost ) to different
operations
Some are charged more than actual cost
Some are charged less
This fee is consumed to perform the operation.
Any amount not immediately consumed is stored in the
bank for use by subsequent operations.
The bank balance (the credit) must not go negative!
n n
We must ensure that
for all n.
∑ci ≤ ∑
cˆi
i =1 i =1
7
Amortized Analysis
….. Accounting Method: Stack Example
3 ops:
9
Amortized Analysis
Introduction
k-bit Binary Counter: A[0..k−1]
−1 A[i ] ⋅ 2i
x = ∑ik=0
INCREMENT(A)
1. i ← 0
2. while i < length[A] and A[i] = 1
3. do A[i] ← 0 ⊳ reset a bit
4. i←i+1
5. if i < length[A]
6. then A[i] ← 1 ⊳ set a bit
10
Amortized Analysis
11
Amortized Analysis
lg n
n
Cost of n increments= ∑ i
i =1 2
∞
1
< n ∑ i = 2n
i =1 2
= Θ(n)
13
Amortized Analysis
Example:
0 0 0 1$1 0 1$1 0
14
Amortized Analysis
INCREMENT(A)
1. i ← 0
2. while i < length[A] and A[i] = 1
3. do A[i] ← 0 ⊳ reset a bit
4. i←i+1
5. if i < length[A]
6. then A[i] ← 1 ⊳ set a bit
When Incrementing,
Amortized cost for line 3 = $0
Amortized cost for line 6 = $2
15
Amortized Analysis
Aggregate Method :
First analyze entire sequence
Then calculate amortized cost per operation
16
Amortized Analysis
3. Potential Method
IDEA: View the bank account as the
potential energy (as in physics) of the
dynamic set.
FRAMEWORK:
Start with an initial data structure D0.
Operation i transforms Di–1 to Di.
The cost of operation i is ci.
Define a potential function Φ : {Di} → R,
such that Φ(D0 ) = 0 and Φ(Di ) ≥ 0 for all i.
The amortized cost ĉi with respect to Φ is
defined to be ĉi = ci + Φ(Di) – Φ(Di–1).
17
Amortized Analysis
18
Amortized Analysis
19
Amortized Analysis
….. Potential Method
The total amortized cost of n operations is
n n
∑cˆ = ∑( c
i =1
i
i =1
i + Φ( Di ) − Φ( Di −1 ) )
20
Amortized Analysis
0 0 0 1 0 1 0
(~ 0 0 0 1$1 0 1$1 0 Accounting method)
22
Amortized Analysis
23
Amortized Analysis
24
Amortized Analysis
25
Amortized Analysis
1 / 2 ≤ α(T) ≤ 1
27
Amortized Analysis
Table Insert:
TABLE-INSERT (T, x)
1. if size[T] = 0 then
2. allocate table[T] with 1 slot table[T] : pointer to block
3. size[T] ← 1 of table storage
4. if num[T] = size[T] then num[T] : number of
5. allocate new-table with 2.size[T] slots
6. copy all items in table[T] into new-table items in the table
7. free table[T] size[T] : total number of
8. table[T] ← new-table[T] slots in the table
9. size[T] ← 2.size[T]
10. insert x into table[T] Initially, table is empty,
11. num[T] ← num[T] + 1 so num[T] = size[T] = 0
12. end
1 / 2 ≤ α(T) ≤ 1
28
Amortized Analysis
27
Amortized Analysis
28
Amortized Analysis
29
Amortized Analysis
30
Amortized Analysis
31
Amortized Analysis
32
Amortized Analysis
33
Amortized Analysis
1. Aggregate Method
i if i is an exact power of 2
ci =
1 otherwise
38
Amortized Analysis
…Aggregate Method
∑c
i =1
i = n+ ∑2
j =0
j
< n + ∑ 2 = n + 2n = 3n
j =0
j
i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
ci 1 2 3 1 5 1 1 1 9 1 1 1 1 1 1 1 17 1 1 1 ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
Expansion 1 2 4 8 16
cost
40
Amortized Analysis
41
Amortized Analysis
1st insertion
(a) $1 for
(c)
(b) insertion
2nd insertion
42
Amortized Analysis
M/2th Insertion
44
Amortized Analysis
Definition of Φ:
Since the table is at least half full
(i.e. num[T] ≥ size[T] / 2)
Φ(T) is always nonnegative.
Thus, the sum of the amortized cost of n TABLE-INSERT
operations is an upper bound on the sum of the actual
costs.
45
Amortized Analysis
47
Amortized Analysis
……. Potential Method: Dynamic Tables
Adding Delete Operation:
TABLE-DELETE: Remove the specified item from the
table. It is often desirable to contract the table.
In table contraction, we would like to preserve two
properties
Load factor of dynamic table is bounded below by a
constant
Amortized cost of a table operation is bounded above
by a constant
We assume that the cost can be measured in terms of
elementary insertions and deletions
48
Amortized Analysis
……. Potential Method: Dynamic Tables
A natural strategy for expansion and contraction
• Double the table size when an item is inserted into a
full table
• Halve the size when a deletion would cause
α (T )< 1 / 2
1
This strategy guarantees ≤ α(T ) ≤1
2
50
Amortized Analysis
……. Potential Method: Dynamic Tables
Worst-Case for α(T) ≥ ½
Example: n=16
Amortized Analysis 52
Amortized Analysis
……. Potential Method: Dynamic Tables
Improving Expansion – Contraction
53
Amortized Analysis
……. Potential Method: Dynamic Tables
Improving Expansion – Contraction
Hence after an expansion, α(T) = ½ , thus at least
half of the items in the table must be deleted
before a contraction can occur.
Similarly, after a contraction α(T) = ½ , thus the
number of items in the table must be doubled by
insertions before an expansion can occur.
54
Amortized Analysis
……. Potential Method: Dynamic Tables
Define the potential function as follows
• Φ(T) = 0 immediately after an expansion or
contraction
• Recall that, α(T) = ½ immediately after and
expansion or contraction,
therefore the potential should build up to num[T]
as α(T) increases to 1 or decreases to ¼
• So that the next expansion or contraction can be
paid by the potential.
55
Amortized Analysis
……. Potential Method: Dynamic Tables
Description of New Φ: One such Φ is
1
2num[T ] − size[T ] if α (T) ≥
Φ(T ) = 2
size[T] 1
− num[T ] if α (T) <
2 2
• Φ(T) = 0 when α = ½
• Φ(T) = num[T] when α = ¼
Operations are:
– TABLE-INSERT
– TABLE-DELETE
57
Amortized Analysis
……. Potential Method: Dynamic Tables
Table Insert:
αi-1 ≥ ½
Analysis is identical to that for table expansion so ĉi = 3.
αi-1 < ½ and αi < ½
Expansion may not occur (ĉi = 1 , sizei = sizei-1 )
58
Amortized Analysis
……. Potential Method: Dynamic Tables
Table Insert:
αi-1 < ½ but αi ≥ ½
59
Amortized Analysis
……. Potential Method: Dynamic Tables
Table Delete:
αi-1 < ½ (No contraction) :sizei = sizei-1
60
Amortized Analysis
….. Dynamic Tables
25
Amortized Analysis
62
Amortized Analysis