You are on page 1of 2

Institut für Informatik

RHEINISCHE INSTITUT FÜR


Abteilung 1
FRIEDRICH-WILHELMS- INFORMATIK DER Prof. Dr. Petra Mutzel
UNIVERSITÄT BONN UNIVERSITÄT BONN Dr. Daniel Schmidt
M. Sc. Christine Dahn
M. Sc. Lutz Oettershagen

Exercises for
Excercise Sheet 6
Advanced Algorithms Discussion:
WS 20/21 CW 52/CW 01

Exercise 6.1 – Questions (3 Points)

• What is a collision? What are some strategies to deal with them?


• In which case is a perfect hashing useful? Which two methods were introduced to calculate a perfect
hashing? What are the pros and cons about them?
• How does cuckoo hashing operate? What are the pros and cons about it? What problem could occur?

Exercise 6.2 – Hash functions (7 Points)


We have a universe of three letter names U = {A − Z} × {a − z} × {a − z}. They need to be hashed into a
hash table. In order to do so, we generate a key for each name, by adding up the Ascii code for each letter;
e.g. Ada = 65 + 100 + 97 = 262. For the set U1 = {Ada,Cat,Gal,Ian,Kai ,P i a} ⊆ U the keys are mapped
using the following hash function h(x) = (a · x + b) mod p mod m ∈ Hp,m with a = 2, b = 1, p = 283


and m = 9.

name Ada Cat Gal Ian Kai P ia


key 262 280 277 282
h 8 2 3

h 0 1 2 3 4 5 6 7 8
key
name Kai P ia Ada

a) Calculate the missing values. Then add Gal to the hash table.
b) What happens when you try to add Cat and Ian to the table? Use open hashing with linear probing
to first add Cat and then Ian to the array.
c) What happens if we try to add Zoe (with a key of 302) to the table? How do we have to transform
the hash function to ensure it works for all names in U? (With the given key calculation.)
d) How could you alter the calculation of the key to avoid getting the same key for two different names?
(The given calculation gives the same key of 277 for Hal and Kai or 280 for Ian and Ina.) How would
you have to transform the hash function to deal with the new key generation.

Optional: If you would like to fill the whole table you can add these names U2 = {Ada,Cal,Ele,Gal,Ida,Kai ,
Lou,Max,P i a} ⊆ U to a new array using the key generation and hash function above.

Page 1 of 2
Exercise 6.3 – Cuckoo hashing (4 Points)
Given two hash functions h1 ,h2 : {A − F } → {1, . . . ,m}. The values for the first hash function are given by
the following array; e.g. h1 (A) = h1 (D) = 1 and h1 (C) = 4. The values for the second hash function are
indicated by the arrows; e.g. h2 (A) = 4 and h2 (D) = 7.
Apply the insert method of the Cuckoo-Hashing algorithm from class to the elements A,B,C,D,E,F (in this
order). Use an array of size m = 10. Log each step of insertion.

!"#$# %# &# '# (#


) #* #+ #, #- #. #/ #0 #1 #)2#

Exercise 6.4 – Perfect hashing (6 Points)


Use the Perfect Hashing algorithm from class to calculate a 2-stage hash-table (a.k.a. a static perfect
hashing data structure) for the following set of n keys:

4, 7, 13, 18, 12, 42, 128, 10, 11, 1

Use the universal class Hp,m with appropriate values for p and m to pick your hash functions from. Log each
step, even if you had to discard a hash function.

Page 2 of 2

You might also like