CISC220Lab9

advertisement
Lab 9:
(48 pts) For this lab, you may work with a partner or you may work on your own.
NOTE: Due to the upcoming celebration, this lab will be due Sunday, May 3.
Problem 1:
3413, 3869, 1897, 3346, 4325, 1657, 1, 2199, 1411, 874, 4323, 2759, 2457, 4071, 1236,
2156, 1610, 4538, 2161, 4632
Part a (2 pts): Given the above set of numbers, choose an array size for a hash array (aka table) (Note:
empirically speaking, we find we get the best results when a hash array is approximately half full).
Part b (10 pts): Think of two different possible hashing functions you could use with this set of numbers
to hash the numbers into indices in the hash array (Note: for this particular assignment, I ask that you
not use google, or, at least, don’t use hashing functions you find when you google. Please come up with
2 different possible hashing functions on your own, Extra points will be given to the most creative yet
easy to calculate functions).
Part c (12 pts): Show a hash array for three hashing functions: your two hashing functions and the
hashing function given in class as such:
Let c =(sqrt(5) – 1)/2
h(k) = floor(m * ((k*c) – floor(k*c)))
Where k is the key, and m is 7
You will have to mod the different functions by the length you chose for your array in part A
Collisions will be handled as follows: the numbers will be hashed and inserted into the array in the order
specified above. If a number hashes to an index that already contains a value, the new key overwrites
the value.
Keep track of the number of collisions for each of the three functions. Which hashing function had the
fewest collisions? Which had the most?
Part d (5 pts): If you haven’t already, implement at least one of your hashing functions as code,
including a function that prints out the hash array after all keys have been inserted.
Problem 2:
560, 671, 353, 502, 490,757, 38, 410,904, 93,818,577, 3,671
Part a (4 pts): Given the above sequence of numbers, and a hash array size of 29, assume the hash
function is (ready for this?) (num%arraysize). Show the array after inserting all the numbers using linear
probing. How many collisions (including probing collisions)
Part b (4 pts): Given the same sequence of numbers and the same hash function and arraysize, show the
array after inserting all the numbers using quadratic probing (using the method shown in class)
How many collisions (including probing collisions)
Part c (6 pts): Same sequence, same hash function, same array size. Now use double hashing with the
secondary hashing function being the one we used in class, e.g.,
P(k,i) = h(k) + i*h2(k)
Where i is the number of probes, h(k) is the original hashing function, and h2(k) is:
1 + (k mod (m))
For this problem, we’ll make m be the prime number 7.
Show the array after double-hashing. How many collisions?
Part d (5 pts): If you haven’t already, code part C.
Download