5

To look up a key in a hash map you have to

  1. calculate its hash
  2. find the entry in the resulting hash bucket

Hash calculation takes at least $O(l)$ operations when the hashes are $l$-bit-numbers.

When using an index (like a binary tree) for each bucket, finding an entry within a bucket that contains $k$ entries can be done in $O(\log k)$. With $n$ being the total number of entries in the hash map and $m$ being the number of buckets, $k$ averages to $n/m$. Due to $m=2^l$ we thus get $O(\log k) = O(\log n/m) = O(\log n - \log m) = O(\log n - l)$.

Combining these two runtimes one gets a total look-up time of $O(l + \log n - l) = O(\log n)$, which conforms to the intuition that a lookup in a collection with $n$ entries is not possible below $O(\log n)$ operations.

In short, it is generally assumed that $l$ and $k$ are both constant with regard to $n$. But if you fix $l$ then $k$ grows with $n$.

Am I missing something here?

Raphael
  • 72,336
  • 29
  • 179
  • 389
jederik
  • 267
  • 1
  • 4
  • @Raphael This question is fundamentally about hash tables. I don't see how a generic question about the RAM model does more than provide somewhat relevant background. – Gilles 'SO- stop being evil' Jul 28 '16 at 21:36
  • @Gilles You'll note that D.W.'s answer is essentially "because we use the uniform cost model", which is exactly the answer from over there. – Raphael Jul 29 '16 at 09:10
  • @Raphael So? The fact that one answer focuses on one aspect of the question doesn't mean that the question is a duplicate of another that's solely about this particular aspect. – Gilles 'SO- stop being evil' Jul 29 '16 at 10:11
  • @Gilles and Raphael: I looked for similar questions before I posted this one, but I found that none of them addresses the relation between l and k as explicitly as I would have wished for. – jederik Jul 29 '16 at 15:49