B505 Lec.7 AmortizedAnalysis II
B505 Lec.7 AmortizedAnalysis II
CSCI-B505 / INFO-I500
Lecture 7.
Amortized Analysis - 2
M. Oguzhan Kulekci
• Dynamic Array Allocation
• Amortized Dictionary Data Structure
Dynamic Arrays
• At the end we will see that, theoretically it is no di erent then static arrays !?
fi
ff
Dynamic Array Allocation
Main Question: What to do when array is full, in other words, all cells are occupied,
and we need to add a new element to this full array.
13
1 2 3 4 5 6 7 8 9 10 11 12
2
• E ort per item is O(n )/n = O(n)
1…k
Move k
items
• Not good, when compared to O(1)
1…k k+1 … 2k in the regular static array.
Move 2k
items 1…k k+1 … 2k 2k+1 … 3k
n
m=⌊ ⌋
Move mk k
items
1…k k+1 … 2k …………. mk+1 … n
k ⋅ m ⋅ (m − 1) 2 2
k(1 + 2 + … + (m − 1)) = ∈ O(k ⋅ m ) → O(n )
2
ff
fi
Dynamic Array Allocation
Let’s consider doubling the current size each time we need to resize.
k
2 n 2k+1
k
k
(1 + 2 + … + 2 ) = 2k+1 k
− 1 ∈ O(2 ) → O(n) k = ⌊log n⌋ → 2 ≈ n
ff
Dynamic Array Allocation
th
Aggregate analysis
ci is the cost of i insertion to the array
k
• If i = 2 + 1, for some k>0, then ci = i.
• Else, ci = 1.
n ⌊log n⌋
j
∑ ∑
ci ≤ n + 2
i=1 j=0
< n + 2n
< 3n
3n
• The cost per item is less than = 3
n
⌊log n⌋
• Therefore, O(1).
Dynamic Array Allocation
Accounting Method
∀i, ϕ(Di) ≥ 0, since after each resize capacity is 2 times the number of elements.
∀i, ϕ(Di) ≥ 0, since after each resize capacity is 2 times the number of elements.
A0 :
A1 :
A2 :
A3 :
Amortized Dictionary Data Structure
INSERTING a new key
• Put new element into array H of size 1.
Merging two sorted list, each with size • i=0
ℓ requires less than 2ℓ comparisons ! • Check Ai. If empty, copy H to Ai and stop. Else,
H ← merge(Ai, H) and i ← i + 1 and repeat.
2 5 9 2 5 9 2 5 9 2 5 9
4 7 8 4 7 8 4 7 8 4 7 8
2 4 5 7 8 9
2 2 4 2 4 5
Amortized Dictionary Data Structure
INSERTING a new key
k
• Worst case: We visit and merge all arrays in the dictionary, e.g. n = 2 − 1 elements in
k
the dictionary for some k, and we are adding the 2 th element
• What will be the cost of this worst case?
• Merging two sorted list, each with size ℓ requires less than 2ℓ comparisons !
k k+1
• Therefore, C = 2 + 4 + 8 + . . . . + 2 = 2 − 1. Since k ∈ O(log n), C ∈ O(n).
This is exactly the same with the binary counter amortized analysis with one
th k
di erence as the cost of ipping k bit is 2 instead of a constant 1 unit.
ff
fl
Amortized Dictionary Data Structure
DELETING a key
i
• Assume we will be deleting an item from the array Ai that includes 2 elements.
i−1 i−1 i
• Split Ai into small arrays of length 1,2,4,…,2 . Notice that 1 + 2 + 4 + … + 2 = 2 − 1,
which is exactly the number of remaining elements in Ai. Delete all items from Ai.
• For each of these small arrays, insert it into the dictionary again. Insert operation starts with
the corresponding list length, i.e., small array of size 1, start with A0, size 2 start with A1, and
continue accordingly.
There can be at most log n small arrays after deleting an element. The amortized cost of insertion
process is O(log n) as we showed previously. So the cost of deleting an element in the worst
2
case is O(log n) with the proposed method.
There might be other ways of deletion as well ?
Reading assignment
• Read chapter 17 Amortized Analysis from Cormen and also related chapters
from other text books or resources on the internet.