Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Slides:



Advertisements
Similar presentations
Hash Tables CSC220 Winter What is strength of b-tree? Can we make an array to be as fast search and insert as B-tree and LL?
Advertisements

CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
HASH TABLE. HASH TABLE a group of people could be arranged in a database like this: Hashing is the transformation of a string of characters into a.
Skip List & Hashing CSE, POSTECH.
Hashing as a Dictionary Implementation
Appendix I Hashing. Chapter Scope Hashing, conceptually Using hashes to solve problems Hash implementations Java Foundations, 3rd Edition, Lewis/DePasquale/Chase21.
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys. Operations.  get(theKey)  put(theKey, theElement)  remove(theKey) 5/2/20151.
© 2004 Goodrich, Tamassia Hash Tables1  
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
Hashing CS 3358 Data Structures.
Maps, Dictionaries, Hashtables
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 48 Hashing.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
Hash Tables1 Part E Hash Tables  
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
Hashing as a Dictionary Implementation Chapter 20 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Hash Tables1   © 2010 Goodrich, Tamassia.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Hashing Hashing is another method for sorting and searching data.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing as a Dictionary Implementation Chapter 19.
Hashing 8 April Example Consider a situation where we want to make a list of records for students currently doing the BSU CS degree, with each.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Hashing Suppose we want to search for a data item in a huge data record tables How long will it take? – It depends on the data structure – (unsorted) linked.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Sets and Maps Chapter 9.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Hashing.
Hashing CSE 2011 Winter July 2018.
Slides by Steve Armstrong LeTourneau University Longview, TX
Hashing Alexandra Stefan.
Hashing Alexandra Stefan.
Dictionaries 9/14/ :35 AM Hash Tables   4
Chapter 28 Hashing.
Chapter 21 Hashing: Implementing Dictionaries and Sets
Dictionaries and Their Implementations
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
Advanced Implementation of Tables
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Sets and Maps Chapter 9.
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Contents What Is Hashing? Hash Functions  Computing Hash Codes  Compressing a Hash Code into an Index for the Hash Table Copyright ©2012 by Pearson Education, Inc. All rights reserved

Contents Resolving Collisions  Open Addressing with Linear Probing  Open Addressing with Quadratic Probing  Open Addressing with Double Hashing  A Potential Problem with Open Addressing  Separate Chaining Copyright ©2012 by Pearson Education, Inc. All rights reserved

Objectives Describe basic idea of hashing Describe purpose of hash table, hash function, perfect hash function Explain why to override method hashCode for objects used as search keys Describe how hash function compresses hash code into index to the hash table Copyright ©2012 by Pearson Education, Inc. All rights reserved

Objectives Describe algorithms for dictionary operations getValue, add, and remove when open addressing resolves collisions Describe separate chaining as method to resolve collisions Copyright ©2012 by Pearson Education, Inc. All rights reserved

Objectives Describe algorithms for dictionary operations getValue, add, and remove when separate chaining resolves collisions Describe clustering and problems it causes Copyright ©2012 by Pearson Education, Inc. All rights reserved

What Is Hashing? Method to locate data quickly  Ideally has O(1) search times  Yet cannot do easy traversal of data items Technique that determines index using only a search key Hash function locates correct item in hash table  Maps or “hashes to” entry Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 21-1 A hash function indexes its hash table Copyright ©2012 by Pearson Education, Inc. All rights reserved

Typical Hashing Algorithm will  Convert search key to integer called hash code.  Compress hash code into range of indices for hash table. Typical hash functions not perfect  Can allow more than one search key to map into single index  Causes “collision” in hash table Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 21-2 A collision caused by the hash function h Copyright ©2012 by Pearson Education, Inc. All rights reserved

