Suppose you need to devise a hash function for a program where the key is a list
ID: 3713085 • Letter: S
Question
Suppose you need to devise a hash function for a program where the key is a list of integers (n1, n2, n3, ...) Indicate whether each of the following hash functions is good or bad, and why, and which is the best. (2 marks)- H1 returns a random integer value - H2 returns the first element n1 - H3 returns the smallest nk value - H4 returns the product of all nk Suppose you need to devise a hash function for a program where the key is a list of integers (n1, n2, n3, ...) Indicate whether each of the following hash functions is good or bad, and why, and which is the best. (2 marks)
- H1 returns a random integer value - H2 returns the first element n1 - H3 returns the smallest nk value - H4 returns the product of all nk Suppose you need to devise a hash function for a program where the key is a list of integers (n1, n2, n3, ...) Indicate whether each of the following hash functions is good or bad, and why, and which is the best. (2 marks)
- H1 returns a random integer value - H2 returns the first element n1 - H3 returns the smallest nk value - H4 returns the product of all nk
Explanation / Answer
- H1 returns a random integer value
- H2 returns the first element n1
- H3 returns the smallest nk value
- H4 returns the product of all nk
There are some characteristics of good hash function
1. Hash function should be fully determined by the data being hashed.If something else is used, it will alllow for worse distribution of the hash values.
2. Hash function should use all the input data. If it doesn't then slight variation in input data would cause an inappropriate number of similar hash values resulting in too many collisions.
3. Uniformly distribute data across entire set if possible hash values.
4. Generate diferent hash values for similar input
H1 does not satisfy the caharacteristics 1 and 2. Input data is not being used
H2 will lead into many collisions as there can be many list of integers having first element same by chance.
H3 not satisfying the characteritics 1 and 2 and can lead to colllisions
H4 is good. Satisfies characteritics of good hash functions. Uses all the input data to determine hash value and there is less chance of collision
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.