Docsity

Amortized Cost Analysis: Potential Method and Dynamic Tables - Prof. Jared C. Saia, Study notes of Computer Science

Toggle sidebar
bg1
CS 561, Lecture 17
Jared Saia
University of New Mexico
Today’s Outline
•Potential Method
•Dynamic Tables
1
Potential Method
•The most powerful method (and hardest to use)
•Builds on the idea from physics of potential energy
•Instead of associating taxes with particular operations, rep-
resent prepaid work as a potential that can be spent on later
operations
•Potential is a function of the entire data structure
2
Potential Function
•Let Didenote our data structure after ioperations
•Let Φidenote the potential of Di
•Let cidenote the cost of the i-th operation (this changes
Di−1into Di)
•Then the amortized cost of the i-th operation, ai, is defined
to be the actual cost plus the change in potential:
ai=ci+ Φi−Φi−1
3
pf3
pf4
pf5

Partial preview of the text

Download Amortized Cost Analysis: Potential Method and Dynamic Tables - Prof. Jared C. Saia and more Study notes Computer Science in PDF only on Docsity!

CS 561, Lecture 17

Jared Saia University of New Mexico

Today’s Outline

  • Potential Method
  • Dynamic Tables

Potential Method

  • The most powerful method (and hardest to use)
  • Builds on the idea from physics of potential energy
  • Instead of associating taxes with particular operations, rep- resent prepaid work as a potential that can be spent on later operations
  • Potential is a function of the entire data structure

Potential Function

  • Let Di denote our data structure after i operations
  • Let Φi denote the potential of Di
  • Let ci denote the cost of the i-th operation (this changes Di− 1 into Di)
  • Then the amortized cost of the i-th operation, ai, is defined to be the actual cost plus the change in potential:

ai = ci + Φi − Φi− 1

Potential Method

  • So the total amortized cost of n operations is the actual cost plus the change in potential: ∑^ n i=

ai =

∑^ n i=

( ci + Φi − Φi− 1 ) =

∑^ n i=

ci + Φn − Φ 0.

Potential Method

  • Our task is to define a potential function so that
    1. Φ 0 = 0
    2. Φi ≥ 0 for all i
  • If we do this, the total actual cost of any sequence of oper- ations will be less than the total amortized cost ∑^ n i=

ci =

∑^ n i=

ai − Φn ≤

∑^ n i=

ai.

Binary Counter Example

  • For the binary counter, we can define the potential Φi after the i-th Increment operation to be the number of bits with value 1
  • Initially all bits are 0 so Φ 0 = 0, further Φi ≥ 0 for all i > 0, so this is a legal potential function

Binary Counter

  • We can describe both the actual cost of an Increment and the change in potential in terms of the number of bits set to 1 and reset to 0. ci = #bits flipped from 0 to 1 + #bits flipped 1 to 0 Φi − Φi− 1 = #bits flipped from 0 to 1 − #bits flipped 1 to 0
  • Thus, the amortized cost of the ith Increment is ai = ci + Φi − Φi− 1 = 2 × #bits flipped from 0 to 1

Stack Example

  • Consider again a stack with Multipop
  • Define the potential function Φ on the stack to be the num- ber of objects on the stack
  • This potential function is “legal” since Φ 0 = 0 and Φi ≥ 0 for all i > 0

Push

  • Let’s now compute the costs of the different stack operations on a stack with s items
  • If the i-th operation on the stack is a push operation on a stack containing s objects, then

Φi − Φi− 1 = (s + 1) − s = 1

  • So ai = ci + 1 = 2

Multipop

  • Let the i-th operation be Multipop(S,k) and let k′^ = min(k, s) be the number of objects popped off the stack. Then Φi − Φi− 1 = (s − k′) − s = −k′.
  • Further ci = k′.
  • Thus,

ai = −k′^ + k′^ = 0

  • (We can show similarly that the amortized cost of a pop operation is 0)

Wrapup

  • The amortized cost of each of these three operations is O(1)
  • Thus the worst case cost of n operations is O(n)

Dynamic Tables

  • Consider the situation where we do not know in advance the number of items that will be stored in a table, but we want constant time access
  • We might allocate a fixed amount of space for the table only to find out later that this was not enough space
  • In this case, we need to copy over all objects stored in the original table into a new larger table
  • Similarly, if many objects are deleted, we might want to re- duce the size of the table

Dynamic Tables

  • The data structure that we want is a Dynamic Table (aka Dynamic Array)
  • We can show using amortized analysis that the amortized cost of an insertion and deletion into a Dynamic Table is O(1) even though worst case cost may be much larger

Load Factor

  • For a nonempty table T , we define the “load factor” of T , α(T ), to be the number of items stored in the table divided by the size (number of slots) of the table
  • We assign an empty table (one with no items) size 0 and load factor of 1
  • Note that the load factor of any table is always between 0 and 1
  • Further if we can say that the load factor of a table is always at least some constant c, then the unused space in the table is never more than 1 − c

Table Expansion

  • Assume that the table is allocated as an array
  • A table is full when all slots are used i.e. when the load factor is 1
  • When an insert occurs when the table is full, we need to expand the table
  • The way we will do this is to allocate an array which is twice the size of the old array and then copy all the elements of the old array into this new larger array
  • If only insertions are performed, this ensures that the load factor is always at least 1/ 2

Taxation Method

  • Suppose that the size of the table is m right after an expan- sion
  • Then the number of items in the table is m/ 2
  • Each time Table-Insert is called, we tax the operation 3 dol- lars: 1. One dollar is used immediately to pay for the elementary insert 2. Another dollar is stored with the item that is inserted 3. The third dollar is placed as credit on one of the m/ 2 items already in the table

Taxation Method

  • Filling the table again requires m/2 total calls to Table-Insert
  • Thus by the time the table is full and we do another expan- sion, each item will have one dollar of credit on it
  • This dollar of credit can be used to pay for the movement of that item during the expansion

Potential Method

  • Let’s now analyze Table-Insert using the potential method
  • Let numi be the num value for the i-th call to Table-Insert
  • Let sizei be the size value for the i-th call to Table-Insert
  • Then let

Φi = 2 ∗ numi − sizei

In Class Exercise

Recall that ai = ci + Φi − Φi− 1

  • Show that this potential function is 0 initially and always nonnegative
  • Compute ai for the case where Table-Insert does not trigger an expansion
  • Compute ai for the case where Table-Insert does trigger an expansion (note that numi− 1 = numi − 1, sizei− 1 = numi − 1, sizei = 2 ∗ (numi − 1))