CS 473Lecture X1 CS473-Algorithms I Lecture X Dynamic Tables.

Slides:



Advertisements
Similar presentations
Chapter 11. Hash Tables.
Advertisements

October 31, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL13.1 Introduction to Algorithms LECTURE 11 Amortized Analysis Dynamic tables.
Hash Tables.
Factoring Polynomials to Find Roots. Factoring a Polynomial to Find Roots Factor 3x x x x – 24 and find all of the roots. Roots: Factors:
Outcome: Determine the square root of perfect squares.
2 x /18/2014 Know Your Facts!. 11 x /18/2014 Know Your Facts!
5 x4. 10 x2 9 x3 10 x9 10 x4 10 x8 9 x2 9 x4.
Production Mix Problem Graphical Solution med lrg Electronics Cabinetry Profit (10,20) (Optimal Product Mix!) Profit.
COMP9314Xuemin Continuously Maintaining Order Statistics Over Data Streams Lecture Notes COM9314.
Planar point location -- example
ArrayLists David Kauchak cs201 Spring Extendable array Arrays store data in sequential locations in memory Elements are accessed via their index.
Quick Review of Apr 10 material B+-Tree File Organization –similar to B+-tree index –leaf nodes store records, not pointers to records stored in an original.
Data Structures Using C++ 2E
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
Data Structures Haim Kaplan and Uri Zwick October 2013 Lecture 2 Amortized Analysis “amortized analysis bounds the worst case running time of a sequence.
David Luebke 1 5/4/2015 CS 332: Algorithms Dynamic Programming Greedy Algorithms.
1 Chapter 17: Amortized Analysis III. 2 Dynamic Table Sometimes, we may not know in advance the #objects to be stored in a table We may allocate space.
CS261 Data Structures Dynamic Arrays. Pro: only core data structure designed to hold a collection of elements Pro: random access: can quickly get to any.
Hashing21 Hashing II: The leftovers. hashing22 Hash functions Choice of hash function can be important factor in reducing the likelihood of collisions.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 3 Tuesday, 2/9/10 Amortized Analysis.
CPSC 411, Fall 2008: Set 6 1 CPSC 411 Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Jennifer Welch Fall 2008.
1 Amortized Analysis Consider an algorithm in which an operation is executed repeatedly with the property that its running time fluctuates throughout the.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2005 Lecture 3 Tuesday, 2/8/05 Amortized Analysis.
Tirgul 9 Amortized analysis Graph representation.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Lecture 3 Tuesday, 2/10/09 Amortized Analysis.
Amortized Anaysis of Algorihms, A.Yazici, Spring 2007CEng Amortized Analysis of Algorithms Adnan YAZICI Dept. of Computer Engineering Middle East.
Theory I Algorithm Design and Analysis (8 – Dynamic tables) Prof. Th. Ottmann.
Amortized Analysis (chap. 17) Not just consider one operation, but a sequence of operations on a given data structure. Average cost over a sequence of.
CS305/503, Spring 2009 Amortized Analysis Michael Barnathan.
CS333 / Cutler Amortized Analysis 1 Amortized Analysis The average cost of a sequence of n operations on a given Data Structure. Aggregate Analysis Accounting.
Andreas Klappenecker [based on the slides of Prof. Welch]
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
17.Amortized analysis Hsu, Lih-Hsing. Computer Theory Lab. Chapter 17P.2 The time required to perform a sequence of data structure operations in average.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 2 (Part 2) Tuesday, 9/11/01 Amortized Analysis.
Amortized Analysis Not just consider one operation, but a sequence of operations on a given data structure. Average cost over a sequence of operations.
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 14 Prof. Charles E. Leiserson.
Amortized Analysis The problem domains vary widely, so this approach is not tied to any single data structure The goal is to guarantee the average performance.
Amortized Analysis Typically, most data structures provide absolute guarantees on the worst case time for performing a single operation. We will study.
An Optimal Cache-Oblivious Priority Queue and Its Applications in Graph Algorithms By Arge, Bender, Demaine, Holland-Minkley, Munro Presented by Adam Sheffer.
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner.
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
Amortized Analysis Some of the slides are from Prof. Leong Hon Wai’s resources at National University of Singapore.
CSCE 411H Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 6 1 * Slides adapted.
CMSC 341 Amortized Analysis.
David Luebke 1 12/12/2015 CS 332: Algorithms Amortized Analysis.
Introduction to Algorithms 6.046J/18.401J LECTURE7 Hashing I Direct-access tables Resolving collisions by chaining Choosing hash functions Open addressing.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
Amortized Analysis. Problem What is the time complexity of n insert operations into an dynamic array, which doubles its size each time it is completely.
David Luebke 1 2/26/2016 CS 332: Algorithms Dynamic Programming.
Amortized Analysis In amortized analysis, the time required to perform a sequence of operations is averaged over all the operations performed Aggregate.
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
Amortized Analysis.
Amortized Analysis of Rehashing
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Amortized Analysis (chap. 17)
CSCE 411 Design and Analysis of Algorithms
Presentation by Marty Krogel
Amortized Analysis The problem domains vary widely, so this approach is not tied to any single data structure The goal is to guarantee the average performance.
Chapter 17 Amortized Analysis Lee, Hsiu-Hui
CSCE 411 Design and Analysis of Algorithms
CS 332: Algorithms Amortized Analysis Continued
CSCE 411 Design and Analysis of Algorithms
CSE 373: Data Structures and Algorithms
Presentation transcript:

