Why hash function H (k) = k% m medium m try not to be a power of 2
The screenshot below is from Chapter 11 of the clrs discussion on the hash function
I've been confused before, why
When using the division method, we usually avoid certain values of M. For example, M should not being a power of 2, since ifm = 2^p, then H (k) is just the p lowest-order bits of K.unless we know, all low-order p-bit patterns is equally likely, we are better off designing the hash func tion to depend on all the bits of the key.
-----------------------------------------------------------------------------------------------------------
Why try to avoid k%m m not a power of 2?
If 2 i power 2^i = 10 ... .... 0 starting from the first 0 of the 1 back to the end, there were altogether I 0
If you use K%m to take the remainder operation, m = 2^i, the result is to truncate the K to keep the low I bit. It's a "bad feature" for hash.
The root of the problem is that the hash must have a good character, that is, to avoid collisions, to avoid collisions will be evenly distributed insertion
Direct truncation is a rude way to ensure that the inserted data can be distributed evenly across the hash table.
-----------------------------------------------------------------------------------------------------------
is to avoid K%m in M not 2 power-1 (2^i-1)?
for different strings S1 = "abcd" S2 = "adcB"
Their hash value is the same! But they are different strings! They'll clash!
What do we do? Consider the order of individual characters in a string, weighting the individual strings, and weighting them in a bit in the string. For example, string
S1 evaluation can be so ' a ' *2^ (0) + ' B ' * (2^ (1)) + ' C ' * (2^2) + ' d ' * (2^3)
S2 evaluation can be so ' a ' *2^ (0) + ' d ' * (2^ (1)) + ' C ' * (2^2) + ' B ' * (2^3)
The literal value of the two is different. That's it? Can you ensure that there is no conflict? It's not over yet ...
proved to be cool and handsome.
Why is the hash function H (k) = k% m medium m try not to be a power of 2 or if 2^i-1