Introduction to algorithms-Chapter 11th-Hash

Source: Internet
Author: User
I. Concept 1. Summary hash tables only support insert, search, and delete operations. The process of ing the keyword K to the slot H (k) is called a hash. Multiple keywords are mapped to the subscript position of the same array. A good hash function should make every keyword possible to be hashed to M slots. 2. Hash Function

(1) If the function is H (K) = K, directly addressing the table

(2) Division hash: H (K) = K mod m

(3) multiplication hash: H (K) = M * (K * a mod 1) (0 <A <1)

(4) Global hash: select a random hash function from a group of well-designed hash functions. (Even for the same input, each time it is different, the average state is better)

3. Conflict Resolution Policy

(1) Connection Method

(2) Open addressing

A. linear test: H (K, I) = (H '(k) + I) mod m

B. Secondary test: H (K, I) = (H '(k) + C1 * I + C2 * I ^ 2) mod m

C. double hash: H (K, I) = (H1 (k) + I * H2 (k) mod m

(3) Full hash: design a small secondary hash list

 

Ii. function pointers are used in code. for usage of function pointers, refer to function pointer summary.
// Hash. h # include <iostream> using namespace STD; int M, nil = 0; // 11.4 open addressing method typedef int (* probing) (int K, int I ); int H (int K) {return K % m;} int H2 (int K) {return 1 + K % m-1);} // linear int linear_probing (int K, int I) {return (H (k) + I) % m ;}// Secondary test int quadratic_probint (int K, int I) {int C1 = 1, C2 = 3; return (H (k) + C1 * I + C2 * I * I) % m;} // double detection int double_probint (int K, int I) {return (H (k) + I * H2 (k) ) % M;} int hash_insert (int * t, int K, probing p) {int I = 0, J; do {J = P (K, I ); if (T [J] = nil) {T [J] = K; return J;} I ++;} while (I! = M); cout <"error: Hash Table overflow" <Endl;} int hash_search (int * t, int K, probing p) {int I = 0, J; while (1) {J = P (K, I); If (T [J] = nil | I = m) break; if (T [J] = k) return J; I ++ ;}}
Iii. Exercise 11.1 direct addressing table

11.1-1o (M). In the worst case, there is only one minimum keyword in the Set: 11.1-2. If X is inserted, the X position of the vector is 1. If X is deleted, the X position of the vector is 011.1-3. When satellite data with the key keyword exists, t [Key] points to a satellite data with the key keyword. If it does not exist, t [Key] points to null. Each satellite data is represented by a node. The node has three fields: the key word, the satellite data, nextdirect-address-search (T, k) return T [k]; Direct-address-insert (t, x) if t [Key [x] = nullthent [Key [x] <-xelse next [x] <-next [T [Key [x] Next [T [Key [x] <-x

11.1-4 see Introduction to algorithms-11.1-4

A stack is used to store the actually inserted data. When the stack is inserted, a space is extended up. When the stack is deleted, the top element of the stack is used to add the location of the deleted element, and the stack recycles a space down, the method is similar to P127-11.3-4.

This very large array does not directly store data, but stores the location of data in the stack.

For an element P, if H [p] <total number of elements in the stack & P = stack [H [p], it is stored, otherwise it does not exist

 

11.2 hash

11.2-3

The time for all operations is O (n/2), and N is the length of a linked list.

11.2-4 see Introduction to algorithms-11.2-4

During the insert operation, an idle slot is retrieved from the free linked list, and the keyword X is entered to modify the pointer. The corresponding queue of the linked list can be divided into the following situations:

(1) If the slot to which X belongs is not occupied

Step 1: Remove this slot from the free linked list

Step 2: Enter the keyword X.

Step 3: Modify the pointer. In this case, both next and pre are set to-1.

(2) The slot to which X belongs has been occupied. The key to occupying this slot is that Y and Y also belong to this slot.

Step 1: Extract an idle slot from the free linked list. This slot is definitely not the slot to which X belongs.

Step 2: Enter the keyword X.

Step 3: Modify the pointer and add the slot linked list to the "queue with the slot X as the header node"

(3) The slot to which X belongs has been occupied. The key to occupying this slot is Y, and y does not belong to this slot. (2) We can see that this situation is possible.

Step 1: Extract an idle slot from the free linked list. This slot is definitely not the slot of X or the slot of Y, just take it and use it

Step 2: Fill in the keyword Y in the new slot, modify the pointer, And let y use this new slot, and return the original slot to X

Step 3: Enter the keyword X in the slot to which X belongs.

Step 4: Modify the "slot" pointer to which X belongs, similar to (1)-Step 3

During the delete operation, the keyword to be deleted is X, and the slot occupied by X is released, which can be divided into the following situations:

(1) The slot occupied by X is the slot of X, and slot-> next =-1, that is, only X in all keywords belongs to this slot. After X is deleted, slot is idle.

Step 1: Release slot to the free linked list

(2) The slot occupied by X is the slot to which X belongs. However, only X belongs to this slot in other keywords. The Key Slot should be used first, instead, release the "slat that is not your own keyword and temporarily used ."

Step 1: select another slot2 from the queue with the slot as the header node. The slot2 keyword belongs to the slot rather than the slot2, and only the slot2 is used because it is occupied.

Step 2: Fill in slot2 content

Step 3: Modify the pointer so that slot exists in the queue instead of slot2. The difference is slot or queue header.

Step 4: Release slot2 to the free linked list

(3) The slot occupied by X is not the slot to which X belongs. In this case, this slot must not be the queue header, and other keywords exist in the queue, and occupies the slot to which X belongs.

Step 1: remove the slots occupied by X from the "queue with the slot header of X"

Step 2: Release slot to the free linked list

Search operations. If you understand insert and delete operations, the search operations are relatively simple. The keyword to be searched is X, which can be divided into several situations.

(1) The slot to which X belongs is not occupied, that is, there is no keyword with the same slot as X, and of course there is no

(2) The slot to which X belongs is occupied, but its key does not belong to this slot. It is the same as (1) and does not have the keyword of the same slot as X.

(3) The slot to which X belongs is occupied, and the key to its storage belongs to this slot, that is, the keyword with the same slot as x exists. I just don't know if this keyword is X or not, so I need to look for it further.

 

11.3 Hash Functions

11.3-1

Search for a key word K from the H (k) table in the hash table

11.3-2

Each time a character in the string is processed, the modulo operation is performed once.

11.3-4

#include <cmath>int main(){int n;while(cin>>n){double A = (sqrt(5.0)-1) / 2;double t1 = A * n;int t2 = (int)t1;double t3 = t1 - t2;int t4 = t3 * 1000;cout<<t4<<endl;}}

H (61) = 700

H (62) = 318

H (63) = 936

H (64) = 554

H (65) = 172

 

11.4 open addressing

11.4-1

Linear test: 22 88 0 0 4 15 28 17 59 31 10

Secondary test: 22 0 88 17 4 0 28 59 15 31 10

Dual Detection: 22 0 59 17 4 15 28 88 0 31 10

The Code is as follows: function pointers are used in the Code. For usage of function pointers, refer to function pointers.

#include <iostream>#include <string>#include "Hash.h"using namespace std;void Print(int *T){int i;for(i = 0; i < m; i++)cout<<T[i]<<' ';cout<<endl;}int main(){string str;int i, j;m = 11;int T[11], A[9] = {10, 22, 31, 4, 15, 28, 17, 88, 59};Probing P[3] = {Linear_Probing, Quadratic_Probint, Double_Probint};for(i = 0; i < 3; i++){memset(T, 0, sizeof(T));for(j = 0; j < 9; j++)Hash_Insert(T, A[j], P[i]);Print(T);}return 0;}

11.4-2

After deleting an element, set this position to deleted. In the insert operation, change L3 to if T [J] = nil or T [J] = deleted.

#define DELETED -1int Hash_Insert(int *T, int k, Probing p){int i = 0, j;do{j = p(k, i);if(T[j] == NIL || T[j] == DELETED){T[j] = k;return j;}i++;}while(i != m);cout<<"error:hash table overflow"<<endl;}void Hash_Delete(int *T, int k, Probing p){int j = Hash_Search(T, k, p);if(j != NIL)T[j] = DELETED;}

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.