1
$\begingroup$

I just answered this question on StackOverflow, which asks for an efficient algorithm such that given a nonempty hashtable, the algorithm should return a pointer to an arbitrary nonempty entry in the hashtable. My answer was quite casual and I am not confident if my proof was correct. I am rephrasing the problem statement below:

  • We are allowed to extend the insert()/remove() operations of a hashtable
  • We need to expose a get_arbitrary() operation that always returns a nonempty entry in the hashtable if it is nonempty, or nil if it is empty.
  • We are allowed to store extra data related to hashtable
  • Assume hash keys are uniformly distributed.
  • Assume insert()/remove() have memorylessly equal probability to be called on any key that exists in the hashtable.

This problem asks if it is possible to draw some relation on the lower bounds of the

  • time complexity of insert() $T_i(n)$
  • time complexity of remove() $T_r(n)$
  • time complexity of get_arbitrary() $T_a(n)$
  • space complexixty of any extra data we are storing, in addition to the original hashtable $S(n)$

One of the conjectures I thought of (as mentioned in the linked StackOverflow question) is $T_a(n) = O\left(\frac n{S(n)}\right)$, but I am not sure if this is correct. Also I am interested in knowing something can be said about how $T_a(n)$ relates to $T_r(n)$.

$\endgroup$

1 Answer 1

2
$\begingroup$

We usually assume that a reasonable proportion of entries in a hash table are used, or we would be wasting memory. If we know that a hash table is at least 20% full then we can just visit random locations until we find a used one , five or so in this case (starting with a random entry and checking consecutive entries wouldn’t work with badly distributed hash keys). You could do this if you re-hash the table with fewer slots if it gets to empty, or if you know that you never delete any hash entry.

You could have a bitmap of unused entries which would make the search faster by a large constant factor. You could create a linked list of the used slots if less than 20% of slots are used and maintain it until usage exceeds 50%, instead of re-hashing.

$\endgroup$
3
  • $\begingroup$ The expected number of entries to probe in a $\frac15$ full hashtable is not "five or so", but $\frac n5$. So the time to find an arbitrary entry is still $O(n)$. $\endgroup$
    – SOFe
    May 2, 2020 at 12:42
  • $\begingroup$ How do you maintain a linked list of used slots in the remove() operation? Accessing the $n$th element in a linked list is $O(n)$ as well. $\endgroup$
    – SOFe
    May 2, 2020 at 12:45
  • 1
    $\begingroup$ If your hashtable is 1/5 full, then (by definition) this means that a random entry will be empty with probability 4/5. Therefore the expected number of entries until you find an empty one is actually 5/4. $\endgroup$ May 2, 2020 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.