Hashing.

Slides:



Advertisements
Similar presentations
Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.
Advertisements

1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
§4 Open Addressing 2. Quadratic Probing f ( i ) = i 2 ; /* a quadratic function */ 【 Theorem 】 If quadratic probing is used, and the table size is prime,
An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
Hashing General idea Hash function Separate Chaining Open Addressing
Lecture 11 oct 6 Goals: hashing hash functions chaining closed hashing application of hashing.
Lecture 10 Sept 29 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
1 Hashing: Collision Resolution Schemes Collision Resolution Techniques Introduction to Separate Chaining Collision Resolution using Separate Chaining.
CSE 326 Hashing Richard Anderson (instead of Martin Tompa)
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
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.
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.
L. Grewe. Computing hash function for a string Horner’s rule: (( … (a 0 x + a 1 ) x + a 2 ) x + … + a n-2 )x + a n-1 ) int hash( const string & key )
Hashing. Hashing as a Data Structure Performs operations in O(c) –Insert –Delete –Find Is not suitable for –FindMin –FindMax –Sort or output as sorted.
§3 Separate Chaining ---- keep a list of all keys that hash to the same value struct ListNode; typedef struct ListNode *Position; struct HashTbl; typedef.
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.
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
1.  We’ll discuss the hash table ADT which supports only a subset of the operations allowed by binary search trees.  The implementation of hash tables.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
Hashing Vishnu Kotrajaras, PhD. What do we want to do? Insert Delete find (constant time) No sorting No Findmin findmax.
Hashing as a Dictionary Implementation Chapter 19.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
Lecture 17 April 11, 11 Chapter 5, Hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.
CMSC 341 Hashing Readings: Chapter 5. Announcements Midterm II on Nov 7 Review out Oct 29 HW 5 due Thursday CMSC 341 Hashing 2.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
Hashing Vishnu Kotrajaras, PhD Nattee Niparnan, PhD.
Implementing the Map ADT.  The Map ADT  Implementation with Java Generics  A Hash Function  translation of a string key into an integer  Consider.
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
Hashing1 Maps and hashing. Dr.Alagoz Hashing2 Maps A map models a searchable collection of key- value entries Typically a key is a string with an associated.
Building Java Programs Generics, hashing reading: 18.1.
Fundamental Structures of Computer Science II
Hashing: Collision Resolution Schemes
Hashing.
Hashing.
Hashing Problem: store and retrieving an item using its key (for example, ID number, name) Linked List takes O(N) time Binary Search Tree take O(logN)
CMSC 341 Hashing.
Hashing CSE 2011 Winter July 2018.
Week 8 - Wednesday CS221.
Lecture 17 April 11, 11 Chapter 5, Hashing dictionary operations
Building Java Programs
Chapter 21 Hashing: Implementing Dictionaries and Sets
Collision Resolution Neil Tang 02/18/2010
CMSC 341 Hashing 12/2/2018.
Searching Tables Table: sequence of (key,information) pairs
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CMSC 341 Hashing 2/18/2019.
A Hash Table with Chaining
CMSC 341 Hashing.
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
Tree traversal preorder, postorder: applies to any kind of tree
CMSC 341 Hashing 4/11/2019.
Collision Resolution Neil Tang 02/21/2008
CMSC 341 Hashing 4/27/2019.
Ch Hash Tables Array or linked list Binary search trees
Hashing Vishnu Kotrajaras, PhD.
Hashing based on slides by Marty Stepp
Ch. 13 Hash Tables  .
Podcast Ch21b Title: Collision Resolution
CMSC 341 Lecture 12.
Hashing: Collision Resolution Schemes
Presentation transcript:

Hashing

Data Structure: Hashing Outline Hash Function Separate Chaining Open Addressing 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Introduction 1 2 3 4 : 8 9 Somsri 152431 Sodsai 645162 Saisin 356129 Sansai 872128 Susie 543294 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Hash Function 1 2 3 4 : 8 9 Somsri 152431 Find 645162 f Sodsai 645162 Saisin 356129 Sansai 872128 Susie 543294 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Insertion Collision resolution 1 2 3 4 : 8 9 collision 341212 f Somsri 152431 Sodsai 645162 Insert Songsarn 341212 Saisin 356129 Sansai 872128 Susie 543294 7/20/2019 Data Structure: Hashing

