0% found this document useful (0 votes)
29 views12 pages

Amort

- Amortized analysis averages the time required for a sequence of data structure operations over all operations, rather than looking at each operation individually. This can show that the average cost per operation is small, even if some operations have high cost. - It differs from average-case analysis because it does not consider probabilities of different operations - it provides worst-case guarantees about performance. - The accounting method and potential method are two techniques used in amortized analysis. The accounting method assigns costs and "credits" to operations, while the potential method uses a potential function to represent prepaid work that can pay for future operations.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views12 pages

Amort

- Amortized analysis averages the time required for a sequence of data structure operations over all operations, rather than looking at each operation individually. This can show that the average cost per operation is small, even if some operations have high cost. - It differs from average-case analysis because it does not consider probabilities of different operations - it provides worst-case guarantees about performance. - The accounting method and potential method are two techniques used in amortized analysis. The accounting method assigns costs and "credits" to operations, while the potential method uses a potential function to represent prepaid work that can pay for future operations.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Amortized Analysis

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

Aggregate Analysis: Incrementing a binary counter


We show that for all n, a sequence of n operations takes worst-case time T (n). The amortized cost is T (n)/n in the worst case. We use an array A[0, . . . k 1] to store the counter. The lowest order bit is stored in A[0]. We use the counter to count upward from 0. x=
k1 i=0

A[i] 2i .

l[A] = length of A.

Amortised Analysis

Incrementing a binary counter II


increment(A)
i0 while i < l[A] and A[i] = 1 do A[i] 0 ii+1 if i < l[A] then A[i] 1

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

Accounting method: incrementing binary counter


Let ci be the actual cost of the i-th operation, and ci the amortised cost.
n i=1 ci

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 .

The total credit is never allowed to become negative!

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 + (Di ) (Di1 )) =


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

Potential Method III


In practice, we often do not know in advance how many operations we will make. Then we need (Di ) (D0 ) for all i. In most cases (D0 ) = 0. If the potential is positive, then we overcharged for some operations. If it is negative we undercharged. Of course, dierent potential functions will result if dierent amortised costs!

Amortised Analysis

10

Potential Method: Binary Counter


We dene the potential of the counter after the i-th INCREMENT operation to be bi , the number of 1s in the counter after the i-th operation. Suppose the i-th INCREMENT operation resets ti bits. The actual cost of the operation is (ti + 1) (resetting ti bits and set one bit to one). bi bi1 ti + 1: If bi = 0 then the i-th operation resets all k bits, and so bi1 = ti = k. If bi > 0, then bi = bi1 ti + 1.

Amortised Analysis

11

Potential Method: Binary Counter


The potential dierence is (Di ) (Di1 ) (bi1 ti + 1) = 1 ti . The amortised cost is therefore ci = ci + (Di ) (Di1 ) (ti + 1) + (1 ti ) = 2. If the counter starts at zero, then (D0 ) 0 for all i. The amortised cost is an upper bound on the actual cost and the worst case cost on n INCREMENT operations is O(n). But this time we also get results if the counter starts at another value:
n n n

ci =
i=1 i=1

ci (Dn ) = (D0 )
i=1

2 bn + b0 = 2n bn + b0

Amortised Analysis

12

You might also like