Theory I Algorithm Design and Analysis (8 – Dynamic tables) Prof. Th. Ottmann.

Slides:



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

Hash Tables.
ArrayLists David Kauchak cs201 Spring Extendable array Arrays store data in sequential locations in memory Elements are accessed via their index.
Hash-based Indexes CS 186, Spring 2006 Lecture 7 R &G Chapter 11 HASH, x. There is no definition for this word -- nobody knows what hash is. Ambrose Bierce,
Hash-Based Indexes The slides for this text are organized into chapters. This lecture covers Chapter 10. Chapter 1: Introduction to Database Systems Chapter.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11.
Lecture 11 oct 6 Goals: hashing hash functions chaining closed hashing application of hashing.
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.
Theory I Algorithm Design and Analysis (7 Hashing: Open Addressing)
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 Hash-Based Indexes Yanlei Diao UMass Amherst Feb 22, 2006 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
UMass Lowell Computer Science Graduate Analysis of Algorithms Prof. Karen Daniels Spring, 2005 Lecture 3 Tuesday, 2/8/05 Amortized Analysis.
Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Amortized Analysis Some of these lecture slides are adapted from CLRS.
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.
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.
Amortized Analysis.
CS333 / Cutler Amortized Analysis 1 Amortized Analysis The average cost of a sequence of n operations on a given Data Structure. Aggregate Analysis Accounting.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
Andreas Klappenecker [based on the slides of Prof. Welch]
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
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.
Data Structures Hashing Uri Zwick January 2014.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 14 Prof. Charles E. Leiserson.
IS 2610: Data Structures Searching March 29, 2004.
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.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner.
Search  We’ve got all the students here at this university and we want to find information about one of the students.  How do we do it?  Linked List?
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Indexed Sequential Access Method.
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.
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.
David Luebke 1 12/12/2015 CS 332: Algorithms Amortized Analysis.
Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision handling Separate chaining.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
CMSC 341 Hashing Readings: Chapter 5. Announcements Midterm II on Nov 7 Review out Oct 29 HW 5 due Thursday CMSC 341 Hashing 2.
Amortized Analysis. p2. Amortized analysis: Guarantees the avg. performance of each operation in the worst case. Aggregate method Accounting method Potential.
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.
Amortized Analysis.
Amortized Analysis of Rehashing
Andreas Klappenecker [partially based on the slides of Prof. Welch]
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Amortized Analysis (chap. 17)
Table Amortized cost: $3 Insert 5 Actual cost: $1.
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
ADT Heap data structure
Hashing CS2110 Spring 2018.
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
Hashing II CS2110 Spring 2018.
Hashing CS2110.
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
CS 332: Algorithms Amortized Analysis Continued
Linked List Intro CSCE 121.
CS 113: Data Structures and Algorithms
Jordi Cortadella and Jordi Petit Department of Computer Science
Presentation transcript:

Theory I Algorithm Design and Analysis (8 – Dynamic tables) Prof. Th. Ottmann

Dynamic Tables Problem: Maintenance of a table under the operations insert and delete such that the table size can be adjusted to the number of elements a fixed portion of the table is always filled with elements the costs for n insert or delete operations are in O(n). Organisation of the table: hash table, heap, stack, etc. Load factor  T : fraction of table spaces of T which are occupied. Cost model: Insertion or deletion of an element causes cost 1, if the table is not filled yet. If the table size is changed, all elements must be copied.

Initialisation class dynamicTable { private int [] table; private int size; private int num; dynamicTable () { table = new int [1];// initialize empty table size = 1; num = 0; }

Expansion strategy: insert Double the table size whenever an element is inserted in the fully occupied table! public void insert (int x) { if (num == size) { int[] newTable = new int[2*size]; for (int i=0; i < size; i++) insert table[i] in newTable; table = newTable; size = 2*size; } insert x in table; num = num + 1; }

insert operations in an initially empty table t i = cost of the i-th insert operation Worst case: t i = 1, if the table was not full before operation i t i = (i – 1) + 1, if the table was full before operation i Hence, n insert operations require costs of at most Amortized worst case: Aggregate analysis, accounting method, potential method

Potential method T table with k = T.num elements and s = T.size spaces Potential function  (T) = 2 k – s

Properties of the potential function Properties  0 =  (T 0 ) =  (empty table) = -1 For all i  1 :  i =  (T i )  0 Since  n -  0  0,  a i is an upper bound for  t i Directly before an expansion, k = s, hence  (T) = k = s. Directly after an expansion, k = s/2, hence  (T) = 2k – s = 0.

Amortized cost of insert (1) k i = # elements in T after the i-th operation s i = table size of T after the i-th operation Case 1: [ i-th operation does not trigger an expansion]

Case 2: [ i-th operation triggers an expansion] Amortized cost of insert (2)

Insertion and deletion of elements Now: contract table, if the load is too small! Goals: (1) Load factor is always bounded below by a constant (2) Amortized cost of a single insert or delete operation is constant. First attempt: Expansion: same as before Contraction: halve the table size as soon as table is less than ½ occupied (after the deletion)!

„Bad“ sequence of insert and delete operations Cost n/2 times insert (table fully occupied) 3 n/2 I: expansionn/2 + 1 D, D: contractionn/2 + 1 I, I : expansionn/2 + 1 D, D: contraction Total cost of the sequence I n/2,I,D,D,I,I,D,D,... of length n:

Second attempt Expansion: (as before) double the table size, if an element is inserted in the full table. Contraction: As soon as the load factor is below ¼, halve the table size. Hence: At least ¼ of the table is always occupied, i.e. ¼   (T)  1 Cost of a sequence of insert and delete operations?

Analysis: insert and delete k = T.num, s = T.size,  = k/s Potential function 

Analysis: insert and delete Directly after an expansion or contraction of the table: s = 2k, hence  (T) = 0

insert i-th operation: k i = k i Case 1:  i-1  ½ Case 2:  i-1 < ½ Case 2.1:  i < ½ Case 2.2:  i  ½

insert Case 2.1:  i-1 < ½,  i < ½ (no expansion) Potential function 

insert Case 2.2:  i-1 < ½,  i  ½ (no expansion) Potential function 

delete k i = k i Case 1:  i-1 < ½ Case 1.1: deletion causes no contraction s i = s i-1 Potential function 

delete Case 1.2:  i-1 < ½ deletion causes a contraction 2s i = s i –1 k i-1 = s i-1 /4 k i = k i Case 1:  i-1 < ½ Potential function 

delete Case 2:  i-1  ½ no contraction s i = s i –1 k i = k i Case 2.1:  i-1  ½ Potential function 

delete Case 2:  i-1  ½ no contraction s i = s i –1 k i = k i Case 2.2:  i < ½ Potential function 