Вы находитесь на странице: 1из 2

Lecture 6 Practical

Hash Tables
Exercise 1:
Do the foowng.
1. Create an array A of sze n (n w be defned ater) and f t up wth random ntegers.
2. Bud an AVL tree T from ths array by nsertng the eements of the array A n the tree
accordng to the order n whch they appear n A .e., the eement at ndex +1 n A s
nserted mmedatey n T after the eement at ndex n A.
3. Bud a hash table H of sze m where the ntegers (keys) n the nta array are stored
accordng to some hash functon h.
For exampe:
1. A = l505, 404, 303, 202, 101|, n = 5
2. T s the AVL tree that contans 404 as root wth eft chd 202, and rght chd 505; and 202
has eft chd 101 and rght chd 303.
3. H = l505, 101, 202, 303, 404|, m = 5, h(k) = (k mod m).
Now:
1. Set n = 10000 and m = 3000.
2. Use the hash functon h(k) = (k mod m).
3. F n H usng a) open hashng (ca t H1), b) usng cosed hashng wth near probng (ca t
H2).
4. Choose a key k (at a random poston) from array A to search for.
5. Search for key k n T and compute the tme needed for the search.
6. Search for key k n H1 and compute the tme needed for the search.
7. Search for key k n H2 and compute the tme needed for the search.
8. Perform the steps 4, 5, 6, and 7 nsde a oop that terates 1000 tmes. In other words,
repeat for 1000 tmes the process of choosng a random key, and searchng for t n T, H1,
and H2; and recordng the tme. You need to sum up the 1000 executon tmes n each of
the three cases. At the end you shoud get tT the tota executon tme of 1000 searches n
T, tH1 the tota executon tme for 1000 searches n H1, and tH2 the tota executon tme
for 1000 searches n H2. If you get tT tH1 tH2, try performng k>1 searches n every oop
teraton unt you start notcng a dfference between tT, tH1, and tH2.
9. Dvde each the tmng measurements you obtan n step 8 by 1000 to get the average
executon tme per one search (or dvde per 1000k f you performed k searches per oop
teraton). Compare the executon tmes you get. Do they conform to what we saw n
ecture?
Exercise 2 (not compulsory):
Do not underestmate the tme t takes to perform ths exercse.
1. Repeat the experments above (Exercse 1) but by settng n = 10000 and m = 1000, and
usng the foowng.
A hash tabe H1 wth open hashng.
A hash tabe H2 wth cosed hashng mpemented usng near probng.
A hash tabe H3 wth cosed hashng mpemented usng quadratc probng (pck a vaue
for constant c yoursef, or research a good vaue for c, or use any quadratc hash
functon that s shown to be good e.g., by researchng the textbooks, Internet, etc.).
A hash tabe H4 wth cosed hashng mpemented usng doube-hash probng (search
the Internet/books for sutabe ones, expan why they are sutabe).
2. What are your observatons?
3. Your mpementaton of H2, H3, and H4 can be mproved by usng rehashng .e., reszng
your hash tabe and updatng the defnton of your hash functon.
For each of the three hash tabes above, modfy your code to mpement rehashng.
Perform the testng agan (.e., tmng anayss of search operatons).
Compare the new resuts wth your prevous mpementaton that does not use
rehashng.
Comment on your fndngs. How expensve was rehashng?
If you successfuy do exercse 2, send me your work.

Вам также может понравиться