CS 473Lecture X1 CS473-Algorithms I Lecture X Dynamic Tables

CS 473Lecture X2 Why Dynamic Tables? In some applications: We don't know how many objects will be stored in a table. We may allocate space for a table –But, later we may find out that it is not enough. –Then, the table must be reallocated with a larger size. All the objects stored in the original table Must be copied over into the new table.

CS 473Lecture X3 Why Dynamic Tables? Similarly, if many objects are deleted from the table: –it may be worthwhile to reallocate the table with a smaller size. This problem is called Dynamically Expanding and Contracting a table.

CS 473Lecture X4 Why Dynamic Tables? Using amortized analysis we will show that, The amortized cost of insertion and deletion is O(1). Even though the actual cost of an operation is large when it triggers an expansion or a contraction. We will also show how to guarantee that The unused space in a dynamic table never exceeds a constant fraction of the total space.

CS 473Lecture X5 Operations TABLE-INSERT: Inserts into the table an item that occupies a single slot. TABLE-DELETE: Removes an item from the table & frees its slot.

CS 473Lecture X6 Load Factor Load Factor of a Dynamic Table T For an empty table by definition

CS 473Lecture X7 Insertion-Only Dynamic Tables Table-Expansion: Assumption: –Table is allocated as an array of slots A table fills up when –all slots have been used –equivalently, when its load factor becomes 1 Table-Expansion occurs when –An item is to be inserted into a full table

CS 473Lecture X8 Insertion-Only Dynamic Tables A Common Heuristic –Allocate a new table that has twice as many slots as the old one. Hence, insertions are performed if only 1 / 2 α(T) 1

CS 473Lecture X9 Table Insert TABLE-INSERT (T, x) if size[T] = 0 then allocate table[T] with 1 slot size[T] 1 if num[T] = size[T] then allocate new-table with 2.size[T] slots copy all items in table[T] into new-table free table[T] table[T] new-table[T] size[T] 2.size[T] insert x into table[T] num[T] num[T] + 1 end table[T] : pointer to block of table storage num[T] : number of items in the table size[T] : total number of slots in the table Initially, table is empty, so num[T] = size[T] = 0

CS 473Lecture X10 Table Expansion Running time of TABLE-INSERT is proportional to the number of elementary insert operations. Assign a cost of 1 to each elementary insertion Analyze a sequence of n TABLE-INSERT operations on an initially empty table

