Today s Outline. CS 561, Lecture 18. Potential Method. Pseudocode. Dynamic Tables. Jared Saia University of New Mexico

Similar documents
CS 561, Lecture Topic: Amortized Analysis. Jared Saia University of New Mexico

CS 561, Lecture Topic: Amortized Analysis. Jared Saia University of New Mexico

Amortized Analysis. The time required to perform a sequence of (data structure) operations is averaged over all the operations performed.

Let the dynamic table support the operations TABLE-INSERT and TABLE-DELETE It is convenient to use the load factor ( )

Graduate Analysis of Algorithms Prof. Karen Daniels

Amortized Analysis. A Simple Analysis. An Amortized Analysis. Incrementing Binary Numbers. A Simple Analysis

CS:3330 (22c:31) Algorithms

Graduate Algorithms CS F-12 Amortized Analysis

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

Introduction to Algorithms

Today s Outline. CS 561, Lecture 8. Direct Addressing Problem. Hash Tables. Hash Tables Trees. Jared Saia University of New Mexico

CSCI Analysis of Algorithms I

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Outline. CS 561, Lecture 6. Priority Queues. Applications of Priority Queue. For NASA, space is still a high priority, Dan Quayle

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

COMP251: Amortized Analysis

Subsequence Definition. CS 461, Lecture 8. Today s Outline. Example. Assume given sequence X = x 1, x 2,..., x m. Jared Saia University of New Mexico

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

Complexity of Prim s Algorithm

CS 261 Spring 2013 Midterm

CS 561, Lecture 2 : Randomization in Data Structures. Jared Saia University of New Mexico

CS 561, Lecture 10. Jared Saia University of New Mexico

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

CS 561, Lecture 10. Jared Saia University of New Mexico

CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find. Lauren Milne Spring 2015

CS583 Lecture 12. Previously. Jana Kosecka. Amortized/Accounting Analysis Disjoint Sets. Dynamic Programming Greedy Algorithms

Disjoint set (Union-Find)

Disk Accesses. CS 361, Lecture 25. B-Tree Properties. Outline

Amortized Analysis. Andreas Klappenecker. [partially based on the slides of Prof. Welch]

W4231: Analysis of Algorithms

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Lecture slides by Kevin Wayne. Last updated on Apr 8, :13 AM

Algebra of Sets. Aditya Ghosh. April 6, 2018 It is recommended that while reading it, sit with a pen and a paper.

Lecture Summary CSC 263H. August 5, 2016

Divide and Conquer Algorithms

Introduction. Introduction: Multi-Pop Stack Example. Multi-Pop Stack Cost (clever) Multi-Pop Stack Cost (naïve)

Today we begin studying how to calculate the total time of a sequence of operations as a whole. (As opposed to each operation individually.

CS261: Problem Set #1

Dynamic Data Structures. John Ross Wallrabenstein

Union-Find and Amortization

Fundamental Graph Algorithms Part Three

Writing Parallel Programs; Cost Model.

Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445

Minimum Spanning Trees

Amortized Analysis and Union-Find , Inge Li Gørtz

Dynamic Arrays and Amortized Analysis

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Dynamic Arrays and Amortized Analysis

CPS 231 Exam 2 SOLUTIONS

CS261: A Second Course in Algorithms Lecture #16: The Traveling Salesman Problem

Outline for Today. Why could we construct them in time O(n)? Analyzing data structures over the long term.

Data Structures and Algorithm Analysis (CSC317) Hash tables (part2)

Mathematics for Computer Science Exercises from Week 4

Johns Hopkins Math Tournament Proof Round: Point Set Topology

Lecture 11 Unbounded Arrays

CS F-12 Amortized Analysis 1

Discrete Structures for Computer Science

Problem Set 4 Solutions

Amortized Analysis. Ric Glassey

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Solutions to Exam Data structures (X and NV)

1 Static-to-Dynamic Transformations

Exact String Matching Part II. Suffix Trees See Gusfield, Chapter 5

Greedy Algorithms Part Three

Binary Heaps in Dynamic Arrays

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

CS 161: Design and Analysis of Algorithms

Outline for Today. Analyzing data structures over the long term. Why could we construct them in time O(n)? A simple and elegant queue implementation.

1 The Shortest Path Problem

CS166 Handout 11 Spring 2018 May 8, 2018 Problem Set 4: Amortized Efficiency

Probabilistic Graphical Models

Analysis of Algorithms

CS261: A Second Course in Algorithms Lecture #14: Online Bipartite Matching

Math 190: Quotient Topology Supplement

4 Greedy Approximation for Set Cover

11 Sets II Operations

Outline. Computer Science 331. Definitions: Paths and Their Costs. Computation of Minimum Cost Paths

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1

Simplicial Complexes: Second Lecture

6.1 Minimum Spanning Trees

Bases of topologies. 1 Motivation

Persistent Data Structures

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

CS261: Problem Set #2

CS 561, Lecture 2 : Hash Tables, Skip Lists, Bloom Filters, Count-Min sketch. Jared Saia University of New Mexico

COMP171 Data Structures and Algorithms Fall 2006 Midterm Examination

COMP Analysis of Algorithms & Data Structures

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

CS264: Homework #1. Due by midnight on Thursday, January 19, 2017

Hashing. Hashing Procedures

Balanced Binary Search Trees. Victor Gao

DATA STRUCTURES AND ALGORITHMS

Lecture Transcript While and Do While Statements in C++

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

Algorithm Analysis. Part I. Tyler Moore. Lecture 3. CSE 3353, SMU, Dallas, TX

TOPOLOGY, DR. BLOCK, FALL 2015, NOTES, PART 3.

Lecture 4: examples of topological spaces, coarser and finer topologies, bases and closed sets

Transcription:

Today s Outline CS 561, Lecture 18 Jared Saia University of New Mexico Dynamic Tables 1 Pseudocode Potential Method 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++ } Let s now analyze Table-Insert using the potential method Let num i be the num value for the i-th call to Table-Insert Let size i be the size value for the i-th call to Table-Insert Then let Φ i = 2 num i size i 2 3

