2-choice hashing

This article possibly contains original research. Please improve it by verifying the claims made and adding inline citations. Statements consisting only of original research should be removed. (May 2013) (Learn how and when to remove this message)

|

2-choice hashing, also known as 2-choice chaining, is "a variant of a hash table in which keys are added by hashing with two hash functions. The key is put in the array position with the fewer (colliding) keys. Some collision resolution scheme is needed, unless keys are kept in buckets. The average-case cost of a successful search is  O ( 2 + ( m − 1 ) / n ) {\displaystyle O(2+(m-1)/n)} , where  m {\displaystyle m} is the number of keys and  n {\displaystyle n} is the size of the array. The most collisions is  log 2 ⁡ ln ⁡ n + θ ( m / n ) {\displaystyle \log _{2}\ln n+\theta (m/n)} with high probability."[1]

## How it works

[edit]

2-choice hashing utilizes two hash functions h1(x) and h2(x) which work as hash functions are expected to work (i.e. mapping integers from the universe into a specified range). The two hash functions should be independent and have no correlation to each other. Having two hash functions allows any key x to have up to two potential locations to be stored based on the values of the respective outputs, h1(x) and h2(x). It is important to note that, although there are two hash functions, there is only one table; both hash functions map to locations on that table.

## Implementation

[edit]

The most important functions of the hashing implementation in this case are insertion and search.

* Insertion: When inserting the values of both hash functions are computed for the to-be-inserted object. The object is then placed in the bucket which contains fewer objects. If the buckets are equal in size, the default location is the h1(x) value.
* Search: Effective searches are done by looking in both buckets (the bucket locations to which h1(x) and h2(x) mapped) for the desired value.

## Performance

[edit]

As is true with all hash tables, the performance is based on the largest bucket. Although there are instances where bucket sizes happen to be large based on the values and the hash functions used, this is rare. Having two hash functions and, therefore, two possible locations for any one value, makes the possibility of large buckets even more unlikely to happen.

The expected bucket size while using 2-choice hashing is: θ(log(log(n))). This improvement is due to the randomized concept known as The Power of Two Choices.

Using two hash functions offers substantial benefits over a single hash function. There is little improvement (and no change to the expected order statistics) if more than two hash functions are used: "Additional hash functions only decrease the maximum by a constant factor."[2]

Some people recommend a type of 2-choice hashing called two-way skewed-associative cache in some CPU caches.[3]

2-left hashing—using two hash tables of equal size n/2, and asymmetrically resolving ties by putting the key in the left hash table—has fewer collisions and therefore better performance than 2-choice hashing with one large hash table of size n.[4][full citation needed]

## References

[edit]

1. ^  This article incorporates public domain material from Paul E. Black. "2-choice hashing". Dictionary of Algorithms and Data Structures. NIST. 2008\. (accessed 2016-07-28).
2. ^ Paul E. Black, DADS, retrieved 29 January 2015.
3. ^ "Micro-Architecture".
4. ^  This article incorporates public domain material from Paul E. Black. "2-left hashing". Dictionary of Algorithms and Data Structures. NIST. 19 December 2012. (accessed 2015-09-15).

This article incorporates public domain material from Paul E. Black. "2-choice hashing". Dictionary of Algorithms and Data Structures. NIST.

## Further reading

[edit]

* Azar, Yossi; Broder, Andrei Z.; Karlin, Anna R.; Upfal, Eli (23–25 May 1994), "Balanced Allocations (extended abstract)" (PDF), Proceedings of the twenty-sixth annual ACM symposium on Theory of computing - STOC '94, Montréal: Association for Computing Machinery, pp. 593–602, CiteSeerX 10.1.1.38.5375, doi:10.1145/195058.195412, ISBN 0897916638, S2CID 1014349
* Azar, Yossi; Broder, Andrei Z.; Karlin, Anna R.; Upfal, Eli (1999), "Balanced Allocations" (PDF), SIAM J. Comput., 29 (1): 180–200, doi:10.1137/S0097539795288490