Calculation of finding success and finding unsuccessful average lookup lengths in the case of hash tables and other probabilities

Source: Internet
Author: User

Recently reviewed the next data structure in the hash table, found in the calculation of the probability of finding an unsuccessful average search length is confused, do not know how to calculate it. Now that we know how to calculate the data through the access information, we record it for future reference.

The following is the 2010 2010 National Postgraduate Entrance examination of the unification of computer science and technology subjects in the basic comprehensive examination of computers subject to a test hash table problem.

Question1:

The keyword sequence (7, 8, 30, 11, 18, 9, 14) is hashed to the hash table. The hash table's storage space is a one-dimensional array that starts with a subscript of 0, and the hash function is: H (key) = (KEYX3) MOD 7, which uses a linear detection and re-hashing method, which requires a loading (load) factor of 0.7.

(1) Please draw a hash list that is constructed.

(2) Calculate the average lookup length of the finding success and finding unsuccessful in the equal probability case respectively.

Ans:

(1). First, define a concept loading factor, which is the degree of saturation after all key sub-populated hash tables, which equals the total number of keywords/hash table length. According to test instructions, we can determine the length of the hash table as L = 7/0.7 = 10; So the hash table that needs to be built is a one-dimensional array labeled 0~9. The following hash function values table can be obtained according to the hash function.

H (key) = (KEYX3) MOD 7, such as Key=7, h (7) = (7x3)%7 = 21%7=0, other keywords are the same.

Key 7 8 30 11 18 9 14
H (Key) 0 3 6 5 5 6 0

(table 1)

The linear detection and hashing method is used to deal with the conflict, and the hash table is constructed as follows:

Address 0 1 2 3 4 5 6 7 8 9
Key words 7 14
8
11 30 18 9

(Table 2)

The following is a description of how the hash list is constructed, note that the keywords 7 and 14,30 and 9, 11, and 18 in table 1 have the same H (key) values for the three groups of keys, which creates a conflict when the hash table is built, because their addresses are the same, so a certain conflict-handling approach is needed to resolve the problem. According to the problem, the method of linear detection and re-hashing is used to deal with the conflict. Here's a detailed description of how to build a hash table:

The first key 7, its address is 0, so put in the hash list of the table below the position of 0, there is no keyword in this position, so there is no conflict can be directly filled in;

The second key 8, its address is 3, so put in the hash list of the table below the position of 3, there is no keyword in this position, so there is no conflict can be directly filled in;

The third key 30, its address is 6, so put in the hash list of the table below the position of 6, there is no keyword in this position, so there is no conflict can be directly filled in;

The fourth key 11, its address is 5, so put in the hash list of the table below the position of 5, there is no keyword in this position, so there is no conflict can be directly filled in;

The fifth key 18, its address is 5, so put in the hash list of the array below the table is 5 position, but this position already has the keyword 11, encountered a conflict, at this time we are based on the linear detection re-hash method to deal with this conflict, the next position 6, 6 This position already exists keyword 30 continues to increase step 1, So now the new address should be 7, position 7 on no keyword, put it, to this conflict has been resolved;

Sixth key 9, its address is 6, so put into the array of the table below the list of 6 position, but this position already has the keyword 30, encountered a conflict, probing the next position 7, 7 This position already exists keyword 18 continues to increase the step 1, so now the new address should be 8, no keyword on position 8, can be put in;

Seventh key 14, its address is 0, so put into the array of the table below the list of 0 position, but this position already has the keyword 7, encountered a conflict, probing the next position 1, position 1 No keywords, put it;

All the keywords are filled in this step, and the hash list is constructed, as shown in table 2.

(2) Equal probability of finding the average length of the search success:

This question can be solved according to the construction process of the first question:

Key7 is filled in the table once, so the number of lookups is 1, the same as 8, 30, 11 lookups are 1, KEY18 carried out 3 times, the detection location is 5,6,7, so the number of lookups is 3;key9 3 times, key14 two probes, so the number of lookups is 2. The number of times table is shown in table 3

Key 7 8 30 11 18 9 14
Count 1 1 1 1 3 3 2

(Table 3)

So aslsuccess= (1+1+1+1+3+3+2)/7 = 12/7.

Find the average lookup length that is unsuccessful in the case of equal probabilities:

Next discuss the unsuccessful situation, look at table 2, calculate the number of unsuccessful lookups to find the keyword directly to the first address is a null key to the distance, but according to the hash function address is MOD7, so the initial only possible in the 0~6 location. In the case of equal probability, find 0~6 location lookup failed to find the number of times:

Looking at address 0, the distance to address 2 where the first keyword is empty is 3, so the number of unsuccessful lookups is 3.

Address 1, the distance to the first key-empty address 2 is 2, so the number of unsuccessful lookups is 2.

Address 2, the distance to the first key-empty address 2 is 1, so the number of unsuccessful lookups is 1.

Address 3, the distance to the first key-empty address 4 is 2, so the number of unsuccessful lookups is 2.

Address 4, the distance to the first key-empty address 4 is 1, so the number of unsuccessful lookups is 1.

Address 5, to the first key is empty address 2 (note not address 9, because the initial may only be between 0~6, so the loop goes back) is 5, so the number of unsuccessful lookups is 5.

Address 6, to the first key is empty address 2 (note not address 9, because the initial may only be between 0~6, so the loop goes back) is 4, so the number of unsuccessful lookups is 4.

So the number of unsuccessful lookups is shown in the table below

Key 7 8 30 11 18 9 14
Count 3 2 1 2 1 5 4

(Table 4)

So aslunsuccess= (3+2+1+2+1+5+4)/7 = 18/7.


Calculation of finding success and finding unsuccessful average lookup lengths in the case of hash tables and other probabilities

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.