Separate Chaining Hash Table 1 2 3 4 : 8 9 Linked list Table of linked lists Hash table 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Hash Table public interface Hashable { int hash( int tableSize ); } public class SeparateChainingHashTable { ... private static final int DEFAULT_TABLE_SIZE=101; private LinkedList [ ] theLists; } 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Constructor public SeparateChainingHashTable( ) { this( DEFAULT_TABLE_SIZE ); } public SeparateChainingHashTable( int size ) { theLists = new LinkedList[ nextPrime( size ) ]; for( int i = 0; i < theLists.length; i++ ) theLists[ i ] = new LinkedList( ); public void makeEmpty( ) { for( int i = 0; i < theLists.length; i++ ) theLists[ i ].makeEmpty( ); 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Hash Function public static int hash( String key, int tableSize ) { int hashVal = 0; for( int i = 0; i < key.length( ); i++ ) hashVal = 37 * hashVal + key.charAt( i ); hashVal %= tableSize; if( hashVal < 0 ) hashVal += tableSize; return hashVal; } 7/20/2019 Data Structure: Hashing

Methods insert and remove public void insert( Hashable x ) { LinkedList whichList = theLists[x.hash(theLists.length)]; LinkedListItr itr = whichList.find( x ); if( itr.isPastEnd( ) ) whichList.insert( x, whichList.zeroth( ) ); } public void remove( Hashable x ) { theLists[x.hash(theLists.length)].remove( x ); 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Methods find public Hashable find( Hashable x ) { int size=theLists.length; LinkedList chain = theLists[x.hash(size)]; return (Hashable)chain.find(x).retrieve(); } 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Methods for Primes private static int nextPrime( int n ) { if( n % 2 == 0 ) n++; while(!isPrime( n )) n = n+2; return n; } private static boolean isPrime( int n ) { if( n == 2 || n == 3 ) return true; if( n == 1 || n % 2 == 0 ) return false; for( int i = 3; i * i <= n; i += 2 ) if( n % i == 0 ) return false; return true; 7/20/2019 Data Structure: Hashing

Open Addressing Hashing Following a linked list or a chain in a hash table is costly. How to reduce this cost. Resolving collision by finding other available entry in the table. 7/20/2019 Data Structure: Hashing

Open Addressing Hash Table 1 2 3 4 : 8 9 Somsri 152431 Sodsai 645162 Saisin 356129 Sansai 872128 Susie 543294 Songsan 341212 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Delete 1 2 3 4 : 8 9 Somsri 152431 Sodsai 645162  Songsan 341212 delete Susie 543294 Saisai 412122 Sansai 872128 Saisin 356129 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Linear Probing 1 2 3 4 : 8 9 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Quadratic Probing 1 2 3 4 : 8 9 1 4 9 7/20/2019 Data Structure: Hashing

Open Addressing Hash Table public interface Hashable { int hash( int tableSize ); } class HashEntry { Hashable element; // the element boolean isActive; // false is deleted public HashEntry( Hashable e ) { this( e, true ); } public HashEntry( Hashable e, boolean i ) { element = e; isActive = i; } } public class QuadraticProbingHashTable { ... private static final int DEFAULT_TABLE_SIZE=101; private HashEntry [ ] theLists; } 7/20/2019 Data Structure: Hashing

Data Structure: Hashing constructor public QuadraticProbingHashTable( ) { this( DEFAULT_TABLE_SIZE ); } public QuadraticProbingHashTable( int size ) { allocateArray( size ); makeEmpty( ); private void allocateArray( int arraySize ) { array = new HashEntry[ arraySize ]; 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Quadratic Probing public QuadraticProbingHashTable( ) { this( DEFAULT_TABLE_SIZE ); } public QuadraticProbingHashTable( int size ) { allocateArray( size ); makeEmpty( ); private void allocateArray( int arraySize ) { array = new HashEntry[ arraySize ]; 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Method findPos private int findPos( Hashable x ) { int collisionNum = 0; int currentPos = x.hash( array.length ); while( array[ currentPos ] != null && !array[ currentPos ].element.equals( x ) ) { currentPos += 2 * ++collisionNum - 1; if( currentPos >= array.length ) currentPos -= array.length; } return currentPos; 7/20/2019 Data Structure: Hashing

Methods insert and remove public void insert( Hashable x ) { int currentPos = findPos( x ); if( isActive( currentPos ) ) return; array[ currentPos ] = new HashEntry( x, true ); // Rehash if( ++currentSize > array.length / 2 ) rehash( ); } public void remove( Hashable x ) if( isActive( currentPos ) ) array[ currentPos ].isActive = false; 7/20/2019 Data Structure: Hashing

Methods insert and remove public Hashable find( Hashable x ) { int currentPos = findPos( x ); if ( isActive(currentPos)) return array[ currentPos ].element; else return null; } private boolean isActive( int currentPos ) { return array[ currentPos ] != null && array[ currentPos ].isActive; 7/20/2019 Data Structure: Hashing

Data Structure: Hashing Method rehash private void rehash( ) { HashEntry [ ] oldArray = array; // Create a new double-sized, empty table allocateArray( nextPrime( 2*oldArray.length ) ); currentSize = 0; // Copy table over for( int i = 0; i < oldArray.length; i++ ) if(oldArray[i]!=null && oldArray[i].isActive) insert( oldArray[ i ].element ); return; } 7/20/2019 Data Structure: Hashing

Data Structure: Hashing 7/20/2019 Data Structure: Hashing