



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Amortized Analysis
In an amortized analysis, we consider the to- tal time of a sequence of operations. Even if a single operation is O(f (n)), the average of n operations might be o(f (n)).
Examples: Sequence of Heapify calls from Build-Heap. Sequence of calls to Tree-Successor. Repeatedly incrementing a bit string. Repeated insertions into a dynamic array. Make-Set, Union, and Find-Set.
Aggregate Method: Directly analyze total time.
Accounting Method: Assign an amortized cost to each operation. Show total amortized cost ≥ total time.
Potential Method: Specify an initial charge (potential). Amortized op cost = time + change in potential. Total amortized cost = total time + change in potential. Show change ≥ 0 or potential ≥ 0 at all times.
Incrementing a Bit String
A is a bit string. Analyze number of bit flips.
Increment(A[0... m − 1]) i ← 0 while i < m and A[i] = 1 do A[i] ← 0 i ← i + 1 if i < m then A[i] ← 1
Aggregate Method: Assume n increments starting from all 0s. A[0] flips every increment for n flips. A[1] flips every 2nd time for ≤ n/2 flips. A[2] flips every 4th time for ≤ n/4 flips. A[i] flips every 2ith time for ≤ n/ 2 i^ flips.
Number of flips ≤ n +
n 2
n 4
= n
∞ Σ i=
2 i = 2n ∈ O(n)
Dynamic Arrays
Assume num[A] is number of elements in A. Assume num[A] = 0 and size[A] = 1 initially.
Array-Insert(A, x) if num[A] = size[A] then reallocate A with length 2 · size[A] size[A] ← 2 · size[A] num[A] ← num[A] + 1 A[num[A]] ← x
Analyze number of insertions + amount of copy- ing during reallocations.
Aggregate Method:
Assume n insertions starting from 0 elts.
Assume num[A] time for reallocating A.
Assume 1 unit time for rest of Array-Insert.
Reallocate when num[A] is a power of 2.
blg nc Σ i=
2 i^ ≤ n
∞ Σ i=
2 i^
= 2n
So insertion + reallocation time ≤ n + 2n = 3n
Accounting Method:
Assign an amortized cost of 3 units/insertion.
Use one unit immediately for inserting A[j].
Save two units for future reallocation: one for A[j] and the other for A[j − s/2], where s = size[A].
When reallocating, all elts are accounted for, so the total cost of 3n is ≥ time units.
Suppose deletions are allowed.
size[A] ← size[A]/2 when array is 1/4 full.
Assign an amortized cost of 2 units/deletion.
Use one unit immediately for deleting A[j]. Save one unit for reallocation of A[j − s/4], where s = size[A].
If space is at a premium, then use a expansion/contraction factor < 2. In the analysis, each operation will cost more.