Docsity

Amortized Analysis: Dynamic Tables' Insert and Delete with Potential Function - Prof. Jare, Study notes of Computer Science

Toggle sidebar
bg1
CS 561, Lecture 18
Jared Saia
University of New Mexico
Today’s Outline
•Dynamic Tables
1
Pseudocode
Table-Insert(T,x){
if (T.size == 0){allocate T with 1 slot;T.size=1}
if (T.num == T.size){
allocate newTable with 2*T.size slots;
insert all items in T.table into newTable;
T.table = newTable;
T.size = 2*T.size
}
T.table[T.num] = x;
T.num++
}
2
Potential Method
•Let’s now analyze Table-Insert using the potential method
•Let numibe the num value for the i-th call to Table-Insert
•Let sizeibe the size value for the i-th call to Table-Insert
•Then let
Φi= 2 ∗numi−sizei
3
pf3
pf4
pf5
pf8

Partial preview of the text

Download Amortized Analysis: Dynamic Tables' Insert and Delete with Potential Function - Prof. Jare and more Study notes Computer Science in PDF only on Docsity!

CS 561, Lecture 18

Jared Saia University of New Mexico

Today’s Outline

  • Dynamic Tables

Pseudocode

Table-Insert(T,x){ if (T.size == 0){allocate T with 1 slot;T.size=1} if (T.num == T.size){ allocate newTable with 2T.size slots; insert all items in T.table into newTable; T.table = newTable; T.size = 2T.size } T.table[T.num] = x; T.num++ }

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))

Table Delete

  • We’ve shown that a Table-Insert has O(1) amortized cost
  • To implement Table-Delete, it is enough to remove (or zero out) the specified item from the table
  • However it is also desirable to contract the table when the load factor gets too small
  • Storage for old table can then be freed to the heap

Desirable Properties

We want to preserve two properties:

  • the load factor of the dynamic table is lower bounded by some constant
  • the amortized cost of a table operation is bounded above by a constant

Naive Strategy

  • A natural strategy for expansion and contraction is to double table size when an item is inserted into a full table and halve the size when a deletion would cause the table to become less than half full
  • This strategy guarantees that load factor of table never drops below 1/ 2

The Potential

Φ(t) =

{ 2 ∗ T.num − T.size if α(T ) ≥ 1 / 2 T.size/ 2 − T.num if α(T ) < 1 / 2

}

  • Note that this potential is legal since Φ(0) = 0 and (you can prove that) Φ(i) ≥ 0 for all i

Intuition

  • Note that when α = 1/2, the potential is 0
  • When the load factor is 1 (T.size = T.num), Φ(T ) = T.num, so the potential can pay for an expansion
  • When the load factor is 1/4, T.size = 4∗T.num, which means Φ(T ) = T.num, so the potential can pay for a contraction if an item is deleted

Analysis

  • Let’s now role up our sleeves and show that the amortized costs of insertions and deletions are small
  • We’ll do this by case analysis
  • Let numi be the number of items in the table after the i-th operation, sizei be the size of the table after the i-th opera- tion, and αi denote the load factor after the i-th operation

Table Insert

  • If αi− 1 ≥ 1 /2, analysis is identical to the analysis done in the In-Class Exercise - amortized cost per operation is 3
  • If αi− 1 < 1 /2, the table will not expand as a result of the operation
  • There are two subcases when αi− 1 < 1 /2: 1) αi < 1 /2 2) αi ≥ 1 / 2

αi < 1 / 2

  • In this case, we have

ai = ci + Φi − Φi− 1 (1) = 1 + (sizei/ 2 − numi) − (sizei− 1 / 2 − numi− 1 ) (2) = 1 + (sizei/ 2 − numi) − (sizei/ 2 − (numi − 1)) (3) = 0 (4)

αi ≥ 1 / 2

ai = ci + Φi − Φi− 1 (5) = 1 + (2 ∗ numi − sizei) − (sizei− 1 / 2 − numi− 1 ) (6) = 1 + (2 ∗ (numi− 1 + 1) − sizei− 1 ) − (sizei− 1 / 2 − numi− 1 (7)) = 3 ∗ numi− 1 −

sizei− 1 + 3 (8)

= 3 ∗ αi− 1 ∗ sizei− 1 −

sizei− 1 + 3 (9)

<

∗ sizei− 1 −

sizei− 1 + 3 (10) = 3 (11)

Take Away

  • So we’ve just show that in all cases, the amortized cost of an insertion is 3
  • We did this by case analysis
  • What remains to be shown is that the amortized cost of deletion is small
  • We’ll also do this by case analysis

Deletions

  • For deletions, numi = numi− 1 − 1
  • We will look at two main cases: 1) αi− 1 < 1 /2 and 2) αi− 1 ≥ 1 / 2
  • For the case where αi− 1 < 1 /2, there are two subcases: 1a) the i-th operation does not cause a contraction and 1b) the i-th operation does cause a contraction

Disjoint Sets

  • A disjoint set data structure maintains a collection {S 1 , S 2 ,... Sk} of disjoint dynamic sets
  • Each set is identified by a representative which is a member of that set
  • Let’s call the members of the sets objects.

Operations

We want to support the following operations:

  • Make-Set(x): creates a new set whose only member (and representative) is x
  • Union(x,y): unites the sets that contain x and y (call them Sx and Sy) into a new set that is Sx ∪ Sy. The new set is added to the data structure while Sx and Sy are deleted. The representative of the new set is any member of the set.
  • Find-Set(x): Returns a pointer to the representative of the (unique) set containing x

Analysis

  • We will analyze this data structure in terms of two parame- ters: 1. n, the number of Make-Set operations 2. m, the total number of Make-Set, Union, and Find-Set operations
  • Since the sets are always disjoint, each Union operation re- duces the number of sets by 1
  • So after n − 1 Union operations, only one set remains
  • Thus the number of Union operations is at most n − 1

Analysis

  • Note also that since the Make-Set operations are included in the total number of operations, we know that m ≥ n
  • We will in general assume that the Make-Set operations are the first n performed

Application

  • Consider a simplified version of Myspace
  • Every person is an object and every set represents a social clique
  • Whenever a person in the set S 1 forges a link to a person in the set S 2 , then we want to create a new larger social clique S 1 ∪ S 2 (and delete S 1 and S 2 )
  • We might also want to find a representative of each set, to make it easy to search through the set
  • For obvious reasons, we want these operation of Union, Make-Set and Find-Set to be as fast as possible