Computing Hash Codes Must override Java Object method hashCode Guidelines for new hashCode method  If class overrides method equals, it should override hashCode.  If method equals considers two objects equal, hashCode must return same value for both objects. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Computing Hash Codes Guidelines continued …  If you call an object’s hashCode more than once during execution of a program, and if object’s data remains same during this time, hashCode must return the same value.  Object’s hash code during one execution of a program can differ from its hash code during another execution of the same program. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Hash Code for a String Assign integer to each character in string  Use 1 – 26 for ‘a’ to ‘z’  Use Unicode integer Possible to sum the integers of the characters for the hash code Better solution  Multiply Unicode value of each character by factor based on character’s position Copyright ©2012 by Pearson Education, Inc. All rights reserved

Hash Code for a Primitive Type For int  Use the value For byte, short, or char  Cast into an int Other primitive types  Manipulate internal binary representations Copyright ©2012 by Pearson Education, Inc. All rights reserved

Compressing a Hash Code Scale an integer by using Java % operator  For code c and size of table n, use c % n  Result is remainder of division If n is prime, provides values distributed in range 0 to n – 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Resolving Collisions Open addressing scheme locates alternate open location in hash table Linear probing  Collision at a[k]  Check for open slot at a[k + 1], a[k+2], etc. For retrievals  Must check for agreement of search key in successive elements of array Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 21-3 The effect of linear probing after adding four entries whose search keys hash to the same index Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 21-4 A revision of the hash table shown in Figure 21-3 when linear probing resolves collisions; each entry contains a search key and its associated value Copyright ©2012 by Pearson Education, Inc. All rights reserved

Removals Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 21-5 A hash table if remove used null to remove entries

Removals Problem  h( ) goes to location 52  Collision occurs  Linear probing cannot find desired data Removal must be marked differently  Instead of null, use a value to show slot is available but location’s entry was removed Location reused later for an add Copyright ©2012 by Pearson Education, Inc. All rights reserved

Clustering When collisions resolved with linear probing  Groups of consecutive locations occupied  Called primary clustering If clusters grow (size and number) can cause problems  Longer searches for retrievals Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 21-6 A linear probe sequence (a) after adding an entry; (b) after removing two entries; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 21-6 A linear probe sequence; (c) after a search; (d) during the search while adding an entry; (e) after an addition to a formerly occupied location Copyright ©2012 by Pearson Education, Inc. All rights reserved

Open Addressing with Quadratic Probing Avoid primary clustering by changing the probe sequence Alternative to going to location k + 1  Go to k + 1, then k + 4, then k + 9  In general go to k + j 2 for j = 1, 2, 3, … Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 21-7 A probe sequence of length five using quadratic probing

Open Addressing with Double Hashing Use second hash function to compute increments in key-dependent way Second has function should reach entire table Avoids both primary and secondary clustering Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 21-8 The first three locations in a probe sequence generated by double hashing for the search key 16 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Potential Problem with Open Addressing Frequent additions and removals  Can cause every location in hash table to reference either current entry or former entry Could result in unsuccessful search requiring check of every location Possible solutions  Increase size of hash table (see Ch. 22)  Separate chaining Copyright ©2012 by Pearson Education, Inc. All rights reserved

Separate Chaining Each location of hash table can represent multiple values  Called a “bucket” To add, hash to bucket, insert data in first available slot there To retrieve, hash to bucket, traverse bucket contents To delete, hash to bucket, remove item Copyright ©2012 by Pearson Education, Inc. All rights reserved

Separate Chaining Bucket representation  List (sorted or not)  Chain of linked nodes  Array or vector Arrays or vectors require extra overhead Linked list, chain of linked nodes is reasonable choice Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 21-9 A hash table for use with separate chaining; each bucket is a chain of linked nodes Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE Where to insert new entry into linked bucket when integer search keys are (a) unsorted and possibly duplicate; Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE Where to insert new entry into linked bucket when integer search keys are (b) unsorted and distinct; Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE Where to insert new entry into linked bucket when integer search keys are (c) sorted and distinct Copyright ©2012 by Pearson Education, Inc. All rights reserved

End Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved