







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of hashing concepts, including scattering hash values, hash functions, and hash tables. It also covers open addressing and chaining methods, and discusses the art and magic of java's hashcode() function. Students will learn about hash functions, their importance, and how they are used to index hash tables.
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Scattering Hash Values Hash Function
Open Addressing Chaining
Hashing
Use hash function to convert key into number (hash value) used as index in hash table
Hashing
Array indexed using hash values Hash table A with size N Indices of A range from 0 to N- Store in A[ hashValue % N]
Scattering Hash Values
Produces good hash values Hash value = Math.abs((a * key.hashCode()) % N) Where N is table size a, N are large primes
Beware of % (Modulo Operator)
x % y == x โ y * ( x / y )
3/2 == 1 3%2 == 1 2/2 == 1 2%2 == 0 1/2 == 0 1%2 == 1 0/2 == 0 0% 2 == 0 (โ 1)/2 == 0 (โ 1)%2 == โ 1 (โ 2)/2 == โ 1 (โ 2)%2 == 0 (โ 3)/2 == โ 1 (โ 3)%2 == โ 1
Rather than Math.abs(x) % N
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);
Hash Function
hashCode("apple") = 5 hashCode("watermelon") = 3 hashCode("grapes") = 8 hashCode("kiwi") = 0 hashCode("strawberry") = 9 hashCode("mango") = 6 hashCode("banana") = 2
Unique values for each key
0 1 2 3 4 5 6 7 8 9
Open Addressing Hashing
Hash table contains objects Probe โ examine table entry Collision Move K entries past current location Wrap around table if necessary Find location for X
Open Addressing Hashing
Linear probing K = 1 May form clusters of contiguous entries Deletions Find location for X If X inside cluster, leave non-empty marker Insertion Find location for X Insert if X not in hash table Can insert X at first non-empty marker
Open Addressing Example
Size = 8 elements ฮ = empty entry
Collision โ move 1 entry past current location
Open Addressing Example
Insert A, Insert B, Insert C, Insert D
1 2 3 4 5 6 7 8 ฮ ฮ ฮ ฮ ฮ A ฮ ฮ 1 2 3 4 5 6 7 8 ฮ ฮ ฮ ฮ ฮ A B ฮ 1 2 3 4 5 6 7 8 ฮ ฮ ฮ ฮ ฮ A B C 1 2 3 4 5 6 7 8 D ฮ ฮ ฮ ฮ A B C
Efficiency of Open Hashing
Chaining (Bucket Hashing)
Hash table contains lists of objects Find location for X Find hash code key for X Examine list at table entry A[ key ] Collision Multiple entries in list for entry
Chaining Example
Size = 8 elements ฮ = empty entry
Chaining Example
Insert A, Insert B, Insert C
Hashing in Java
hashMap & hashSet implement hashing
Built-in support for hashing boolean equals(object o) int hashCode( ) Can override with own definitions Must be careful to support Java contract if a.equals(b) == true then a.hashCode( ) == b.hashCode( ) must be true