Docsity

Amortized Analysis of Disjoint Sets and Dynamic Tables, Slides of Computer Science

Partial preview of the text

Download Amortized Analysis of Disjoint Sets and Dynamic Tables and more Slides Computer Science in PDF only on Docsity!

Algorithms

Amortized Analysis

Review: Running Time of Kruskal’s Algorithm

● Expensive operations:

■ Sort edges: O(E lg E)

■ O(V) MakeSet()’s

■ O(E) FindSet()’s

■ O(V) Union()’s

● Upshot:

■ Comes down to efficiency of disjoint-set

operations, particularly Union()

Review: Disjoint Set Union Analysis

● Worst-case analysis: O(n

) time for n Union’s

Union(S

1

, S

2

) “copy” 1 element
Union(S 2 , S 3 ) “copy” 2 elements
Union(S

n-

, S

n

) “copy” n-1 elements
O(n

2

● Improvement: always copy smaller into larger

■ How long would above sequence of Union’s take?

■ Worst case: n Union’s take O(n lg n) time

■ Proof uses amortized analysis

Review: Amortized Analysis of Disjoint Sets

● If elements are copied from the smaller set into

the larger set, an element can be copied at most

lg n times

■ Worst case: Each time copied, element in smaller set

1st time resulting set size ≥ 2
2nd time ≥ 4
(lg n)th time ≥ n

Amortized Analysis: Accounting Method

● Accounting method

■ Charge each operation an amortized cost

■ Amount not used stored in “bank”

■ Later operations can used stored money

■ Balance must not go negative

● Book also discusses potential method

■ But we won’t worry about it here

Accounting Method Example: Dynamic Tables

● Implementing a table (e.g., hash table) for

dynamic data, want to make it small as possible

● Problem: if too many items inserted, table may

be too small

● Idea: allocate more memory as needed

Analysis Of Dynamic Tables

● Let c

i

= cost of i th insert

● c

i

= i if i-1 is exact power of 2, 1 otherwise

● Example:

■ Operation Table Size Cost

Insert(1) 1 1 1

Analysis Of Dynamic Tables

● Let c

i

= cost of i th insert

● c

i

= i if i-1 is exact power of 2, 1 otherwise

● Example:

■ Operation Table Size Cost

Insert(1) 1 1 1 Insert(2) 2 1 + 1 2

Analysis Of Dynamic Tables

● Let c

i

= cost of i th insert

● c

i

= i if i-1 is exact power of 2, 1 otherwise

● Example:

■ Operation Table Size Cost

Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Insert(3) 4 1 + 2 3 Insert(4) 4 1 4

Analysis Of Dynamic Tables

● Let c

i

= cost of i th insert

● c

i

= i if i-1 is exact power of 2, 1 otherwise

● Example:

■ Operation Table Size Cost

Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Insert(3) 4 1 + 2 3 Insert(4) 4 1 4 Insert(5) 8 1 + 4 5

Analysis Of Dynamic Tables

● Let c

i

= cost of i th insert

● c

i

= i if i-1 is exact power of 2, 1 otherwise

● Example:

■ Operation Table Size Cost

Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Insert(3) 4 1 + 2 3 Insert(4) 4 1 4 Insert(5) 8 1 + 4 5 Insert(6) 8 1 6 Insert(7) 8 1 7

Analysis Of Dynamic Tables

● Let c

i

= cost of i th insert

● c

i

= i if i-1 is exact power of 2, 1 otherwise

● Example:

■ Operation Table Size Cost

Insert(1) 1 1 1 Insert(2) 2 1 + 1 2 Insert(3) 4 1 + 2 3 Insert(4) 4 1 4 Insert(5) 8 1 + 4 5 Insert(6) 8 1 6 Insert(7) 8 1 7 Insert(8) 8 1 8

Aggregate Analysis

● n Insert() operations cost

● Average cost of operation

= (total cost)/(# operations) < 3

● Asymptotically, then, a dynamic table costs the

same as a fixed-size table

■ Both O(1) per Insert operation

c n n n n

n j j n i i

lg 1 0

= =

Accounting Analysis

● Charge each operation $3 amortized cost

■ Use $1 to perform immediate Insert()

■ Store $

● When table doubles

■ $1 reinserts old item, $1 reinserts another old item

■ Point is, we’ve already paid these costs

■ Upshot: constant (amortized) cost per operation