Hashing Course: Data Structures Lecturer: Uri Zwick March 2008

Slides:



Advertisements
Similar presentations
Chapter 11. Hash Tables.
Advertisements

Hash Tables.
Hash Tables CIS 606 Spring 2010.
Hashing.
Lecture 11 oct 6 Goals: hashing hash functions chaining closed hashing application of hashing.
© 2004 Goodrich, Tamassia Hash Tables1  
CS 253: Algorithms Chapter 11 Hashing Credit: Dr. George Bebis.
Dictionaries and Their Implementations
Hash Tables How well do hash tables support dynamic set operations? Implementations –Direct address –Hash functions Collision resolution methods –Universal.
Dictionaries and Hash Tables1  
11.Hash Tables Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Directed-address tables Direct addressing is a simple technique that works well.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 48 Hashing.
Tirgul 9 Hash Tables (continued) Reminder Examples.
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Tirgul 8 Hash Tables (continued) Reminder Examples.
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
L. Grewe. Computing hash function for a string Horner’s rule: (( … (a 0 x + a 1 ) x + a 2 ) x + … + a n-2 )x + a n-1 ) int hash( const string & key )
Data Structures Hashing Uri Zwick January 2014.
Spring 2015 Lecture 6: Hash Tables
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
Implementing Dictionaries Many applications require a dynamic set that supports dictionary-type operations such as Insert, Delete, and Search. E.g., a.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Hashing Amihood Amir Bar Ilan University Direct Addressing In old days: LD 1,1 LD 2,2 AD 1,2 ST 1,3 Today: C
David Luebke 1 11/26/2015 Hash Tables. David Luebke 2 11/26/2015 Hash Tables ● Motivation: Dictionaries ■ Set of key/value pairs ■ We care about search,
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
Introduction to Algorithms 6.046J/18.401J LECTURE7 Hashing I Direct-access tables Resolving collisions by chaining Choosing hash functions Open addressing.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Data Structure & Algorithm Lecture 8 – Hashing JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
Hashing TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AA Course: Data Structures Lecturer: Haim Kaplan and Uri Zwick.
Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision handling Separate chaining.
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.
Hash table CSC317 We have elements with key and satellite data
Hashing CSE 2011 Winter July 2018.
Hashing Alexandra Stefan.
Hash Tables (Chapter 13) Part 2.
EEE2108: Programming for Engineers Chapter 8. Hashing
Hashing Alexandra Stefan.
Data Structures Using C++ 2E
Hashing Course: Data Structures Lecturer: Uri Zwick March 2008
Quadratic probing Double hashing Removal and open addressing Chaining
Advanced Associative Structures
Hash Table.
Hash Table.
Chapter 28 Hashing.
Hashing and Hash Tables
Instructor: Lilian de Greef Quarter: Summer 2017
Hash In-Class Quiz.
CSE 2331/5331 Topic 8: Hash Tables CSE 2331/5331.
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
Resolving collisions: Open addressing
Double hashing Removal (open addressing) Chaining
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.
Introduction to Algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Collision Resolution Neil Tang 02/21/2008
Slide Sources: CLRS “Intro. To Algorithms” book website
Hash Tables – 2 1.
Presentation transcript:

Hashing Course: Data Structures Lecturer: Uri Zwick March 2008 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AA

Hashing Huge universe U Hash function h Collisions 1 m-1 Hash table

Hashing with chaining We use a hash table to maintain a dictionary, a data structure that supports insert, delete and find operations. 1 m-1 i

What makes a hash function good? Should behave like a “random function” Should have a succinct representation Should be easy to compute Usually interested in families of hash functions Allows rehashing, changing table size, …

Two possible views of hashing The hash function h is fixed. The keys are random. The hash function h is chosen randomly from a family of hash functions. The keys are fixed. Typical assumption (in both scenarios):

Universal families of hash functions A family H of hash functions from U to [m] is said to be universal if and only if

A simple universal family To represent a function from the family we only need two numbers, a and b. The size m of the hash table is arbitrary.

Probabilistic analysis of chaining n – number of elements in dictionary D m – size of hash table Assume that h is randomly chosen from a universal family H Expected Worst-case Insert O(1) Delete/Find O(1+n/m) O(n)

Perfect hashing Suppose that D is static. We want to implement Find is O(1) worst case time. Perfect hashing: No collisions Can we achieve it?

Expected no. of collisions

Expected no. of collisions If we are willing to use m=n2, then any universal family contains a perfect hash function. No collisions!

2-level hashing

2-level hashing

Assume that each hi can be represented using 2 words 2-level hashing Assume that each hi can be represented using 2 words Total size:

A randomized algorithm for constructing a perfect 2-level hash table: Choose a random h from H(n) and compute the number of collisions. If there are more than n collisions, repeat. For each cell i,if ni>1, choose a random hash function from H(ni2). If there are any collisions, repeat. Expected construction time – O(n) Worst case find time – O(1)

Hashing with open addressing Hashing without pointers Insert key k in the first free position among Assumed to be a permutation No room found  Table is full To search, follow the same order

Hashing with open addressing

Hashing with open addressing Caution: When we delete elements, do not set the corresponding cells to nil!

Probabilistic analysis of open addressing n – number of elements in dictionary D m – size of hash table =n/m – load factor (Note: 1) Assume that for every k, h(k,0),…,h(k,m-1) is random permutation Expected time of Insert or Find is:

Probabilistic analysis of open addressing Expected time of Insert or Find is: Non rigorous proof: If we choose a random element in the table, the probability that it is full is . The probability that the first i locations in the probe sequence are all occupied is therefore i.

Implementation of open addressing How do we define h(k,i) ? Linear probing: Quadratic probing: Double hashing: