Hash Tables Computer Science and Engineering

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Hash Tables
Advertisements

© 2004 Goodrich, Tamassia Hash Tables
Hashing as a Dictionary Implementation
© 2004 Goodrich, Tamassia Hash Tables1  
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Data Structures Lecture 12 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Log Files. O(n) Data Structure Exercises 16.1.
HashMaps. Overview What are HashMaps? Implementing DictionaryADT with HashMaps HashMaps 2/16.
Maps, Dictionaries, Hashtables
Dictionaries and Hash Tables1  
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.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hashing General idea: Get a large array
Dictionaries 4/17/2017 3:23 PM Hash Tables  
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
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.
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  
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++,
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
1 What is it? A side order for your eggs? A form of narcotic intake? A combination of the two?
Hash Maps Rem Collier Room A1.02 School of Computer Science and Informatics University College Dublin, Ireland.
Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.
Sets and Maps Chapter 9.
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.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Hashing CSE 2011 Winter July 2018.
Slides by Steve Armstrong LeTourneau University Longview, TX
Dictionaries and Hash Tables
Review Graph Directed Graph Undirected Graph Sub-Graph
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.
Data Structures and Database Applications Hashing and Hashtables in C#
Hash Table.
Chapter 28 Hashing.
Data Structures Maps and Hash.
Data Structures and Database Applications Hashing and Hashtables in C#
Hash Tables 11/22/2018 3:15 AM Hash Tables  1 2  3  4
Hash Tables.
Dictionaries 11/23/2018 5:34 PM Hash Tables   Hash Tables.
Chapter 10 Hashing.
Dictionaries and Hash Tables
Chapter 21 Hashing: Implementing Dictionaries and Sets
Copyright © Aiman Hanna All rights reserved
Hash Tables   Maps Dictionaries 12/7/2018 5:58 AM Hash Tables  
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Dictionaries and Hash Tables
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
Hash Tables Computer Science and Engineering
Dictionaries 4/5/2019 1:49 AM Hash Tables  
Collision Handling Collisions occur when different elements are mapped to the same cell.
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Dictionaries and Hash Tables
Presentation transcript:

Hash Tables Computer Science and Engineering Chapter 8 of Goodrich and Tomassia’s Text 2/24/2019 B. Ramamurthy

Topics Hashing Hash functions Hash Tables Collision Collision resolution Chaining, linear probing, quadratic probing, double hashing Java Hash table Example 2/24/2019 B. Ramamurthy

Hashing Concept Another approach for storing and searching elements. Used in applications where add and delete, besides search, are used. Worst case linear but can get O(1) best case. 2/24/2019 B. Ramamurthy

Hash Function A hash function h maps keys of a given type into an integer interval {0, N-1} A simple function: h(x) = x mod N A good hash function will uniformly disperse the keys in the range {0, N-1} 2/24/2019 B. Ramamurthy

Hash Table Hash table ADT: Has a hash function h Array of size N Collision occurs when two keys map to the same array index. Two major collision resolution schemes are: chaining and open addressing 2/24/2019 B. Ramamurthy

Example Item(name, ssn) where ssn is 9 digit positive integer. Hash table N = 10000. Hash function: last four digits of of x Use chaining to handle collision. 2/24/2019 B. Ramamurthy

Hash Table: Example 1 2 3 045-34-0002 . 9996 9997 9998 9999 045-35-9996 567-34-9996 2/24/2019 B. Ramamurthy

Hash Functions Usually specified as two components: Component1 called the hash code map, collects the parts of the data and maps them to integers (numeric data) H1: keys  integers Component2 called the compression map, takes the integers and maps them to 0 to N-1. H2: integers {0, N-1} 2/24/2019 B. Ramamurthy

Hash Code Maps Memory address of data Integer cast of non-numeric data: bit/byte pattern of data Sum of all components: add the ascii values of your last name Polynomials of various parts of data. 2/24/2019 B. Ramamurthy

Compression maps Reminder of division (mod) h(y) = y mod N Multiple, Add and Divide (MAD) h(y) = (a*y + b) mod N where a mod N <> 0 (otherwise everything will map to b!) 2/24/2019 B. Ramamurthy

Linear Probing Linear probing resolves collision by placing the colliding item in the next available empty cell. Each entry inspected is referred to as the “probe” Example: h(x) = x mod 13 Insert keys: 18, 41, 22, 44, 59, 32, 31, 73, in that order 2/24/2019 B. Ramamurthy

Search with linear probing: find(k) j = h(k); probe = 0; while (p < N) 2.1 c = A[j] 2.2 if (c == null) return NOT_FOUND; else if (c.key() == k) return c.element(); else j = (j+1) mod N p = p +1; 3. return NOT_FOUND; 2/24/2019 B. Ramamurthy

Double Hashing h1: primary hash function If it results in collision, resolve by applying another hash function, secondary hash function; here is an example of such a function. d(k) = q – k mod q Where q < N, Possible values of d(k) are 1,2,3,..q It cannot be 0 When collision occurs: (h(k)+j*d(k)) mod N j = 0, 1, ..N-1 2/24/2019 B. Ramamurthy

Example h(k) = k mod 13 d(k) = 7 – k mod 7 Insert 18, 41, 22, 44, 59, 32, 31, 73. 2/24/2019 B. Ramamurthy

Java support for Hashing Java1.4 API Hashtable since jdk1.0 HashMap since jdk1.2 2/24/2019 B. Ramamurthy

Hashtable/HashMap Array of Entry Chaining is the collision resolution policy Entry is an inner class; Entry is a list; Entry holds a key, value and a link to next Entry Key Object should implement hashcode() and equals() methods hashcode() of the Key class is the hash code map that converts key attributes to an integer Compression map/function is hashcode mod array.length Loadfactor (how full it can get before expanding), initial capacity can be specified 2/24/2019 B. Ramamurthy

Hashtable vs HashMap Dictionary extends Hashtable Map, Cloneable, Serailizable Dictionary extends implements AbstractMap extends Hashtable Map, Cloneable, Serailizable implements 2/24/2019 B. Ramamurthy

Hashtable vs HashMap Hashtable implements Dictionary which is deprecated and has been superceded by AbstractMap Hashtable implementation is a synchronized for concurrent access. HashMap leaves synchronization to the application. Hashtable is slower (due to synchronized implementation) 2/24/2019 B. Ramamurthy

HashMap 2/24/2019 B. Ramamurthy

Using HashMap Problem: Consider employee data that has last name, first name, employee number (5 digit integer), and salary. We want to maintain a data structure of 100 temporary employees for easy add, remove and search. Solution: HashMap 2/24/2019 B. Ramamurthy

Using HashMap Problem: An employee data is made up of Name (first, last), employee number (Integer), and salary. Maintain a set of 50 employee data for a “temp agency” so that employees can be added, removed and searched easily. Solution: Choice of data structure: HashMap Key? Lets consider employee number as key in the first version In the second version we will consider name as the key Name is a class of two attributes: first, last 2/24/2019 B. Ramamurthy

TempAgency: Design 2/24/2019 B. Ramamurthy

More hashCode() method hashCode() is predefined in Object class for all classes. The default value that hashCode() returns is the memory address of the object. Where is hashCode() called in the HashMap? In the put(), get(), remove() and others that compute the hash index in the two phase hashing (hash code map and compression map) 2/24/2019 B. Ramamurthy

Summary Hashing offers a convenient way to store and search for data. The data considered in the form of {key, value} pair. The location of a data depends only on its key, not on its “successor” and/or “predecessor” key. 2/24/2019 B. Ramamurthy