ECE 3035B
Computing Mechanisms
Fall 2004
4 problems, 8 pages
Exam Three Solutions
18 November 2004
1
Problem 1
(4 parts, 25 points)
Associative Sets
Part A
(10 points) Consider a hash table that uses
seven buckets
.
Suppose our hash function is the sum
of the character codes of the characters in the key modulo the number of buckets B=7.
Assume the
character codes are as follows (A=1, B=2, C=3, D=4, E=5, F=6, G=7, H=8, I=9, J=10, K=11, L=12,
M=13, N=14, O=15, P=16, Q=17, R=18, S=19, T=20, U=21, V=22, W=23, X=24, Y=25, Z=26).
That
is, the hash function is:
,
mod
)
(
)
(
B
k
ode
CharacterC
k
h
i
i
∑
=
where
i
= 0 … |key|-1 and
k
i
is the
i
th
character of the key.
Suppose the hash table is implemented using
closed hashing
with a
quadratic
probe
strategy to handle
collisions.
That is, the elements of the hash table are stored within the bucket array itself, using h(k) to
determine the bucket.
However, if a collision occurs, compute a series of alternative locations in the
bucket array until one with the same key or an empty one is found.
The alternative locations
h
1
(k), h
2
(k),
etc. are computed using a secondary quadratic probe function:
,
mod
)
)
(
(
)
(
2
B
j
k
h
k
h
j
+
=
where
j = 1, 2, …, B-1
. Show the result of inserting the following (key, value) pairs into this hashtable.
(“AJC”, “Georgia”), (“NYT”, “New York”), (“WPOST”, “D.C.”), (“CT”, “Chicago”), (“GLOBE”,
“Boston”).
h(AJC) = 0
h(NYT) = 3
h(WPOST) = 2
h(CT) = 2
-- conflict with WPOST, so compute h
1
(CT) = (2 + 1
2
)mod 7 = 3
-- conflict with NYT, so compute h
2
(CT) = (2 + 2
2
)mod 7 =6
h(GLOBE) = 6
-- conflict with CT, so compute h
1
(GLOBE) = (6 + 1
2
)mod 7 = 0
-- conflict with AJC, so compute h
2
(GLOBE) = (6 + 2
2
)mod 7 = 3
-- conflict with NYT, so compute h
3
(GLOBE) = (6 + 3
2
)mod 7 = 1
0
AJC
Atlanta
1
GLOBE
Boston
2
WPOST
D.C.
3
NYT
New York
4
5
6
CT
Part B
Now consider a hash table that uses a
chaining strategy
(implemented with a singly linked list) to
handle collisions.
The hash table has
seven buckets
and inserted entries are pushed on the front of the
bucket list. Suppose the hash table has an
average key length
of
five
characters and an
average bucket list
length
of
16
, (i.e., each bucket has approximately 16 entries). For the following questions assume that
computing the hash function
takes an average of
three

#### You've reached the end of your free preview.

Want to read all 8 pages?

- Fall '08
- Staff
- hash function