Resolving collisions: Open addressing

Slides:



Advertisements
Similar presentations
Hash Tables.
Advertisements

Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Hashing CS 3358 Data Structures.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Hashing General idea: Get a large array
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 4 Search Algorithms.
1 Symbol Tables The symbol table contains information about –variables –functions –class names –type names –temporary variables –etc.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Ihab Mohammed and Safaa Alwajidi. Introduction Hash tables are dictionary structure that store objects with keys and provide very fast access. Hash table.
Hashing Chapter 7 Section 3. What is hashing? Hashing is using a 1-D array to implement a dictionary o This implementation is called a "hash table" Items.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
DS.H.1 Hashing Chapter 5 Overview The General Idea Hash Functions Separate Chaining Open Addressing Rehashing Extendible Hashing Application Example: Geometric.
Fundamental Structures of Computer Science II
Hashing (part 2) CSE 2011 Winter March 2018.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Data Structures Using C++ 2E
Hashing Jeff Chastine.
Design & Analysis of Algorithm Hashing (Contd.)
Hashing, Hash Function, Collision & Deletion
Hash table CSC317 We have elements with key and satellite data
Hashing - resolving collisions
Hashing Alexandra Stefan.
Hash Tables (Chapter 13) Part 2.
Handling Collisions Open Addressing SNSCT-CSE/16IT201-DS.
EEE2108: Programming for Engineers Chapter 8. Hashing
Hashing Alexandra Stefan.
Data Structures Using C++ 2E
Hash Table.
Chapter 28 Hashing.
Instructor: Lilian de Greef Quarter: Summer 2017
Hash In-Class Quiz.
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
CSE 2331/5331 Topic 8: Hash Tables CSE 2331/5331.
Hash Tables.
Data Structures and Algorithms
Chapter 21 Hashing: Implementing Dictionaries and Sets
Dictionaries and Their Implementations
Collision Resolution Neil Tang 02/18/2010
Introduction to Algorithms 6.046J/18.401J
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
Searching Tables Table: sequence of (key,information) pairs
Data Structures and Algorithms
Hash Tables – 2 Comp 122, Spring 2004.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Hashing Alexandra Stefan.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CS202 - Fundamental Structures of Computer Science II
Dictionaries Collection of pairs. Operations. (key, element)
Introduction to Algorithms
A Hash Table with Chaining
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Pseudorandom number, Universal Hashing, Chaining and Linear-Probing
EE 312 Software Design and Implementation I
Collision Resolution Neil Tang 02/21/2008
CSE 326: Data Structures Lecture #12 Hashing II
Collision Handling Collisions occur when different elements are mapped to the same cell.
Ch. 13 Hash Tables  .
Hashing.
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
EE 312 Software Design and Implementation I
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Hash Tables – 2 1.
Lecture-Hashing.
Presentation transcript:

Resolving collisions: Open addressing Store all elements within the table The space we save from the chain pointers is used instead to make the array larger. If there is a collision, probe the table in a systematic way to find an empty slot. If the table fills up, we need to enlarge it and rehash all the keys.

Open addressing: Linear Probing hash function: (h(k) + i ) mod m for i=0, 1,...,m-1 Insert : Start with the location where the key hashed and do a sequential search for an empty slot. Search : Start with the location where the key hashed and do a sequential search until you either find the key (success) or find an empty slot (failure). Delete : (lazy deletion) follow same route but mark slot as DELETED rather than EMPTY, otherwise subsequent searches will fail.

Open addressing: Linear Probing Advantage: very easy to implement Disadvantage: primary clustering Long sequences of used slots build up with gaps between them. Every insertion requires several probes and adds to the cluster. The average length of a probe sequence when inserting is

Open addressing: Quadratic Probing Probe the table at slots ( h(k) + i2 ) mod m for i =0, 1,2, 3, ..., m-1 Ease of computation: Not as easy as linear probing. Do we really have to compute a power? Clustering Primary clustering is avoided, since the probes are not sequential. But...

Open addressing: Quadratic Probing Probe sequence for hash value 3 in a table of size 16: 3 + 02 = 3 3 + 12 = 4 3 + 22 = 7 3 + 32 = 12 3 + 42 = 3 3 + 52 = 12 3 + 62 = 7 3 + 72 = 4 3 + 82 = 3 3 + 92 = 4 3 + 102 = 7 3 + 112 = 12 3 + 122 = 3 3 + 132 = 12 3 + 142 = 7 3 + 152 = 4

Open addressing: Quadratic Probing Probe sequence for hash value 3 in a table of size 19: Why did this happen? 3 + 02 = 3 3 + 12 = 4 3 + 22 = 7 3 + 32 = 12 3 + 42 = 0 3 + 52 = 9 3 + 62 = 1 3 + 72 = 14 3 + 82 = 10 3 + 92 = 8 3 + 102 = 8 3 + 112 = 10 3 + 122 = 14 3 + 132 = 1 3 + 142 = 9 3 + 152 = 0 3 + 162 = 12 3 + 172 = 7 3 + 182 = 4 i2 is not a good idea after all. Use a small variant instead: h(k)+(-1)i-1 ((i+1)/1)2

Open addressing: Quadratic Probing An element can always be inserted, All probes will be at different indices Prime table size  <= 0.5

Open addressing: Quadratic Probing Disadvantage: secondary clustering: if h(k1)==h(k2) the probing sequences for k1 and k2 are exactly the same. Is this really bad? In practice, not so much It becomes an issue when the load factor is high.

Open addressing: Double hashing The hash function is (h(k)+i h2(k)) mod m In English: use a second hash function to obtain the next slot. The probing sequence is: h(k), h(k)+h2(k), h(k)+2h2(k), h(k)+3h3(k), ... Performance : Much better than linear or quadratic probing. Does not suffer from clustering BUT requires computation of a second function

Open addressing: Double hashing The choice of h2(k) is important It must never evaluate to zero consider h2(k)=k mod 9 for k=81 The choice of m is important If it is not prime, we may run out of alternate locations very fast. If m and h2(k) are relatively prime, we’ll end up probing the entire table. A good choice for h2 is h2(k) = p  (k mod p) where p is a prime less than m.

Open addressing: Random hashing Use a pseudorandom number generator to obtain the sequence of slots that are probed.