Question

In: Computer Science

Assume that you are hashing key K to a hash table of n slots (indexed from...

Assume that you are hashing key K to a hash table of n slots (indexed from 0 to n - 1). For each of the following functions h(K), answer the following question(s):

1) Is the function acceptable as a hash function (i.e., would the has program work correctly for both insertions and searches),

2) and if so, is it a good hash function?

Function rand(n) returns a random integer between 0 and n - 1. Also assume k is a an integer values that is >> (potentially much larger than) n.

(a) h(k) = k / n where k and n are integers, k >> n

(b) h(k) = 1

(c) h(k) = (k + rand(n)) mod n

(d) h(k) = k mod n where n is a prime number and k is an integer k >> n

Solutions

Expert Solution

So First we will talk about first function:

1) H(k)=k/n This function can be used as a Hash function But there will be some collisions as for a group of n numbers key will be same. So we have to use Linear Searching in that case if collision occurs. Eg. if n=10 then 0-9,will be in key 0, 10-19 will be in key1 and so on. So for the given case as k>>n it is a good hashing function because for searching O(n) complexity will be required and as n is small less time will be taken.

2) H(k)=1 It is also acceptable but is a very bad hashing function as all the elements will be present under the same key and searching requires K searches which is O(K) time complexity which is very high and It is equivalent to Linear Searching. Collisions occurs at a high rate.

3)H(k) = (k + rand(n)) % n : It is not acceptable as a hashing function as Key cant be calculated Or more clearly if we calculate the key using this function many times different key will be generated and hence searching becomes impossible in this case.

4)H(k) = k mod n where n is a prime number and k is an integer k >> n : It is acceptable as a hashing function and is a best one among all the above functions as It reduces the number of collisions in the hashing table and is very fast in searching the element in the hashing table.

If it was any help to you then Please UPVOTE my answer

And Feel free to ask any doubts in the comment section..


Related Solutions

Use double hashing and the following hash functions: hash(key) = (key % table size) and hash2(key)...
Use double hashing and the following hash functions: hash(key) = (key % table size) and hash2(key) = (16 - (key % 10)) + 1 to insert the following numbers into a dictionary's hash table: 1, 12, 14, 3, 5 and 17. The table size is 11. Show the table and where each value is inserted. No coding required for this problem.
Develop an algorithm to demonstrate hashing using hash table with modulo as the hash function. Assume...
Develop an algorithm to demonstrate hashing using hash table with modulo as the hash function. Assume the size of the hash table as 10. To avoid collisions in case of identical keys for two different elements, use Linear Probing collision resolution technique. using c++ add comment on the code
1.Consider hashing with a table size of 100 and a hash function of key%tablesize. Insert 25...
1.Consider hashing with a table size of 100 and a hash function of key%tablesize. Insert 25 keys. Do you expect to see any collisions? Why or why not? Yes, because random values likely will land on same locations. No becase there are four times as many slots as needed. 2. Secondary clustering means that elements that hash to the same position will probe to the same alternate cells. Simple hashing uses key%tablesize as the hash function. Which of the following...
3. Assume a hash table with 7 locations and the hashing function h(i) = i%7. Show...
3. Assume a hash table with 7 locations and the hashing function h(i) = i%7. Show the hash table that results when the integers are inserted in the order given. (10 points each. Total 30) • 5, 11, 18, 23, 28, 13, 25, with collisions resolved using linear probing • 5, 11, 18, 23, 28, 13, 25, with collisions resolved using quadratic probing • 5, 11, 18, 23, 28, 13, 25, with collisions resolved using chaining.
A hash table works as follows. We allocate a table of m slots. All the items...
A hash table works as follows. We allocate a table of m slots. All the items we intend to store in the table belong to a large set U of items. We adopt a function f: U ->{0,1...m-1) which maps an item to a slot. Suppose we seek to store n items from U in a table of m slots. (a) Suppose f maps every item in U with equal probability to one of the m slots. What is the...
SHOW WORK Draw the hash table that results using the hash function: h(k)=kmod7 to hash the...
SHOW WORK Draw the hash table that results using the hash function: h(k)=kmod7 to hash the keys 41, 16, 40, 47, 10, 55. Assuming collisions are handled by Double hashing. SHOW WORK
3. Draw the hash table that results using the hash function: h(k)=kmod 7 to hash the...
3. Draw the hash table that results using the hash function: h(k)=kmod 7 to hash the keys 50, 700, 76, 85, 92, 73, 101. Assuming collisions are handled by Quadratic probing. Don't write a program. Just please manually solve the problem. Thanks.
Consider a hash table T with m=10 slots that uses open addressing with linear probing and...
Consider a hash table T with m=10 slots that uses open addressing with linear probing and the hash function h(k,i) = ((k mod m) + i) mod m, where i = 0,1,..., m-1 is the probe number. Show T after inserting keys <867,755,535,296,866,135,669,445,524>.
Data Structures: Hashing Exercise: 1. a) Given an empty “separate chaining hash table” of size 10,...
Data Structures: Hashing Exercise: 1. a) Given an empty “separate chaining hash table” of size 10, with hash function                        Location = Number modulus 10 Show the hash table after inserting the following numbers: 21, 18, 27 , 31 , 48, 51 , 37, 98, 17 b) What table size would be better? 11? 12? 20? 2. We want to hash name strings into a chaining hash table of size 10, how would you divide the alphabet into 10 groups?...
For the division method for creating hash functions, map a key k into one of m...
For the division method for creating hash functions, map a key k into one of m slots by taking the remainder of k divided by m. The hash function is:        h(k) = k mod m,     where m should not be a power of 2. For the Multiplication method for creating hash functions,     The hash function is h(k) = └ m(kA –└ k A ┘) ┘ = └ m(k A mod 1) ┘    where “k A...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT