Amortized Analysis: Techniques for Analyzing Sequences of Data Structure Operations, Study notes of Algorithms and Programming

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

Pre 2010

Uploaded on 08/19/2009

koofers-user-kx5-1
koofers-user-kx5-1 šŸ‡ŗšŸ‡ø

8 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS231 Algorithms Handout # 34
Prof. Lyn Turbak November 28, 2001
Wellesley College
Amortization
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 Awhose slots contain either 0or 1,theInc(A) procedure below increments the
binary number represented by A:
BIncrement the binary number denoted by A.
BAssume indexing of A is 0-based, right-to-left.
Inc(A)
i←0
while (i <length[A]) and A[i] =1do
flip(A[i]) BFlip the bit value from 1 to 0
i←i+1
if i<length[A] then
flip(A[i]) BFlip the bit value from 0 to 1
We can a d d nto the number denoted by Aby incrementing ntimes:
BAdd n to the binary number denoted by A.
BAssume indexing of A is 0-based, right-to-left.
Add(n,A)
for j←1to ndo
Inc(A)
BCount 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: AcalltoInc(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).
1
pf3

Partial preview of the text

Download Amortized Analysis: Techniques for Analyzing Sequences of Data Structure Operations and more Study notes Algorithms and Programming in PDF only on Docsity!

CS231 Algorithms Handout # 34 Prof. Lyn Turbak November 28, 2001 Wellesley College

Amortization

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.