Docsity

Hashing and Hash Table - Object Oriented Programming II | CMSC 132, Study notes of Computer Science

Partial preview of the text

Download Hashing and Hash Table - Object Oriented Programming II | CMSC 132 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

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]

Hash Function

Function for converting key into hash value For Java

Hash value

32-bit signed int

Default hash function

int hashCode( )

For hash table of size N

Must reduce hash value to 0..N – 1

Scattering Hash Values

Multiplicative congruency method

Produces good hash values Hash value = Math.abs((a * key.hashCode( )) % N) Where

N is table size a is large prime number

Beware of % (Modulo Operator)

The % operator is integer remainder

x % y == x – y * ( x / y )

Result may be negative

–|y| <

x % y

< +|y|

x % y has same sign as x

Use Math.abs( x % N )

Rather than Math.abs(x) % N In case N is negative

Hash Function

ExamplehashCode("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

applemangograpes

strawberry

0 1 2 3 4 5 6 7 8 9

Hash Function

Suppose nowhashCode("apple") = 5hashCode("watermelon") = 3hashCode("grapes") = 8hashCode("kiwi") = 0hashCode("strawberry") = 9hashCode("mango") = 6hashCode("banana") = 2hashCode(“orange") = 3 Collision

Same hash value formultiple keys

kiwi

banana

watermelon

applemangograpes

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 X1.

Examine entry at A[ key(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 non-empty marker

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