In Class Exercise Table Delete Recall that a i = c i + Φ i Φ i 1 Show that this potential function is 0 initially and always nonnegative Compute a i for the case where Table-Insert does not trigger an expansion Compute a i for the case where Table-Insert does trigger an expansion (note that num i 1 = num i 1, size i 1 = num i 1, size i = 2 (num i 1)) 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 4 5 Desirable Properties Naive Strategy 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 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 6 7

D Oh Analysis Unfortunately this strategy can cause amortized cost of an operation to be large Assume we perform n operations where n is a power of 2 The first n/2 operations are insertions At the end of this, T.num = T.size = n/2 Now the remaining n/2 operations are as follows: I, D, D, I, I, D, D, I, I,... where I represents an insertion and D represents a deletion Note that the first insertion causes an expansion The two following deletions cause a contraction The next two insertions cause an expansion again, etc., etc. The cost of each expansion and deletion is Θ(n) and there are Θ(n) of them Thus the total cost of n operations is Θ(n 2 ) and so the amortized cost per operation is Θ(n) 8 9 The Solution Recall: Load Factor The Problem: After an expansion, we don t perform enough deletions to pay for the contraction (and vice versa) The Solution: We allow the load factor to drop below 1/2 In particular, halve the table size when a deletion causes the table to be less than 1/4 full We can now create a potential function to show that Insertion and Deletion are fast in an amortized sense 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 10 11

The Potential Intuition Φ(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 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 12 13 Analysis Table Insert 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 num i be the number of items in the table after the i-th operation, size i be the size of the table after the i-th operation, and α i denote the load factor after the i-th operation 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 14 15

α i < 1/2 α i 1/2 In this case, we have a i = c i + Φ i Φ i 1 (1) = 1 + (size i /2 num i ) (size i 1 /2 num i 1 ) (2) = 1 + (size i /2 num i ) (size i /2 (num i 1)) (3) = 0 (4) a i = c i + Φ i Φ i 1 (5) = 1 + (2 num i size i ) (size i 1 /2 num i 1 ) (6) = 1 + (2 (num i 1 + 1) size i 1 ) (size i 1 /2 num i 1 (7) ) = 3 num i 1 3 2 size i 1 + 3 (8) = 3 α i 1 size i 1 3 2 size i 1 + 3 (9) < 3 2 size i 1 3 2 size i 1 + 3 (10) = 3 (11) 16 17 Take Away Deletions 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 For deletions, num i = num i 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 18 19

Case 1a Case 1b If α i 1 < 1/2 and the i-th operation does not cause a contraction, we know size i = size i 1 and we have: a i = c i + Φ i Φ i 1 (12) = 1 + (size i /2 num i ) (size i 1 /2 num i 1 ) (13) = 1 + (size i /2 num i ) (size i /2 (num i + 1))(14) = 2 (15) In this case, α i 1 < 1/2 and the i-th operation causes a contraction. We know that: c i = num i + 1 and size i /2 = size i 1 /4 = num i 1 = num i + 1. Thus: a i = c i + Φ i Φ i 1 ( = (num i + 1) + (size i /2 num i ) (size i 1 /2 num i 1 ) ( = (num i + 1) + ((num i + 1) num i ) ((2num i + 2) (num i +( = 1 ( 20 21 Case 2 Take Away In this case, α i 1 1/2 Proving that the amortized cost is constant for this case is left as an exercise to the diligent student Hint1: Q: In this case is it possible for the i-th operation to be a contraction? If so, when can this occur? Hint2: Try a case analysis on α i. Since we ve shown that the amortized cost of every operation is at most a constant, we ve shown that any sequence of n operations on a Dynamic table take O(n) time Note that in our scheme, the load factor never drops below 1/4 This means that we also never have more than 3/4 of the table that is just empty space 22 23

Disjoint Sets Operations We want to support the following operations: A disjoint set data structure maintains a collection {S 1, S 2,... S k } 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. 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 S x and S y ) into a new set that is S x S y. The new set is added to the data structure while S x and S y 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 24 25 Analysis Analysis We will analyze this data structure in terms of two parameters: 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 reduces 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 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 26 27

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 28