Amort
Amort
Time required to perform a sequence of data-structure operations is averaged over all operations performed. Can be used to show that average cost is small. It is dierent from average case analysis since it does not use probabilities.
Amortised Analysis
A[i] 2i .
l[A] = length of A.
Amortised Analysis
Amortised Analysis
Analysis
A single exec. of INCREMENT takes O(k) worst case time. A sequence of n increments would take time O(nk). We can tighten our analysis to yield a worst case of O(n) op. A[0] is ipped every time we call INCREMENT. A[1] is only ipped every other time. A[2] is ipped n/4 times. In general, for i = 0, 1, . . . , lg n , bit A[i] ips n/2i times in a sequence of n increments. For i > lg n , bit A[i] never ips at all. The total number of ips is
lg n i=0 n 2i
<n
1 i=0 2i
= 2n.
Amortised Analysis
Accounting method
We assign dierent charges to dierent operations. Some operations are charged more than and others less than they actually cost. The amount we charge an operation is called amortised cost. When the amortised cost of an operation exceeds the real cost, the dierence is assigned to specic objects in the data structure, credit The credit can be used later for another operation. The amortized cost function has to be chosen very carefully! The amortised cost of a sequence of operations must be an upper bound on the total cost of the sequence of operations, also in the worst case.
Amortised Analysis 5
n i=1 ci .
The total credit stored in the data structure is the dierence between the total amortised cost and the total actual cost:
n n
ci
i=1 i=1
ci .
Amortised Analysis
Analysis
We charge an amortised cost of $2 to set a bit to one. When a bit is set, we use $1 out of the $2 to pay for the setting. We place the other dollar on the bit as credit to be used later when we ip it back to zero. At any point, every one in the counter has a dollar on it. Thus, we need not charge anything to reset a bit to zero. The cost of resetting a bit in the while loop is paid for by the saved dollars paid for by the bits that are reset. At most one bit is set in every round of the loop. Hence, the amortised cost of an INCREMENT operation is $2. n INCREMENT operations cost O(n).
Amortised Analysis
Potential Method
The prepaid work is represented by a potential that can be used to pay for future operations. Very important method that is used in lots of dierent contexts. We start with an initial data structure D0 on which n operations are performed. For 1 i n let ci be the cost of the i-th operation. Di is the data structure that results after applying the i-th operation to the data structure Di1 . A potential function maps each data structure Di to a real number (Di ).
Amortised Analysis
Potential Method II
The amortised cost ci of the i-th operation is dened by ci + (Di ) (Di1 ). The amortised cost of each operation is therefore its actual cost plus the increase in the potential due to the operation. The total amortised cost of the n operations is
n n n
ci =
i=1 i=1
ci + (Dn ) (D0 ).
If we can dene a potential function such that (Dn ) (D0 ), then the total amortized cost n upper bound on the actual cost i=1 ci
n i=1 ci
is an
Amortised Analysis
Amortised Analysis
10
Amortised Analysis
11
ci =
i=1 i=1
ci (Dn ) = (D0 )
i=1
2 bn + b0 = 2n bn + b0
Amortised Analysis
12