Leetcode Lect7 hash table

 

 


The traditional hash table

For a hash table of length n, which are stored as follows: 
    it is calculated according to the hash value h Key = the hash (Key) 
    The number n is assumed case, it should be placed in the first key-value pair (h % n) in a box 
    if the box has been key-value pairs, use the zipper open addressing method or methods to resolve conflict

 

 


Hash collision

If the string is different hash to the same location, called a hash collision. Common solutions to hash collisions are the following:

Zipper Act (open hash)

When using the fastener to resolve hash collision method, for each array location, corresponding to a list of elements is placed, all belonging to the same box of keys are arranged in a list. When there is a conflict, we will insert this element to the tail of the list, in order to avoid conflicts.

 

 

Linear detection method (closed hash)

One kind is an open detection method of linear addressing method.

When conflict occurs, the next we check for conflicts hash address (array index plus one) to determine whether the insertion, if not then continue at a check position.

Hash table method implemented fastener, because of a linked list, can be elastically received key-value pairs, and for the hash table to realize linear detection method, the number of key-value pairs that receive direct the limited size of the array. It is necessary to adjust the size of the array before the array is full . In general, whenever the total number of key-value pairs to reach half of the array, we will double the size of the entire array.

Closed hash is not much use, because there has been a downward insertion can lead to an increasing number of collision

 

Re-hash

This method is constructed simultaneously a plurality of different hash functions:

Hi=RH1(key)        i=1,2,…,k

When the hash address Hi = RH1 (key) conflict, then calculating Hi = RH2 (key) ......, until the conflict is not generated. This method is less likely to occur aggregation, but increase computation time.

 


Distributed Hash Table

Referring DHT, a hash table will be divided on a different machine by hash values.

 

 


Consistent hashing

In general distributed hash table, if there is a node dynamically added or deleted, it will lead to large amounts of data failure. So how to improve this situation?

consistent hashing algorithm is a hash, simply put, when you remove / add a cache, it can be as small as possible has been key to change the mapping relationship exists. The idea is that the machine and the data hash into the same space.

For example, in Chord algorithm where each node is responsible for it in a clockwise direction to hash the data points in the region before the next node. If you have a dynamic node add / delete, then only the endpoint of this interval two machines will be affected, while other machines are not. Within this range

 

There is also a DHT algorithm, called Kademlia, it is the P2P download basis. Can refer https://colobu.com/2018/03/26/distributed-hash-table/

 

 


Ref:https://xiekeyi98.com/819591f7.html

 

Guess you like

Origin www.cnblogs.com/pdev/p/11332264.html