









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Hashing
Hashing Hashing function function that maps data to a value (e.g., integer) Hash Code/Hash Value value returned by a hash function Hash functions can be used to speed up data access We can achieve O(1) data access using hashing
Approach Use hash function to convert key into number (hash value) used as index in hash table
Hash Function
Hash Function Function for converting key into hash value For hash table of size N Must reduce hash value to 0..N – 1 Can use modulo operator hash value = Math.abs(keyValue % N) Example Problem Assign 4 parking spaces to 4 people using h(key) = keyValue % 4 What happens if we have 4 spaces and 8 people? Collision Same hash value for multiple keys Bucket Each table entry can be referred to as a bucket In some implementations the bucket is represented by a list (those elements hashing to the same bucket are placed in the same list) Properties of a Good Hash Function Distributes (scatters) values uniformly across range of possible values It is not expensive to compute
Scattering Hash Values
Reduces likelihood of conflicts between keys
Satisfies definition of hash function But not very useful (all keys at same location)
Might not distribute values well Particularly if N is a power of 2
Hash Function
hash("apple") = 5
hash("watermelon") = 3 hash("grapes") = 8 hash("kiwi") = 0 hash("strawberry") = 9 hash("mango") = 6 hash("banana") = 2
Unique values for each key
0 1 2 3 4 5 6 7 8 9
Hash Function
hash("apple") = 5
hash("watermelon") = 3 hash("grapes") = 8 hash("kiwi") = 0 hash("strawberry") = 9 hash("mango") = 6 hash("banana") = 2
hash(“orange") = 3
Same hash value for multiple keys
0 1 2 3 4 5 6 7 8 9
Hashing in Java
Method int hashCode( ) provides Numerical hash value for any object 32-bit signed int
Usually just address of object in memory
Must work with equals( ) Must satisfy the “ hash code contract ”
Java Hash Code Contract
Java Hash Code Contract if a.equals(b) == true, then we must guarantee a.hashCode( ) == b.hashCode( ) Inverse is not true !a.equals(b) does not imply a.hashCode( ) != b.hashCode( ) (Though Java libraries may be more efficient) Converse is also not true a.hashCode( ) == b.hashCode( ) does not imply a.equals(b) == true hashCode() Must return same value for object in each execution, provided information used in equals( ) comparisons on the object is not modified
Java hashCode( )
Implementing hashCode( )
Include only information used by equals( ) Else 2 “equal” objects different hash values Using all / more of information used by equals( ) Help avoid same hash value for unequal objects
Example hashCode( ) functions
For pair of Strings 1 st^ letter of 1st^ str 1 st^ letter of 1st^ str + 1st^ letter of 2nd^ str Length of 1st^ str + length of 2nd^ str ∑ letter(s) of 1 st^ str + ∑ letter(s) of 2 nd^ str
Art and Magic of hashCode( )
Art involved in finding good hashCode function Also for finding hashCode to hashBucket function
static int hashBucket(Object x, int N) { int h = x.hashCode(); h += ~(h << 9); h ^= (h >>> 14); h += (h << 4); h ^= (h >>> 10); return Math.abs(h % N);