ArticleslgStudy

science

2-choice hashing

2-choice hashing is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand 2-choice hashing rather than just read about it. In short: 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.

Key takeaways

  • 2-choice hashing belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect 2-choice hashing to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of 2-choice hashing from memory before moving on to harder problems.

Reference excerpt

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), where m is the number of keys and n is the size of the array. The most collisions is log2ln⁡n+θ(m/n) with high probability."

How it works 2-choice hashing utilizes two hash functions h1(x) and h2(x). 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). 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 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 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." 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.

References

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

Further reading 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, 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

Worked examples

Example 1 — a first encounter with 2-choice hashing

Start with the simplest possible case. Write down what 2-choice hashing claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to 2-choice hashing before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about 2-choice hashing ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of 2-choice hashing

In research
2-choice hashing appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses 2-choice hashing in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
2-choice hashing is common in secondary-school and first-year university syllabi. It links to neighbouring topics Hashing, so understanding it makes those chapters shorter.
In everyday life
Look for 2-choice hashing outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “2-choice hashing” →

How to study 2-choice hashing in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what 2-choice hashing means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain 2-choice hashing out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is 2-choice hashing in simple terms?

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.

Why does 2-choice hashing matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study 2-choice hashing?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on 2-choice hashing.

Tags

  • Hashing

Keep exploring