CS 473Lecture X11 Cost of Table Expansion What is cost c i of the i-th operation? If there is room in the current table (or this is the first operation) c i = 1 (only one elementary insert operation) If the current table is full, an expansion occurs, then the cost is c i = i. 1 for the elementary insertion of the new item i-1 for the items that must be copied from the old table to the new table.

CS 473Lecture X12 Cost of Table Expansion If n operations are performed, The worst case cost of an operation is O(n) Therefore the total running time is O(n 2 ) This bound is not tight, because Expansion does not occur so often in the course of n operations.

CS 473Lecture X13 Amortized Analysis of Insert The Aggregate Method Table is initially empty. Observe: i-th operation causes an expansion only when i- 1 is a power of 2.

CS 473Lecture X14 The Aggregate Method Therefore the total cost of n TABLE-INSERT operations is The amortized cost of a single operation is 3n/n=3 = O(1) i cici Expansion cost

CS 473Lecture X15 The Accounting Method Assign the following amortized costs –Table-Expansion : 0 –Insertion of a new item : 3 Insertion of a new item a)1 (as an actual cost) for inserting itself into the table b)1 (as a credit) for moving itself in the next expansion c)1 (as a credit) for moving another item (in the next expansion) that has already moved in the last expansion

CS 473Lecture X16 Accounting Method Example Size of the table: M Immediately after an expansion (just before the insertion) num[T] = M/2 and size[T] = M where M is a power of 2. Table contains no credits XXXXXXXX

CS 473Lecture X17 Accounting Method Example 1st insertion 2nd insertion XXXXXXXXZ $1 (a) $1 for insertion (b) (c) XXXXXXXXZZ $1

CS 473Lecture X18 Accounting Method Example M/2th Insertion Thus, by the time the table contains M items and is full –each item in the table has $1 of credit to pay for its move during the next expansion XXXXXXXXZZZZZZZZ $1

CS 473Lecture X19 The Potential Method Define a potential function Φ that is –0 immediately after an expansion –builds to the table size by the time table becomes full Next expansion can be paid for by the potential.

CS 473Lecture X20 Definition of Φ One possible Φ is: Φ(T) = 2*num[T] –size[T] Immediately after an expansion size[T] = 2*num[T] Φ(T) = 0 Immediately before an expansion size[T] = num[T] Φ(T) = num[T] The initial value for the potential is 0

CS 473Lecture X21 Definition of Φ Since the table is at least half full (i.e. num[T] size[T] / 2) Φ(T) is always nonnegative. Thus, the sum of the amortized cost of n TABLE-INSERT operations is an upper bound on the sum of the actual costs.

CS 473Lecture X22 Analysis of i-th Table Insert n i : num[T] after the i-th operation s i : size[T] after the i-th operation Φ i : Potential after the i-th operation Initially we have n i = s i = Φ i = 0 Note that, n i = n i-1 +1 always hold.

CS 473Lecture X23 Insert without Expansion If the i-th TABLE-INSERT does not trigger an expansion, s i = s i – 1 ; c i = 1

CS 473Lecture X24 Insert with Expansion If the i-th TABLE-INSERT does trigger an expansion, then

CS 473Lecture X25

CS 473Lecture X26 Adding Delete Operation TABLE-DELETE: Remove the specified item from the table. It is often desirable to contract the table. In table contraction, we would like to preserve two properties Load factor of dynamic table is bounded below by a constant Amortized cost of a table operation is bounded above by a constant We assume that the cost can be measured in terms of elementary insertions and deletions

CS 473Lecture X27 Expansion and Contraction A natural strategy for expansion and contraction Double the table size when an item is inserted into a full table Halve the size when a deletion would cause < 1 / 2 This strategy guarantees Unfortunately, it can cause the amortized cost of an operation to be quite large

CS 473Lecture X28 Worst-Case for α(T) ½ Consider the following worst case scenario –We perform n operations on an empty table where n is a power of 2 –First n/2 operations are all insertions, cost a total of Θ(n) at the end: we have num[T] = size[T] = n/2 –Second n/2 operations repeat the sequence I D D I that is I D D I I D D I I D D I...

