Hashing: A Comprehensive Approach with Java at University of Maryland, College Park - Prof, Study notes of Computer Science

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-nhu
koofers-user-nhu ๐Ÿ‡บ๐Ÿ‡ธ

8 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 132:
Object-Oriented Programming II
Hashing
Department of Computer Science
University of Maryland, College Park
Overview
Hashing
Scattering Hash Values
Hash Function
Hash Tables
Open Addressing
Chaining
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Hashing: A Comprehensive Approach with Java at University of Maryland, College Park - Prof and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Hashing

Department of Computer Science

University of Maryland, College Park

Overview

Hashing

Scattering Hash Values Hash Function

Hash Tables

Open Addressing Chaining

Hashing

Approach

Use hash function to convert key into number (hash value) used as index in hash table

Hashing

Hash Table

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

Multiplicative congruency method

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)

The % operator is integer remainder

x % y == x โ€“ y * ( x / y )

Not what mathematicians expect

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

Use Math.abs( x % N )

Rather than Math.abs(x) % N

Art and Magic of hashCode( )

There is no โ€œrightโ€ hashCode function

Art involved in finding good hashCode function Also for finding hashCode to hashBucket function

From java.util.HashMap

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

Example

hashCode("apple") = 5 hashCode("watermelon") = 3 hashCode("grapes") = 8 hashCode("kiwi") = 0 hashCode("strawberry") = 9 hashCode("mango") = 6 hashCode("banana") = 2

Perfect hash function

Unique values for each key

kiwi

banana

watermelon

apple

mango

grapes

strawberry

0 1 2 3 4 5 6 7 8 9

Open Addressing Hashing

Approach

Hash table contains objects Probe โ‡’ examine table entry Collision Move K entries past current location Wrap around table if necessary Find location for X

  1. Examine entry at A[ key(X) ]
  2. If entry = X, found
  3. If entry = empty, X not in hash table
  4. Else increment location by K, repeat

Open Addressing Hashing

Approach

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

Hash codes

H(A) = 6 H(C) = 6

H(B) = 7 H(D) = 7

Hash table

Size = 8 elements ฮ› = empty entry

  • = non-empty marker

Linear probing

Collision โ‡’ move 1 entry past current location

Open Addressing Example

Operations

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

Load factor = entries / table size

Hashing is efficient for load factor < 90%

Chaining (Bucket Hashing)

Approach

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

Hash codes

H(A) = 6 H(C) = 6

H(B) = 7 H(D) = 7

Hash table

Size = 8 elements ฮ› = empty entry

Chaining Example

Operations

Insert A, Insert B, Insert C

A

A

B

C

B

A

Hashing in Java

Collections

hashMap & hashSet implement hashing

Objects

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