Why is the hash function H (k) = k% m medium m try not to be a power of 2 or if 2^i-1

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.