Uploaded by Jenn Inn

Exercises-on-hash-tables

advertisement
Some exercises on hash tables
1. Given the values 2341, 4234, 2839, 430, 22, 397, 3920, a hash table of size 7, and hash function h(x) = x
mod 7, show the resulting tables after inserting the values in the above order with each of these collision
strategies:
(a) Chaining
(b) Linear probing
(c) Quadratic probing where c1 = 1 and c2 = 1
(d) Double hashing with second hash function h2 (x) = (2x − 1) mod 7
2. Suppose the hash table is of size m = 9, the division method is used as hashing function h(x) = x mod 9
and chaining handle collisions. The following keys are inserted: 5, 28, 19, 15, 20, 33, 12, 17, 10. In which
entries of the table do collisions occur?
3. Now suppose you use the same hashing function as above with linear probing to handle collisions, and
the same keys as above are inserted. More collisions occur than in the previous question. Where do the
collisions occur and where do the keys end up?
4. Suppose a hash table with m = 31 gets to be over .75 full. You decide to rehash. What will be a good
choice of size for the new table in order to reduce the load factor below .5 and thus avoid collisions?
5. Given a quadratic hash function h(k, i) = (h(k) + c1 i + c2 i2 ) mod m and keys x1 , x2 , x3 , x4 . What would
be the smallest value m such to guaranty that h will eventually probe into an empty entry in the hash
table for each of the above keys? Justify your answer.
6. A hash function consists to take the last digit of a key as the hash value. Linear probing is used to handle
collisions. 11 is the size of the table.
(a) Show the content of the hash table after inserting the following keys in that order: 16, 44, 93, 40, 66,
84, 82, 26, 43, 64, 77
(b) Show what the table looks like after deleting keys 16, 93, and 66.
(c) Show what the table looks like after inserting 72, 41, and 51, in that order.
(d) Consider using a double hashing function h(k, i) = (h1 (k) + ih2 (k)) mod m where h1 (k) = k mod 11
and h2 (k) = (k mod 9) + 1 and h(k, i) = (k mod 11 + i(k mod 9 + 1)) mod 11. Can you insert all
the keys 16, 44, 93, 40, 66, 84, 82, 26, 43, 64, 77 in that order in the table? If yes, can you explain
why h2 manages to probe all the entries in the table?
(e) A way to measure the performance of these hashing methods to run operations search(), insert(),
delete() consists to calculate the average length of the probing sequences. The length of the probing
sequences adds the value of the probe number (i) each time a collision is resolved. The average
lenght divides this number by m, the size of the page table. What is the average length of the
probing sequences for the above two hash functions?
7. You have quadratic hash function h(k, i) = (h(k) + 21 i + 12 i2 ) mod m. Assume m = 11, what keys will
index in entries 1, 3, 4, 8 of the hash tables?
8. You have quadratic hash function h(k, i) = (h(k) + 21 i + 12 i2 ) mod 7.
(a) What is the value of i when the function hash − insert(T, x) succeed or fail to insert each of the
following keys; 14,15,35,1,5,3
(b) What is the value of i when the function hash − search(T, 3) is performed?
(c) What is the value of i when the function hash − search(T, 7) is performed?
(d) What is the value of i when the function hash − delete(T, 15) is performed?
(e) What is the value of i when the function hash − search(T, 35) is performed?
9. The following sequence of events happen in hash table T . A key k1 is stored in entry x of T . A key k2
collides with k1 in T . Key k1 is deleted from T . A key k3 is stored in entry x. A search is performed on
key k2 . Will the key k2 be found? Explain.
1
√
10. Let m = 500 be the size of a hash table T and h1 (k) = ⌊m(ak − ⌊ak⌋)⌋ where a =
5−1
2 .
(a) Hash the keys 31, 32, 33, 34, 35, and compare the hash values with those of h2 (k) = key mod 500,
what do you see?
(b) Passwords can be hashed before been stored on a server such that if the server is cracked, the hackers
won’t be able to see your passwords. None of h1 or h2 is used in practice to hash passwords. However,
if your only choices was one among these two, which properties will lead you to choose one over the
other for this particular application?
2
Download