Question: A collision occurs when you have two values that hash to the same location. There are several methods for dealing with collisions. Here are the
A collision occurs when you have two values that hash to the same location. There are several methods for dealing with collisions. Here are the two most common:
Open Addressing Also called closed hashing
Chaining also called open hashing
Fortunately for you and this exercise you are not going to have to worry about implementing either of these.
To reduce the number of collisions several hash function have been introduced:
Knuth Hash Function
The Knuth hash function flows something like this:
Initialize the return value to the size of the string
For each character within the string
Rotate the return value left by five bits
XOR the return value with the character being examined
Modulus the return value by the number of elements in the array hash table
Bernstein Hash Function
DJ Bernstein is a research professor at the University of Illinois at Chicago. His hash function is as follows:
Initialize the return value to
For each character in the string
Multiply the return value by
Add the character
Modulus the return value by the number of spaces in the hash table
You may wonder why is used to initialize the return value. You will find these kinds of "magic numbers" in most hash algorithms. The reason is that through testing these numbers were found to have far less collisions.
Here is an example of the Bernstein hash function:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
