[ACE] Amortized analysis

Amortized analysis


Why talking about amortized analysis

When we are learning “Vector” in ACE class, we came up with a growable array-based vector.
We want to know what if we execute “push” operation, but the array is full. Instead of throwing a exception, we would like to replace the array with a larger one
Algorithm push(o)
    if t = s.length -1
then 
    A <- new array of size ...
    for i <- 0 to t do
    A[i] <- S[i]
    t <- t+1
    A[t+1] <- o
    garbage collection S
From pseudo-code above, what should be the size of the new array?
There are two simple strategy you can came up with quickly
• incremental strategy: increase size by a constant c
• doubling strategy: double the size

What is the common analysis for this question?

Suppose some individual operation (such as ‘push’) takes time T in the worst-case.
So for the total s operations, the worst-case time should be sT(upper bound)
• however, such an upper-bound might not ever occur. Because there will never be always worst case
Suppose do a sequence of operations, and there are s such operations taking time Ts
The time Ts might well be o(sT) even in the worst-case(little-o)

So what is the cost for each operation in this case(if we apply doubling strategy)?

we do every insert operation step by step like the image.
Do it step by step
we can draw a table regarding the insert sequence i, the size of array size(i) and the cost of ith insert C(i)
Note that C(i) = i, if i-1 is the power of 2
or C(i) = 1 ,otherwise
enter image description here
If we add all the C(i) together, we can get the cost of n inserts, and each C(i) can be 1 or 1 add a number which is 2 to the power of something.
enter image description here
From the analysis, we found it will be less than 3n which is O(n), so each operations takes O(1) time.
It is amortized analysis, which is not simply times the worst case with operation number but analyze in detail

So what is the official definition?

Sequence!!

Amortized analysis is a technique for analyzing an algorithm’s running time. It is often appropriate when one is interested in understanding asymptotic behavior over sequences of operations.

Methods

There are generally three methods for performing amortized analysis: the aggregate method, the accounting method, and the potential method. All of these give the same answers, and their usage difference is primarily circumstantial and due to individual preference. In CLRS, it introduce three method but in class we just cover aggregate method.

Why is amortised analysis different from the average case analysis?

• (long) real sequence of dependent operations
• vs.
• Set of (possibly independent) operations
Because when analyze amortized analysis, we consider the relationship and impact between operation.

Incremental Strategy Analysis

We have cover the doubling strategy analysis, now we finish the incremental strategy analysis.
• Take c=3, with start capacity of 3, then a sequence of pushes might have costs for each push as follows:
1,1,1,3+1,1,1,6+1,1,1,9+1,1,1,12+1,1,1, 15+1,1,1,18+1,…
enter image description here

Is it says doubling are better than incremental?

Not really, though doubling has lower amortized complexity, it might need more space. The space will be waste and might not be use.

评论

此博客中的热门博文

[MLE] Linear Classification

[AIM] MetaHeuristics

[CS231] Neural Networks