Design and Analysis of Algorithms Hash Tables Haidong Xue Summer 2012, at GSU.

Slides:



Advertisements
Similar presentations
1 11. Hash Tables Heejin Park College of Information and Communications Hanyang University.
Advertisements

Hash Tables.
Hashing.
Hash Tables:. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
© 2004 Goodrich, Tamassia Hash Tables1  
CS 253: Algorithms Chapter 11 Hashing Credit: Dr. George Bebis.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
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.
Maps, Dictionaries, Hashtables
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.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Hash Tables1 Part E Hash Tables  
Design and Analysis of Algorithms - Chapter 71 Hashing b A very efficient method for implementing a dictionary, i.e., a set with the operations: – insert.
Tirgul 9 Hash Tables (continued) Reminder Examples.
Hashing for dummies 1 For quick search, insert and delete of data from a table. 1 figures and concepts from MITOPENCOURSEWARE
Hash Tables1 Part E Hash Tables  
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.
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
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.
Design and Analysis of Algorithms Dynamic Set Model Haidong Xue Summer 2012, at GSU.
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:
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 Hashing is another method for sorting and searching data.
© 2004 Goodrich, Tamassia Hash Tables1  
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.
CS201: Data Structures and Discrete Mathematics I Hash Table.
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.
Hashing Basis Ideas A data structure that allows insertion, deletion and search in O(1) in average. A data structure that allows insertion, deletion and.
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.
Midterm Midterm is Wednesday next week ! The quiz contains 5 problems = 50 min + 0 min more –Master Theorem/ Examples –Quicksort/ Mergesort –Binary Heaps.
Instructor Neelima Gupta Expected Running Times and Randomized Algorithms Instructor Neelima Gupta
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.
1 Hash Tables Chapter Motivation Many applications require only: –Insert –Search –Delete Examples –Symbol tables –Memory management mechanisms.
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:
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Hashing Jeff Chastine.
Hash table CSC317 We have elements with key and satellite data
CS 332: Algorithms Hash Tables David Luebke /19/2018.
Hashing Alexandra Stefan.
CS223 Advanced Data Structures and Algorithms
CSE 2331/5331 Topic 8: Hash Tables CSE 2331/5331.
Introduction to Algorithms 6.046J/18.401J
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
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
Chapter 11: Hash Tables.
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Chapter 11: Hash Tables.
CS223 Advanced Data Structures and Algorithms
CS 3343: Analysis of Algorithms
Hash Tables: Associative Containers with Constant Time Operations --- On Average Consider the problem of computing the frequency of words.
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Presentation transcript:

Design and Analysis of Algorithms Hash Tables Haidong Xue Summer 2012, at GSU

Dictionary operations INSERT DELETE SEARCH O(1) “A hash table is an effective data structure for implementing dictionaries” – textbook page 253 Very likelyWorst case O(1)

Direct-address tables Direct-address table: SEARCH(S, 6) INSERT(S, ) DELETE(S, ) 7 4 O(1) What’s the problem here? When the range of element is in [1, 30000]….. Direct-addressing: use keys as addresses

Hash tables Can we have O(1) INSERT, DELETE AND SEARCH with less storage? Hash Table: Hash Function: h(x) = x mod 3 h(2) = 2 mod 3 = 2 h(3) = 3 mod 3 = 0 h(6) = 6 mod 3 = 0 h(1) = 1 mod 3 = 1 h(7) = 7 mod 3 = 1 h(5) = 5 mod 3 = 2 Multiple elements in one slot Collision! Yes!

Hash tables 0 12 Hash Table: SEARCH(S, 6) INSERT(S, ) DELETE(S, ) 7 4 O(1)+2 DELETE in 1-linked-list SEARCH in 0-linked-list INSERT in 1-linked-list O(1)+O(1) = O(1) (2 is the length of the linked-list) h(6)=6 mod 3=0 h(4)=4 mod 3=1 h(7)=7 mod 3=1 A common method is to put them into a linked-list, i.e. chaining What is the upper bound length? What is the average length?

Analysis of hash tables 0 12 Hash Table: 34 …….. n m m-1 … … …… … … Uniform hashing “each key is equally likely to hash to any of the m slots”

Analysis of hash tables …….. m-1 … … …… … … Therorem11.1 Unsuccessful search: Therorem11.2 Successful search: How to get uniform hashing? With the assumption of uniform hashing

Hash functions How to get uniform hashing? Uniform hashing “each key is equally likely to hash to any of the m slots” Division hashing Multiplication hashing Universal hashing To achieve this goal, many hashing methods are proposed:

Hash functions – division hashing h(k) = k mod m where k is value of key, m is the number of slots E.g.: – Final grades of all my students with a hash table of 10 slots – Items in grocery stores with a hash table of 10 slots 99 cents, large soda $1.99, ground beef $6.99, lamb What’s the problem here? What if we still use 10 slots?

Hash functions – division hashing What’s the problem here? e.g.: 99 mod 7 = mod 7 = mod 7 = 6

Hash functions – multiplication hashing h(k) = floor(m(kA mod 1)) where m is the number of slots and A is a constant number in (0, 1) E.g.: A=0.123, m=10 – 99*0.123= – 199*0.123= – 699*0.123= h(99)=floor(10*0.177)=1 h(199)=floor(10*0.477)=4 h(699)=floor(10*0.977)=9

Hash functions – universal hashing Theorem 11.3

Another method to deal with collisions: Open Address

Open addressing: 3612 Another method to deal with collisions: Open Address 3612 h(2, 0)=((2 mod 3) +0)mod 10=2 h(3, 0)=((3 mod 3) +0)mod 10=0 h(6, 0)=((6 mod 3) +0)mod 10=0 h(6, 1)=((6 mod 3) +1)mod 10=1 h(1, 0)=((1 mod 3) +0)mod 10=1 h(1, 1)=((1 mod 3) +1)mod 10=2 h(1, 2)=((1 mod 3) +2)mod 10=3