1 What is it? A side order for your eggs? A form of narcotic intake? A combination of the two?

Slides:



Advertisements
Similar presentations
Hashing.
Advertisements

What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
© 2004 Goodrich, Tamassia Hash Tables1  
September 26, Algorithms and Data Structures Lecture VI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Hashing Techniques.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Hashing General idea: Get a large array
Hash Tables. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
1. 2 Problem RT&T is a large phone company, and they want to provide enhanced caller ID capability: –given a phone number, return the caller’s name –phone.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
Hashing Table Professor Sin-Min Lee Department of Computer Science.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
1 HashTable. 2 Dictionary A collection of data that is accessed by “key” values –The keys may be ordered or unordered –Multiple key values may/may-not.
Hash Tables1   © 2010 Goodrich, Tamassia.
Comp 335 File Structures Hashing.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
1 HASHING Course teacher: Moona Kanwal. 2 Hashing Mathematical concept –To define any number as set of numbers in given interval –To cut down part of.
Hashing Hashing is another method for sorting and searching data.
© 2004 Goodrich, Tamassia Hash Tables1  
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
CSC 172 DATA STRUCTURES. SETS and HASHING  Unadvertised in-store special: SETS!  in JAVA, see Weiss 4.8  Simple Idea: Characteristic Vector  HASHING...The.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
October 6, Algorithms and Data Structures Lecture VII Simonas Šaltenis Aalborg University
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
TOPIC 5 ASSIGNMENT SORTING, HASH TABLES & LINKED LISTS Yerusha Nuh & Ivan Yu.
M. Böhlen and R. Sebastiani 9/29/20161 Data Structures and Algorithms Roberto Sebastiani
Hash Tables 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Hashing (part 2) CSE 2011 Winter March 2018.
Hashing.
CSC 172 DATA STRUCTURES.
Hashing, Hash Function, Collision & Deletion
Hash table CSC317 We have elements with key and satellite data
Hashing CSE 2011 Winter July 2018.
Dictionaries Dictionaries 07/27/16 16:46 07/27/16 16:46 Hash Tables 
© 2013 Goodrich, Tamassia, Goldwasser
Dictionaries 9/14/ :35 AM Hash Tables   4
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Hash functions Open addressing
Advanced Associative Structures
Hash Table.
Hash Table.
Chapter 21 Hashing: Implementing Dictionaries and Sets
Dictionaries and Their Implementations
Collision Resolution Neil Tang 02/18/2010
Resolving collisions: Open addressing
CSCE 3110 Data Structures & Algorithm Analysis
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Data Structures – Week #7
Algorithms and Data Structures Lecture VI
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
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
Hash Tables Computer Science and Engineering
Advanced Implementation of Tables
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
Data Structures – Week #7
Ch. 13 Hash Tables  .
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Hashing.
What we learn with pleasure we never forget. Alfred Mercier
Data Structures and Algorithm Analysis Hashing
DATA STRUCTURES-COLLISION TECHNIQUES
EE 312 Software Design and Implementation I
Collision Resolution: Open Addressing Extendible Hashing
Presentation transcript:

1 What is it? A side order for your eggs? A form of narcotic intake? A combination of the two?

2 Searching O(n) O(lg n) O(n) O(lg n) balanced O(n) - skewed

3 Can we do better? n What if we have students in a class that have been assigned id numbers in the range 0-50? n Just store each student in his/her student number position in the array: n What is the order of insert, remove, find for such a list? n Would this be a reasonable way to store student records at a university? JaneStevePaulMary... [0][1][2][3]...

4 n Can we use the previous method if we want to use SSN as the key field? n How may positions would our array need? n How many of those would we actually use? n What we need is a way to map the range of possible keys (10 9 for SSNs) onto the available array positions. n For SSNs we could just use the last 4 digits of the SSN to determine where to store the element.

5 n This process of mapping a range of keys onto an address space is known as hashing. n The function (usually more complicated than just key mod 10,000 ) used to find the hash value for a key is called the hashing function. n What is the order of insertion and searching using hashing? n What is the order of remove? n Is there a problem with this technique?

6 Collisions n When two keys have the same hash value (they map to the same location) we have what is called a collision. n When using a hash table we must have some collision resolution method. n Some collision resolution methods include: linear probing, quadratic probing, separate chaining, double hashing

7 Linear Probing n With linear probing, when a collision occurs, the element is placed in the next available position Hash( 89) = 9 Hash( 18) = 8 Hash( 49) = 9 Hash( 58) = 8 Hash( 9) = Problem: primary clustering

8 Quadratic Probing n If the element hashes to location H, the next location we try is H+1 2, then H+2 2, H+3 2, … H+i Hash( 89) = 9 Hash( 18) = 8 Hash( 49) = 9 Hash( 58) = 8 Hash( 9) =

9 Double Hashing Use two hash functions If M is prime, eventually will examine every position in the table double_hash_insert(K) if(table is full) error probe = h1(K) offset = h2(K) while (table[probe] occupied) probe = (probe + offset) mod M table[probe] = K Many of same (dis)advantages as linear probing Distributes keys more uniformly than linear probing

10 Double Hashing Example h1(K) = K mod 13 h2(K) = 8 - K mod 8 we want h2 to be an offset to add

11 Separate Chaining n Maintain an array of linked lists. The hash function tells us which list to insert an element X, and where to find an element X Hash( 89) = 9 Hash( 18) = 8 Hash( 49) = 9 Hash( 58) = 8 Hash( 9) =

12 Pseudo-code (chaining implementation) Has 3 basic methods, and the constructor which: creates the table of M lists insert(K) index = h(K) insert into table[index] find(K) index = h(K) walk down list at table[index], looking for a match return what was found (or error) remove(K) index = h(K) walk down list at table[index], looking for a match remove what was found (or error)

13 Hash Functions Need to choose a good hash function –quick to compute –distributes keys uniformly throughout the table How to deal with hashing non-integer keys: –find some way of turning the keys into integers –for example, if the keys are phone numbers, remove the hyphen in to get ! –for a String, add up the ASCII values of the characters of your string –then use a standard hash function on the integers

14 Hash Functions Use the remainder –h(K) = K mod M –K is the key, M the size of the table Need to choose M M = b e (bad) –if M is a power of two, h(K) gives the e least significant bits of K –all keys with the same ending go to the same place M prime (good) –helps ensure uniform distribution –take a number theory class to understand why

15 Hash Functions (cont.) Mid-Square – h(K) = middle digits of K 2 –I.E. Table size power of 10 h( ) = h( ) = h( ) = –I.E. Table power is power of 2 h(1001) = h(1011) = h(1101) =

16 Theoretical Results Let  the load factor: average number of keys per array index Analysis is probabilistic, rather than worst-case

17

18 Sources: Some information in these slides was taken from the slides that accompany Data Structures and Algorithms in Java by Goodrich & Tamassia, Wiley 2014.