Docsity

Hashing: Understanding Hash Functions, Hash Tables, and Collisions in Java, Study notes of Computer Science

Partial preview of the text

Download Hashing: Understanding Hash Functions, Hash Tables, and Collisions in Java and more Study notes Computer Science in PDF only on Docsity!

Hashing

CMSC 132

Department of Computer Science

University of Maryland, College Park

Overview

Hashing Scattering Hash Values Hash Function Open Addressing Chaining Hashing in Java

Hashing

Hash Table

Array indexed using hash values Hash Table A with size N Indices of A range from 0 to N-1 Store in A[ hashValue % N]

Scattering Hash Values

hashCode is a 32-bit signed int

Have to reduce it to 0..N-

Could use Math.abs(key.hashCode() % N)

might not distribute values well, particularly if N is apower of 2

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

Hash Function

Example hashCode("apple") = 5hashCode("watermelon") = 3hashCode("grapes") = 8hashCode("kiwi") = 0hashCode("strawberry") = 9hashCode("mango") = 6hashCode("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

Hash Function

Suppose now hashCode("apple") = 5hashCode("watermelon") =

hashCode("grapes") = 8hashCode("kiwi") = 0hashCode("strawberry") = 9hashCode("mango") = 6hashCode("banana") = 2hashCode(“orange") =

Collision

Same hash value formultiple keys

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

Examine entry at A[ bucket(X) ] If entry = X, found If entry = empty, X not in hash table 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 unoccupied location

Open Addressing Example

Operations

Insert A, Insert B, Insert C, Insert D

ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ A ΛΛΛΛ ΛΛΛΛ

ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ A B ΛΛΛΛ

ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ A B C

D ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ A B C

Open Addressing Example

Operations

Find A,

Find B,

Find C,

Find D

D ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ A B C

D ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ A B C

D ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ ΛΛΛΛ A B C

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

Operations

Insert A,

Insert B,

Insert C

A

A B

C B

A

Chaining Example

Operations

Find B,

Find A

C B

A

C B

A