CS 473Lecture X29 Worst-Case for α(T) ½ In the second n/2 operations –The first INSERT cause an expansion –Two further DELETEs cause contraction –Two further INSERTs cause expansion... and so on Hence there are n/8 expansions and n/8 contractions The cost of each expansion and contraction is n/2 i:i: oper:II...IIIDDIIDDI nini sisi ECEC Example: n=16

CS 473Lecture X30 Worst-Case for α(T) ½ Thus the total cost of n operations is Θ(n 2 ) since –First n/2 operations : 3n –Second n/2 operations : (n/4)*(n/2)=n 2 /8 The amortized cost of an operation is Θ(n) The difficulty with this strategy is –After an expansion, we do not perform enough deletions to pay for a contraction –After a contraction, we do not perform enough insertions to pay for an expansion

CS 473Lecture X31 Improving Expansion – Contraction We can improve upon this strategy by allowing α(T) to drop below ½ We continue to double the table size when an item is inserted into a full table But, we halve the table size (perform contraction) when a deletion causes α(T) < ¼ rather than α(T) < ½, Therefore, ¼ α(T) 1

CS 473Lecture X32 Improving Expansion – Contraction Hence after an expansion, α(T) = ½, thus at least half of the items in the table must be deleted before a contraction can occur. Similarly, after a contraction α(T) = ½, thus the number of items in the table must be doubled by insertions before an expansion can occur.

CS 473Lecture X33 Potential Method Define the potential function as follows Φ(T) = 0 immediately after an expansion or contraction Recall that, α(T) = ½ immediately after and expansion or contraction, therefore the potential should build up to num[T] as α(T) increases to 1 or decreases to ¼ So that the next expansion or contraction can be paid by the potential.

CS 473Lecture X34 Φ(α) w.r.t. α(T) M=num[T] when an expansion or contraction occurs

CS 473Lecture X35 Description of New Φ One such Φ is or

CS 473Lecture X36 Description of New Φ Φ = 0 when α = ½ Φ = num[T] when α = ¼ Φ = 0 for an empty table (num[T] = size[T]=0, α[T] = 0) Φ is always nonnegative

CS 473Lecture X37 Amortized Analysis Operations are: –TABLE-INSERT –TABLE-DELETE after the i-th operation

CS 473Lecture X38 Table Insert n i =n i n i-1 = n i – 1 Table contraction may not occur. α i-1 ½ Analysis is identical to that for table expansion Therefore, ĉ i = 3 whether the table expands or not. α i-1 < ½ and α i < ½ Expansion may not occur (ĉ i = 1, s i = s i-1 )

CS 473Lecture X39 Table Insert α i-1 < ½ and α i ½ α i = ½ Expansion may not occur (c i = 1 ; s i = s i-1 ; n i = s i / 2) Thus, amortized cost of a TABLE-INSERT operation is at most 3.

CS 473Lecture X40 Table Delete n i =n i n i-1 = n i + 1 Table expansion may not occur. α i-1 ½ and ¼ α i < ½ (It does not trigger a contraction) s i = s i-1 and c i = 1 and α i < ½

CS 473Lecture X41 Table Delete α i-1 = ¼ (It does trigger a contraction) s i = s i-1 /2 ; n i = s i-1 /2; and c i = n i +1 α i-1 > ½ (α i ½ ) Contraction may not occur (c i =1 ; s i = s i-1 )

CS 473Lecture X42 Table Delete α i-1 = ½ (α i < ½) Contraction may not occur c i = 1 ; s i = s i-1 ; n i = s i-1 /2; and Φ i-1 =0)

CS 473Lecture X43 Table Delete Thus, the amortized cost of a TABLE-DELETE operation is at most 2 Since the amortized cost of each operation is bounded above by a constant The actual time for any sequence of n operations on a Dynamic Table is O(n)