SDSU CS660 Combinatorial Algorithms
Fall Semester, 1996
Amortized Analysis

[To Lecture Notes Index]
San Diego State University -- This page last updated Thursday, 19 September, 1996
----------

Contents of Amortized Analysis

  1. Reference
  2. Amortized Analysis
    1. Aggregate Method
    2. Accounting Method
    3. Potential Method

Amortized Analysis Slide # 1

Reference

Introduction to Algorithms by Cormen, Leiserson, Rivest. Chapter 18
Amortized Analysis Slide # 2
Dynamic Array

When an array becomes full, just copy the array elements to a larger array

Array-> pointer to an array

Number -> number of items in the array

Size -> size of the array
AddToTable(x)

   if Number == Size then

      allocate NewArray with size 2*Size

      insert all items from Table to NewArray 

      free Array

      Array= NewArray 

      Size = 2 * Size

   end if

   insert x into Array

   Number = Number + 1

   end insert

Amortized Analysis Slide # 3

Amortized Analysis


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


The worst case cost of inserting an element in a dynamic array is N + 1

N = size of the array before the insertion

But you can't get two worst cases in a row!


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


Three methods used in amortized analysis
Aggregate Method
Accounting Method
Potential Method

Amortized Analysis Slide # 4
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)

Amortized Analysis Slide # 5

Aggregate Method


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

Amortized cost per operation is T(n)/n
XA[4]A[3]A[2]A[1]A[0]Total Cost
0000000
1000011
2000103
3000114
4001007
5001018
60011010
70011111
80100015
90100116

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 2J time
Total number of flips is:

So amortized cost of each operation is 2 = O(1)
Amortized Analysis Slide # 6

Accounting 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 to part of the data structure as a credit

No negative credit allowed

Total amortized cost is >= total worst case cost

Example - binary counter

Amortized cost of setting bit to 1 2 units
1 unit to pay for setting bit to 1
1 unit stored with bit

Amortized cost of setting bit to 0 0 units
Only a 1-bit is set to 0,
All 1-bits have credit of one unit
This pays for setting bit to 0

Amortized Analysis Slide # 7
Example Continued
Increment (A)

   J = 0
   while J < length[A] and A[J] == 1  do 
      A[J] = 0                     Cost 0
      J = J + 1
   end while

   if J < length[A] then
      A[J] = 1                     Cost 2
   end if
end Increment



XA[4]A[3]A[2]A[1]A[0]Amortized cost
0000000
100001 (1)2
20001 (1)04
30001 (1)1 (1)6
4001 (1)008
5001 (1)01 (1)10
6001 (1)1 (1)012
7001 (1)1 (1)1 (1)14
801 (1)00016
901 (1)001 (1)18

Amortized Analysis Slide # 8

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
Amortized Analysis Slide # 9
Example - binary counter

Potential = number of 1's in the counter

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



so





How to find ?
Amortized Analysis Slide # 10
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
Amortized Analysis Slide # 11
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

XXXX
XXXXY(2)
XXXXY(2)Y(2)
XXXXY(2)Y(2)Y(2)
XXXXY(2)Y(2)Y(2)Y(2)



1/2 the table has 2 credits

One credit per item to pay for the move to next size table

XXXXYYYY

Amortized Analysis Slide # 12
Table Expansion and Contraction

Policy
When table becomes full move to table twice the size
When table contracts to 1/4 full, move to table 1/2 size


Let load = (Size of table) / (number of items in the table)


Amortized Cost
Inserting when load >= 1/2
3 units
Inserting when load < 1/2
0 units
Deleting when load > 1/2
0 units
Deleting when load <= 1/2
2 units
XXXX
XXXXY(2)
XXXX
XXX(1)
XX(1)(1)
XX

Amortized Analysis Slide # 13
Linked List vs. Dynamic Array
Unordered List

Amortized Costs per operation over n operations
linked ListDynamic Array
Insertion1 call to newlg(n)/n call to new
set two links3 data moves




Deletion two links3 data moves
after find
1 deletelg(n)/n call to delete


----------