COMP 250 Winter 2010
26  hashing
March 17, 2010
Today we look at a particular kind of map, sometimes called a
hash map
.
(Java has a class
HashMap
for representing these kinds of maps.) Before we understand what a hash map is, though,
we need to understand what hashing is.
[Note: make sure you also read through the slides. There
are many figures in the slides which I have not reproduced here and which illustrate the various
maps discussed in this lecture.]
Hashing
Suppose you have a set of keys
K
. Define a
hash function
to be a mapping:
h
:
K
→ {
0
,
1
,
2
,m

1
}
where
m
is some positive integer. Specifically, for each key
k
∈
K
, the hash function specifies some
integer
h
(
k
). For hash functions, typically
m
is smaller than the number of keys in
K
. For example,
if the keys are possible social insurance numbers (10
9
of them), then we might let
m
be 1000, rather
than 10
9
. So it happens alot that two keys in
K
to map to the same integer. Also note that the
hash function
h
is a mapping that is defined on the entire set
K
and not just on a subset of
K
.
(The definition of mapping last class allowed a mapping to only include a subset of keys.).
It is very common to design hash functions by writing them as a composition of two maps. The
first map takes keys
K
to a large set of integers. The second map takes the large set of integers to a
small set of integers
{
0
,
1
,...,m

1
}
. (The reasons for this will be clear by the end of the lecture.)
The first mapping is called
hash coding
and the integer chosen for a key
k
is called the
hash code
for that key. The second mapping is called
compression
. Compression maps the hash codes to
hash
values
.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 BLANCHETTE
 Computer Science, hash function, hash

Click to edit the document details