6.006 Intro to Algorithms
Recitation 05
February 16, 2011
Overview of Hash Tables
A hash table is a data structure that supports the following operations:
•
insert(k)
 puts key
k
into the hash table
•
search(k)
 searches for key
k
in the hash table
•
remove(k)
 removes key
k
from the hash table
In a well formed hash table, each of these operations take on average
O
(1)
time, making hash
tables a very useful data structure.
You can think of a hash table as a list of
m
slots. Inserting a key puts it in one of the slots
in the hash table, deleting a key removes it from the slot it was inserted in, and searching a key
looks in the slot the key would have been inserted into to see if it is indeed there. Empty slots are
designated with a NIL value. The big question is figuring out which slot should a key
k
be inserted
into in order to maintain the
O
(1)
runtime of these operations.
Hash Functions
Consider a function
h
(
k
)
that maps the universe
U
of keys (specific to the hash table, keys could
be integers, strings, etc. depending on the hash table) to some index 0 to
m
. We call this function
a
hash function
. When inserting, searching, or deleting a key
k
, the hash table hashes
k
and looks
at the
h
(
k
)
th slot to add, look for, or remove the key.
A good hash function
•
satisfies (approximately) the assumption of simple uniform hashing: each key is equally
likely to hash to any of the
m
slots. The hash function shouldn’t bias towards particular slots
•
does not hash similar keys to the same slot (e.g. compiler’s symbol table shouldn’t hash
variables
i
and
j
to the same slot since they are used in conjunction a lot)
•
is quick to calculate, should have
O
(1)
runtime
•
is deterministic.
h
(
k
)
should always return the same value for a given
k
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
6.006 Intro to Algorithms
Recitation 05
February 16, 2011
Example 1: Division method
The division method is one way to create hash functions. The functions take the form
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '11
 byrns
 Math

Click to edit the document details