2-choice Hashing
   HOME

TheInfoList



OR:

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 In computing, a hash table, also known as hash map, is a data structure that implements an associative array or dictionary. It is an abstract data type that maps keys to values. A hash table uses a hash function to compute an ''index'', ...
is needed, unless keys are kept in buckets. The
average-case cost In computer science, best, worst, and average cases of a given algorithm express what the resource usage is ''at least'', ''at most'' and ''on average'', respectively. Usually the resource being considered is running time, i.e. time complexity, b ...
of a successful search is O(2 + (m - 1) / n), where m is the number of keys and n is the size of the array. The most collisions is \log_2 \ln n + \theta(m/n) with high probability."


How it works

2-choice hashing utilizes two hash functions ''h''1(''x'') and ''h''2(''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, ''h''1(''x'') and ''h''2(''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

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 ''h''1(''x'') value. * Search: Effective searches are done by looking in both buckets (the bucket locations to which ''h''1(''x'') and ''h''2(''x'') mapped) for the desired value.


Performance

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: . This improvement is due to the randomized concept known as The
Power of Two Choices Power most often refers to: * Power (physics), meaning "rate of doing work" ** Engine power, the power put out by an engine ** Electric power * Power (social and political), the ability to influence people or events ** Abusive power Power may ...
. 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." Some people recommend a type of 2-choice hashing called two-way skewed-associative cache in some CPU caches. 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''. 19 December 2012. (accessed 2015-09-15).


References


Further reading

* *{{citation , first1=Yossi , last1=Azar , first2=Andrei Z. , last2=Broder , first3=Anna R. , last3=Karlin , first4=Eli , last4=Upfal , title=Balanced Allocations , journal=SIAM J. Comput. , volume=29 , issue=1 , pages=180–200 , date=1999 , doi=10.1137/S0097539795288490 , url=https://cs.brown.edu/people/eupfal/papers/SICOMP29.pdf Hashing