Introduction to algorithms Chapter 1 hash

Source: Internet
Author: User

The hash table mainly supports insert, search, and delete operations on dynamic sets. The time for searching elements is O (n) in the worst case, but in actual conditions, the expected time for searching in the hash is O (1), and the hash is the promotion of ordinary arrays. Because the elements can be directly located by using the key words of the elements, the hash can be found in O (1) any element that accesses the array within the specified time.


1. Direct addressing table

When the domain name of a keyword is small, that is, the number of all possible keywords is relatively small, you can create an array to reserve a space for all possible keywords, in this way, you can quickly find the corresponding element based on the keyword. This is the direct addressing table. The time for searching, inserting, and deleting the direct addressing table is O (1. ,


2. Hash

It is unrealistic to create an array that reserves space for all possible keywords when the keyword domain is large and there are many keywords, in particular, a large amount of space is wasted when the number of keywords to be saved is less than that of the whole world. In this case, the hash table is suitable for use. The storage space required by the hash table is much smaller than that of the direct addressing table. Although there is less space, the hash can ensure that the element search time is O (1) on average ).


In the direct addressing table, each keyword is the serial number of the slot in the array, which corresponds one to one directly. However, in the hash list, the most important thing is the hash function, because in the hash list, the tablespace contains only the number of elements actually saved. In this way, the hash function calculates the location where each element should be stored in the hash table based on its keywords, which leads to a problem, the values calculated by two different keywords using the hash function may be the same, and two elements cannot be stored in one location. In this case, this collision problem must be solved.


There are two ways to solve the collision problem: one is to design a good hash function, so that as few keywords as possible to generate a collision. The other is that the actual tablespace contains fewer keywords than any other keywords, or even fewer keywords than the actual table space. In this case, collision is inevitable, we must design a method to handle the collision: The Link Method and the open addressing method.


2.1 Hash Functions

A good hash function must have good randomness, so that every keyword can be hashed to any location of M (hash table size) Table slots, it is irrelevant to the slot to which other keywords have been hashed. Therefore, the hash function needs to export the hash value in any mode that may exist in the data. The following lists some common hash methods.

Because hash columns usually put keywords into Hash Functions for calculation to obtain hash values, they are usually converted to natural numbers. If the keyword is a string, the string can be expressed as an integer according to the appropriate base mark.


Division hash

Hash function: H (K) = K mode M. M is a prime number that is not very close to the integer power of 2. It is not recommended to use the power of 2.


Multiplication hash

Hash function: H (K) = M * (K * a mod 1), where 0 <A <1, m multiplied by the fractional part of Ka to determine the position, a's golden ratio is an ideal value.


Global hash

A hash function is a random selection of a hash function from a specific hash function set. In this way, the results of each hash function for the same keyword are different, this ensures randomness, which means that the average performance of the hash is better.


2.2 Link Method

When a conflict or collision occurs when a hash is used, all elements in the hash to the same slot can be put into a linked list. This slot stores the head pointer of the linked list. When the number of slots in the hash list is proportional to the number of elements, the average length of the linked list in each slot is O (1). Therefore, under average conditions, the time complexity of searching, inserting, and deleting a hash is O (1 ).


2.3 open addressing

Unlike the link method, the open addressing method does not apply to linked lists. Instead, all elements are stored in the slot of the scattered list, which saves the storage space of linked list pointers, save the elements in these spaces. In the open addressing method, if the key word of an element already has an element, it indicates that there is a collision. At this time, the hash function will continue to hash the element to the next position, if the conflict persists, continue the exploration, and so on until an empty slot is found to save the elements. Therefore, for each inserted element, the hash function generates a exploration sequence, searches for empty slots in each location in sequence. A maximum of M (number of slots) can be explored. So the hash function here is different from the hash function in the link method, but only generates a specific hash value. The hash function in the open addressing method generates a probe sequence. Common methods include linear probe, secondary probe, and dual probe.


Linear Exploration

Hash function: H (K, I) = (H '(k) + I) mod m, I = 0, 1, 2 ,..., m-1, where H '(k) is the auxiliary hash function.

During the linear probe process, the offset of each probe is 1. Once H' (k) is determined, the entire probe sequence is determined, so a total of probe sequences in M can be generated. This method is easy to generateOne clusterBecause the exploration sequence offset is 1 continuous space, the more slots occupied, the higher the chance of collision, and the query time.


Secondary Exploration

Hash function: H (K, I) = (H '(k) + C1 * I ^ 2 + C2 * I) mod m, where H' is an auxiliary hash function, c1 and c2 are auxiliary constants.

In a secondary probe, the offset of the probe sequence is not 1, but constantly changing (a definite value sequence). But like in a linear probe, As long as H' (k) is determined, the entire probe sequence is determined, so there are only M probe sequences in total.


Double probing

Hash function: H (K, I) = (H1 (k) + I * H2 (k) mod m. H1 and H2 are auxiliary hash functions.

In the dual-probe, the offset of the probe sequence is constantly changing, and the offset change sequence corresponding to each k value is different, the team uses the same H1 (k) value, the offset H2 (k) may correspond to M types, so a total of m ^ 2 probe sequences can be generated. Therefore, it is one of the best methods in open addressing. However, in order to search for the entire hash, the value H2 (k) must be in the same quality as the table's size m; otherwise, the value H2 and M have a common number, the length of the probe sequence is smaller than M.


On average, the number of probes for each element is O (1 ). Therefore, the overall performance is good.


2.4. Full hash

Hash technology is widely used for its outstanding expected performance. When the key set is static, that is, the key set in the key table will not change, you can use full hash. In the worst case, the time complexity of searching can reach O (1). Full hash is actually two-level hash, the first level is to create a hash list and hash n keywords into M slots. The second level hash is not applicable to the Link Method and the open addressing method when a collision occurs, instead, a hash is created for each corresponding slot, and conflicting elements are hashed to the same table without collision. To ensure this, the size of the level-2 hash table should be the square of the number of elements hashed to the slot. It seems that the demand is very large. In fact, the overall expectation space is still O (n ).

Introduction to algorithms Chapter 1 hash

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.