Homework Assignment 3 Spring 2024
Homework Assignment 3 Spring 2024
1. Suppose you are given the following keys: 112, 2542, 9992, 5502 and the following hash function
h(x) = x mod 10.
Hash the keys using the hash function. How many keys collide?
Choose a random1 hash function, ha,b from H10007,10 . Include your random choice of ha,b with
your answer to this problem. Before you rehash the numbers with the new hash function,
determine the probability that the keys 112 and 2542 collide when hashed with ha,b ? Hash
each of the keys 112, 2542, 9992, 5502 with ha,b . How many keys collided?
If you had 1000 keys (all keys were positive integers less than 10000) inserted into a hash
table of size 2000 using a hash function, ha,b , randomly chosen from H10007,2000 (the family
of universal hash functions we defined in class). What is the expected number of collisions
you would have if you inserted a new key, x, into the hash table?
2. Using the technique for perfect hashing that we discussed in class, store the set of keys:
For your first hash function (”outer hash” function) use ha,b (k) = ((ak + b) mod p) mod m,
where a = 3, b = 42, p = 101, m = 9.
3. What is a bound
Pm−1on the2 probability that you use at most 16n space for the secondary hash
tables? (i.e. i=0 (nj ) ≤ 16n.)
What is a bound on the probability (in terms of k) that you use at most kn space (for some
constant k)?
Use the technique we discussed in class.
4. (10 points) How many times must you attempt to construct one of a perfect hash table’s
sub-tables so you have one with probability greater than 99.99999%? This is a failure rate of
1 out of one ten million.
∗
Many of these questions came from outside sources.
1
Use a random number generator.
1
5. (15 points) Your Aunt Mary wants to know who collected the most AuntPoints during the
party, they come from a variety of activities: shuffle board, bingo, croquet, polo, rugby, roller
derby.
She has an array A which contains the name and their points earned during the evening in
various activities. It is of length O(n). If there are n people in the party, how can you quickly
determine who won the most points? (If there is a tie, return all the winners.) Everyone is
impatient to know the answer, since no one gets to eat cake till the winner is announced.
Write the pseudocode to find the winner(s) in O(n) time (average case). Justify your running
time.
Justify that your algorithm runs in O(n) average case time.
State the worst case running time is of your algorithm in big-Oh notation. Justify your
reasoning.
6. News alert!
Great hoards of space rats2 have caused food shortages in the the eastern galaxy. As part of
the relief effort, the federation is sending cheese to all the colonies in the eastern galaxy.
The huge battleship INS Engorged Larder has been requisitioned to distribute the food. The
cheese has been put into missile like containers, and the battleship will shoot it down to a
hungry colony. One cheese “missile” per colony.
Unfortunately the warehouse was attacked by the space rats, who before they were stopped,
used a laser to cut the food containers into two pieces; those pieces now lay in a jumbled pile.
Using duck tape3 , the containers can be put back together. Unfortunately you don’t know
which two pieces belong together.
You are given the weight of every broken piece of container in an array w of length n. Your
job is to find which two pieces belong together. Two pieces go together if they weigh w.4 .
Find an O(n) average case running time algorithm to determine which pieces belong together
and save the Chancellors vacation - oops, I mean save the people from starvation! Justify
your run time.
7. Unfortunately, the ungrateful colonies didn’t respond well to their new cheese diets5 . Piracy
is on an all time high. New space recruits have been gathered from all over the galaxy to
combat the pirates. The recruits are ranked from 1 to k.
In O(n + k) worst case time, develop an algorithm to sort the n fresh faced recruits based on
their ranking where those ranked 1 occur before those ranked 2, etc. Justify your run time.
8. Political unrest has developed again. Some colonies say they are providing more recruits than
others. Rumors are flying that The Spiral Colonies provide the most recruits. Others say
The Galactic Core provide the most. To settle the rumors before the big academy party, you
want to set the story straight.
Given a list of recruits and which colony they came from, find the colonies that provided the
most recruits. You should find the list of colonies sorted by which colony provided the most
2
The effects of genetic engineering at the beginning of the 20th century.
3
Space vessels and missile are routinely patched up with duct tape to save money.
4
This is really large number, much larger than the number of colonies needing food.
5
Who knew that piracy could occur from the peace loving Tortuga.
2
recruits in decreasing order. (i.e If c = 10, you find the colony providing the most recruits
listed first, then colony providing the second most recruits listed second, etc. The last item
in the list returned is the colony which provided the 10th most recruits.)
You must find this list quickly or there will be no food at the party since everyone will be
arguing instead of preparing. Your algorithm must run in O(n + c log c) average case running
time where n is the number of recruits and c is the number of colonies. Justify your running
time.
9. You have decided to get away from it all and live on a lighthouse on an island. No internet,
no cellular phone, no communication with the outside world except for a telephone. The
problem is that the telephone on the lighthouse keeps going out (the sharks keep biting the
wires). Every month, a supply ship will drop off everything you need if you can find a way
to let the people on shore know what you need.
Since the telephone is unreliable, and you would starve if you didn’t alert the shop keeper on
the mainland what you needed, you have to devise your own solution. Luckily, you live in a
lighthouse and you naturally have lots of lights. You devise a system using n lights on a wire
to act as a hash table, T . Alas the problems... this ”hash table”, T , can only indicate 0 or 1
for every index i (i.e. T [i] = 0 if the ith light is off, T [i] = 1 if the ith light is on). You and
the local shop keeper on the mainland agree upon a hash function h that both of you will
use.
Here is how you use your hash function to let the shop keeper know what you want. Everytime
you realize you need something, you request(k) by turning on the h(k)th light. You can do
this in constant time (since the island that the lighthouse is on is very small).
On the mainland, just before he leaves to drop off food and other supplies. The local
shop keeper goes through his supplies and checks to see if you need an item he has. He
asked for(k) which checks if item k was requested by you. This clearly takes constant time
since all he has to do is take out his binoculars and look at the island to see if the h(k)th
light is on. Explain your answer.
• (15 points) What is the probability the shop keeper sends you something you didn’t
want? i.e. You didn’t compute h(k) and then turn on the h(k)th light. Instead, you
turned on a light because you wanted k 0 and it just so happens that h(k 0 ) = h(k).
• (15 points) If you and the shop keeper decided upon two hash functions, h and h0 , could
you decrease the probability you were sent things you didn’t ask for if you used both hash
functions? (You do not add any extra lights - all you have are the n light you started with
on a wire.) For your new system, what is the probability that asked for(k) returns
the wrong answer? Explain your answer.
For this problem, assume that both hash functions will hash each item equally likely to any
number in {0, . . . , n − 1}.
10. (3 bonus points) Think of a good 6 exam question for the material covered in Lecture 3.
6
Only well thought out questions will receive the bonus points.