

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Amortized analysis, a technique used to analyze the worst-case running times of sequences of data structure operations. An example of binary counters and explains two methods for amortized analysis: the aggregate method and the banker's method. The physicist's method is also mentioned.
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


CS231 Algorithms Handout # 34 Prof. Lyn Turbak November 28, 2001 Wellesley College
Reading: CLRS Chapter 17/CLR Chapter 18
Motivating Amortized Analysis
In many situations, the worst-case running time of a sequence of data structure operations is asymptotically smaller than the sum of the worst-case running times for the individual operations.
Why? Intuitively, a single costly operation is often balanced by a large number of cheap operations.
Amortized analysis is a collection of techniques for analyzing the worst-case running times of sequences of operations. Amortized analysis involves the average running time of operations in a sequence, but it is not probabilistic in nature.
Example: Binary Counters
Given an array A whose slots contain either 0 or 1 , the Inc(A) procedure below increments the binary number represented by A:
B Increment the binary number denoted by A. B Assume indexing of A is 0-based, right-to-left. Inc(A) i ā 0 while (i < length[A]) and A[i] = 1 do flip(A[i]) B Flip the bit value from 1 to 0 i ā i + 1 if i < length[A] then flip(A[i]) B Flip the bit value from 0 to 1
We can add n to the number denoted by A by incrementing n times:
B Add n to the binary number denoted by A. B Assume indexing of A is 0-based, right-to-left. Add(n,A) for j ā 1 to n do Inc(A)
B Count to n in binary using binary numbers with k digits Count(n,k) A ā new array(k) Add(n,A)
Question: What is the cost (measured in number of flips) of Count(n,k)?
Loose Analysis: A call to Inc(A) could have worst-case cost O(k), so Count(n,k) has cost O(n Ā· k).
Tighter Analysis (justified below): Count(n,k) has cost O(n), so amortized cost of Inc(A) is O(1).
Aggregate Method
In the aggregate method, we determine a worst-case cost T (n) for a sequence of n operations. The amortized cost of each operation is then T (n)/n.
Binary counter example: The following shows the bits flipped in Count(17,8):
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1
Observe that bit position i (0-indexed, right-to-left) is flipped every 2i^ calls to Inc. So the total number of flips F (n, k) performed in Count(n,k) is:
F (n, k) =
ā^ k
i=
bn/ 2 ic ⤠n ·
i=
b 1 / 2 ic =
So the amortized cost I(k) of a call to Inc(A) on length-k array A is:
Bankerās Method (Accounting Method)
In the bankerās method (accounting method), some operations can ādepositā credits in par- ticular positions in a data structure that are used to pay for other operations. The credits are used only for analyzing algorithms; the algorithm is not changed to include fields for the credits!
Binary counter example: Suppose that flipping a bit costs $1. In Inc, suppose that we charge $ to flip a bit from 0 to 1 : $1 is used to pay for the bit flip, and a $1 credit remains on the 1 bit. We pay for every flip from 1 to 0 using the $1 credit already āattachedā to the 1 bit. Clearly, Inc(A) costs $2, and Count(n,k) costs $2n.
Unlike the aggregate method, the bankerās method allows us to assign different amortized costs to different operations.