Algorithm analysis Design and Microprocessor - Amortized, Study notes of Design and Analysis of Algorithms

The file contains numerical examples and explanation of the following topics - Amortized Analysis - Aggregate Method - Accounting Method - Potential Method

Typology: Study notes

2010/2011

Uploaded on 09/01/2011

visir66
visir66 🇮🇳

4.4

(74)

97 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Amortized Analysis
Reference
Introduction to Algorithms by Cormen, Leiserson, Rivest. Chapter 18
Amortized Analysis
In a sequence of operations on a data structure often the worst case can not occur in each
operation
Example - Move-to-Front
Assume we have a list of n = 2k items: a1, a2, ..., an
Will perform n accesses on the list
Will access item a1 n/2 times
Worst case access requires n compares
Thus worst case cost for n access is O(n*n)?????
At most 1 + n/2 accesses require n comparisons!
Once item a1 is accessed it will not make it to the end of the list
Amortized Analysis gives the average performance of each operation in the worst case
Example - Incrementing a k-bit binary counter
Let A[0..k-1] be an array of bits representing a number X
A[0] - low order bit, so
length[A] = k
Start with X = 0
pf3
pf4
pf5

Partial preview of the text

Download Algorithm analysis Design and Microprocessor - Amortized and more Study notes Design and Analysis of Algorithms in PDF only on Docsity!

Amortized Analysis

Reference

Introduction to Algorithms by Cormen, Leiserson, Rivest. Chapter 18

Amortized Analysis

In a sequence of operations on a data structure often the worst case can not occur in each

operation

Example - Move-to-Front

Assume we have a list of n = 2k items: a1, a2, ..., an

Will perform n accesses on the list

Will access item a1 n/2 times

Worst case access requires n compares

Thus worst case cost for n access is O(n*n)?????

At most 1 + n/2 accesses require n comparisons!

Once item a1 is accessed it will not make it to the end of the list

Amortized Analysis gives the average performance of each operation in the worst case

Example - Incrementing a k-bit binary counter

Let A[0..k-1] be an array of bits representing a number X

A[0] - low order bit, so

length[A] = k

Start with X = 0

Increment (A)

J = 0

while J < length[A] and A[J] == 1 do

A[J] = 0

J = J + 1

end while

if J < length[A] then

A[J] = 1

end if

end Increment

Count bits flipped

Worst Case

Increment flips k bits in worst case

Sequence of n Increment operations takes O(nk)

Aggregate Method

T(n) = all work done in worst case in sequence of n operations

Amortized cost per operation is T(n)/n

X A[4] A[3] A[2] A[1] A[0] Total Cost 0 0 0 0 0 0 0 1 0 0 0 0 1 1 2 0 0 0 1 0 3 3 0 0 0 1 1 4 4 0 0 1 0 0 7 5 0 0 1 0 1 8 6 0 0 1 1 0 10 7 0 0 1 1 1 11 8 0 1 0 0 0 15 9 0 1 0 0 1 16

A[0] flips each time Increment is called n

A[1] flips every other time

A[1] flips every fourth time

A[J] flips every 2**J time

Total number of flips is:

So amortized cost of each operation is 2 = O(1)

Accounting Method

Assign an amortized cost to each operation

Amortized cost may be more or less than the actual cost

Amortized costs:

First access of a

initial location of a <= n

All other access of a

Accessing a non-a item

actual cost + 1 <= n +

assign credit to a

a, b, c, d, e Amortized cost b, a(1), c, d, e accessed b 2 c, b, a(2), d, e accessed c 4 a, c, b, d, e accessed a 1 e a(1), c, b, d accessed e 6 a, c, b, d, e accessed a 1 a, c, b, d, e accessed a 1

(Amortized cost of all accesses of a) <= n + n/2 - 1 = 3n/2 - 1

(Amortized cost of accessing all non-a items) <= n*(n+1)/

(Total Cost) <= (4n + n*n)/2 -1, (Average Cost/access) <= 4 + n/

Potential Method

Assign an amortized cost to each operation

Amortized cost may be more or less than the actual cost

If amortized cost is more than the actual cost of the operation assign the difference the

entire data structure as potential energy

ck= actual cost of operation k

= amortized cost of operation k

Dk = the state of the data structure after applying k'th operation to Dk

= potential associated with Dk

So if >=0 then is an upper bound on total cost of the algorithm

Example - binary counter

Potential = number of 1's in the counter

if the k'th operation sets tk bits to 0 the actual cost is tk + 1

=1 - tk

so

How to find?

Dynamic Tables

Table -> pointer to a table

Number -> number of items in the table

Size -> size of the table

AddToTable(x)

if Number == Size then

allocate NewTable with size 2*Size

insert all items from Table to newTable

free Table

Table = NewTable

Size = 2 * Size

end if

insert x into Table

Number = Number + 1

end insert

How many inserts are done by N AddToTable?

Amortized Cost per AddToTable 3 inserts

Table after moving from size 4 to size 8

Perform 4 AddToTable operations

X X X X

X X X X Y(2)

X X X X Y(2) Y(2)

1 delete lg(n)/n call to new