平摊分析
之前的笔记中有相关的记录。
Amortized Analysis
Idea: Even when expensive operations must be performed, it is often possible to get away with performing them rarely, so that the average cost per operation is not so high.
Amortized analysis is different from what is commonly referred to as average case analysis, because it does not make any assumption about the distribution of the data values, whereas average case analysis assumes the data are not "bad".
That is, amortized analysis is a worst case analysis, but for a sequence of operations, rather than for individual operations.
Aggregate method for Amortized Analysis
One assumes that there is no need to distinguish between the different operations on the data structure.
We just use aggregate method: Add up the cost of all the operations and then divide by the number of operations.
Let for some constant . Then maximum copying cost of insertions in CircularArray
is .
Therefore, the aggregate cost is .
Definition
Note: Different operations may have different amortized costs.
Operation has amortized cost if for every , the total cost of any operations is at most , where is the size of the data structure when applying the -th operation.
Consider a sequence operations:
- is the actual cost of the -th operation
- is the amortized cost of the -th operation
For the amortized cost to be valid:
- Total cost of operations
- Average cost of operations
Example: Consider CircularArray
implementation of Queue
It is not enough to prove using just 6 operations.
Amortized Analysis Techniques
The Accounting Method(记账法)
Imagine you have a bank account with initial balance .
For the -th operation, you spend money:
- Recall that the actual cost of the -th operation is
- If , pay for the operation and deposit into .
- If , pay for the operation and withdraw from .
Amortized analysis valid if
always for all .
Example: Queue
with CircularArray
if the -th operation is Insert
and if the -th operation is Remove
.
- Goal: Prove for any operations.
- Strategy: account always non-negative via induction on .
- Basis: Prior to 1st operation, account balance is .
- Hypothesis: Prior to -th operation, account balance is always non-negative.
Inductive Step: Consider the -th operation
- If it's
Remove
, then we make no change to account value. - If it's Insert without expansion, we add to account value.
- If it's
Insert
with expansion. Assume expand from to , then we need to withdraw value. - Analysisstep:
- Last expand must be from to .
- Since last expand, each
Insert
adds and eachRemove
makes no change. - Since last expand, there are at least
Insert
operation. - Account balance is at least .
- After the -th operation, account balance is at least .
- Account balance is always non-negative.
Example: Binary Counter
The disadvantage of the accounting method is that it is not always easy to find the right accounting scheme. The guess is always constant.
The Potential Method(势能法)
Design a potential function that maps data structure status to real values:
- : Initial potential of the data structure, usually set to .
- : Potential of the data structure after -th operation.
Define
Amortized analysis valid if
for all .
Potential is like the balance in account in Counting Method.
- Potential slowly accumulates during cheap operations (deposit).
- Potential is used to pay for expensive operations (withdraw).
Example: Binary Counter
The last line is because, in one operation, it will turn to at most time.