1 Hash Tables Chapter 12. 2 Motivation Many applications require only: –Insert –Search –Delete Examples –Symbol tables –Memory management mechanisms.

Slides:



Advertisements
Similar presentations
Chapter 11. Hash Tables.
Advertisements

1 11. Hash Tables Heejin Park College of Information and Communications Hanyang University.
Hash Tables.
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.
Data Structures Using C++ 2E
© 2004 Goodrich, Tamassia Hash Tables1  
CS 253: Algorithms Chapter 11 Hashing Credit: Dr. George Bebis.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1.1 Data Structure and Algorithm Lecture 9 Hashing Topics Reference: Introduction to Algorithm by Cormen Chapter 12: Hash Tables.
Hash Tables How well do hash tables support dynamic set operations? Implementations –Direct address –Hash functions Collision resolution methods –Universal.
11.Hash Tables Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Directed-address tables Direct addressing is a simple technique that works well.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
Tirgul 9 Hash Tables (continued) Reminder Examples.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Tirgul 8 Hash Tables (continued) Reminder Examples.
Lecture 10: Search Structures and Hashing
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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.
Hashtables David Kauchak cs302 Spring Administrative Talk today at lunch Midterm must take it by Friday at 6pm No assignment over the break.
Spring 2015 Lecture 6: Hash Tables
Data Structures Week 6 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 4 Search Algorithms.
Implementing Dictionaries Many applications require a dynamic set that supports dictionary-type operations such as Insert, Delete, and Search. E.g., a.
Data Structures Hash Tables. Hashing Tables l Motivation: symbol tables n A compiler uses a symbol table to relate symbols to associated data u Symbols:
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
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
Searching Given distinct keys k 1, k 2, …, k n and a collection of n records of the form »(k 1,I 1 ), (k 2,I 2 ), …, (k n, I n ) Search Problem - For key.
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.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
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.
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,
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashtables David Kauchak cs302 Spring Administrative Midterm must take it by Friday at 6pm No assignment over the break.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision handling Separate chaining.
Hashing. Search Given: Distinct keys k 1, k 2, …, k n and collection T of n records of the form (k 1, I 1 ), (k 2, I 2 ), …, (k n, I n ) where I j is.
CSC 413/513: Intro to Algorithms Hash Tables. ● Hash table: ■ Given a table T and a record x, with key (= symbol) and satellite data, we need to support:
Data Structures Using C++ 2E
Hashing Jeff Chastine.
Hash table CSC317 We have elements with key and satellite data
Hashing Alexandra Stefan.
Hashing Alexandra Stefan.
Data Structures Using C++ 2E
Hashing and Hash Tables
CSE 2331/5331 Topic 8: Hash Tables CSE 2331/5331.
Hashing.
Introduction to Algorithms 6.046J/18.401J
Resolving collisions: Open addressing
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.
Introduction to Algorithms
A Hash Table with Chaining
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Hash Tables – 2 1.
Presentation transcript:

1 Hash Tables Chapter 12

2 Motivation Many applications require only: –Insert –Search –Delete Examples –Symbol tables –Memory management mechanisms

3 Direct-address tables Assumption- unique keys are taken for a relatively small universe U={0,1,….,m-1} of keys The set is represented by an array of references (direct access table) T[0..m-1] where entry T[j] may potentially point to an object with key=j

4 U T Satellite data

5 Pseudo-code directAddressSearch(T,k) return T[k] directAddressInsert(T,x) T[key(x)]=x directAddressDelete(T,x) T[key(x)]=null

6 Hash Tables In many cases the universe U is large while the set K of keys actually stored is small. Hash tables are data structures that require a table of size O(|K|) (instead of O(|U|)) only but still provide the O(1) (in average) access time.

7 U T Hash function

8 Collisions A collision occurs when 2 different keys are mapped by h to the same slot in T –how to find hash functions that minimize collisions Collisions are inevitable –Since |U|>m there must be two keys who have the same value resolution by chaining open addressing

9 Chaining All the elements that hash to the same slot are kept in a linked list. chainedHashInsert(T,x) insert x at the head of list T[h(key(x))] chainedHashSearch(T,k) search for an element with key k in list T[h(k)] chainedHashDelete(T,x) delete x from the list T[h(key(x))]

10 Analysis Assume - n elements are stored in a hash table of size m –load factor Worst case If the hash function distributes the elements equally and can be computed in O(1) –average complexity –and if we choose m s.t n=O(m) the average complexity turns into O(1)

11 Hash Functions Assume that the keys are natural numbers. Division method –How to choose m? powers of 10 and 2 should be avoided (why) good values for m are prime not to close to powers of 2 ex- n=2000 m=701

12 Hash Functions Multiplication method – choose 0<A<1 –recommended value of A –ex k= m=10000 h(k)=|10000*fract(123456* )| =|10000*fract( )|=|10000* |=|41.151| =41

13 Other type of keys When the keys are not natural numbers we convert them. ex-strings s=“pt” ascii(‘p’) =112, ascii(‘t’)=116 nat(“pt”)= 112* =28788

14 Open Addressing All the elements are stored in the table itself (m>n). When inserting a new element, we successively probe the hash table until we find an empty slot. The sequence of positions probed should depend on the key inserted (why?)

15 Open addressing The hash function is now a function that for each probe provide a slot in the table. In order to ensure that every hash table position is considered we require probe sequence for every key to be a permutation of 0..m-1

16 Pseudo Code openHashInsert(T,x) j=0 k=key(x) repeat z=h(k,j) if T[z]=null or T[z]=Del then T[z]=x return z else j++ until j=m error “table overflow”

17 Pseudo Code openHashSearch(T,k) j=0 repeat z=h(k,j) if T[z]<> null and T[z]<>Del and key(T[z])=k then return z else j++ until T[z]=null or j=m return null

18 Deleting When deleting we must not replace the element with null (why?) –we use a special Del value to denote that the slot was once used. openHashDelete(T,x) z = openHashSearch(T,x); if z<>null then T[z]=Del

19 Hash Functions We would like the probe sequences distributed equally among the m! possible permutations- Uniform Hashing Uniform Hashing is difficult to implement –linear probing –quadratic probing –double hashing

20 Linear Probing Take an ordinary hash function then Problem- the sequential quest for empty slots causes primary clustering –long run of occupied slots.

21 Quadratic Probing Let h’ be as above: (mod m) Adequate constants must be found to ensure that all m slots are probed. –Ex Suffers from secondary clustering

22 Double Hashing The hash function is: In order to probe all the table h’’(k) must be relatively prime to m. choose m to be a power of 2 and h’’ to return a odd number choose m to be prime and h’(k) = k mod m and h’’(k)=1+(k mod m’) where m’ is slightly less than m

23 Analysis Assuming a uniform hashing and load factor the average number of probes is at most: –unsuccessful search –successful search