1
$\begingroup$

Is the Simple Uniform Hashing Assumption (SUHA) sufficient to show that the worst-case time complexity of hash table lookups is O(1)?

It says in the Wikipedia article that this assumption implies that the average length of a chain is $\alpha = n / m$, but...

  • ...this is true even without this assumption, right? If the distribution is [4, 0, 0, 0] the average length is still 1.
  • ...this is a probabilistic statement, which is of little use when discussing worst case complexity, right?

It seems to me like a different assumption would be needed. Something like:

The difference between the largest and smallest bucket is bounded by a constant factor.

Maybe this is this implied by SUHA? If so, I don't see how.

$\endgroup$
  • $\begingroup$ A hashtable with $ [4, 0, 0, 0] $ probabilities, would generate an average chain length of $n$, not $n/m$, as it will chain all the elements together. Think of average chain length like this: what's the expected length of a chain that a random element $x$ would belong to. $\endgroup$ – Ameer Jewdaki Nov 25 at 15:44
  • $\begingroup$ The average of 4, 0, 0, 0 is 1. Perhaps I expressed myself poorly in the question. Do you have a better way of putting it? $\endgroup$ – aioobe Nov 25 at 16:04
  • $\begingroup$ it's the weighted average, in your example the weight of zero-length chains is zero, so the overall average is $4$ not $1$. $\endgroup$ – Ameer Jewdaki Nov 25 at 16:13
  • $\begingroup$ plus, anything that holds in expection, i.e. $\alpha = n/m$, can be converted to a probabablistic bound by Markov inequality: $P(\text{query time of x}\ge t) \le E(\text{chain length of x})/t =n/(m t)$, $\endgroup$ – Ameer Jewdaki Nov 25 at 16:16
  • $\begingroup$ Why is it the weighted average? I don't think it's correct to assume that one will only look up keys that exist in the table. Regarding probabilistic bounds, is that relevant when discussing worst-case time complexity? $\endgroup$ – aioobe Nov 25 at 16:59
1
$\begingroup$

Not only is SUHA insufficient to show that the largest bucket has size $O(\alpha)$, but if $m = n$, then there is a high probability that for any particular set of keys, the longest chain has length $\Theta(\lg n / \lg \lg n)$. There is a proof of this in CLRS and many proofs available in lecture notes.

$\endgroup$
  • $\begingroup$ But what's the expected length of the shortest chain? $\endgroup$ – aioobe Nov 24 at 20:48
  • $\begingroup$ As long as you have fewer items than buckets, the expected length of the shortest chain is 0. $\endgroup$ – gnasher729 Nov 24 at 20:49
  • $\begingroup$ Up until you have $\Omega(m \lg m)$ items, the expected length of the shortest chain is 0. This is called the "coupon collector's problem". $\endgroup$ – jbapple Nov 25 at 14:45
  • $\begingroup$ Ok. Hmm.. This might be key, but I'm still unsure. It seems we're discussing the same thing in both comment threads here, but what I'm questioning here is basically that θ(lg n / lg lg n) for longest chain implies that it's not a constant factor more than the shortest chain. It quite possibly is, in fact I'd be surprised if it's not, but as far as I can see, the proof is not complete without that last bit. $\endgroup$ – aioobe Nov 25 at 15:21
  • $\begingroup$ Are you saying that you think even the average chain is also $\Theta(\lg n / \lg \lg n)$? $\endgroup$ – jbapple Nov 25 at 16:29
1
$\begingroup$

I think the answer is no, SUHA does not imply anything regarding worst-case time complexity.

The bottom line is that

  • the hashing is (even though uniform) still viewed as random, and
  • the keys are unknown.

Regardless of how small the probability is for all keys to hash to the same bucket, it's still a theoretical possibility, thus the theoretical worst-case is still O(n).

When discussing hash tables and complexity, I think this is mentioned briefly, regarded as irrelevant in practice and then the discussion moves on to expected run time complexity, and we're in O(1) land.

With the assumption mentioned in the question...

The difference between the largest and smallest bucket is bounded by a constant factor.

...(which is strictly stronger than SUHA) it can indeed be shown that the worst-case run time complexity is O(1). This assumption implies that even the largest bucket is within a constant factor of the load factor limit. This in turn means all linked lists are bounded by a constant and we get O(1) as worst case.

This assumption however is somewhat unreasonable. While it's quite probable that it holds true for any descent hash function and non-pathological set of keys, how would one guarantee such property? One would have to push this assumption further down and make constraints on what keys are allowed to be stored in the hash table and so on. This is what makes it unreasonable in a sense.

$\endgroup$
  • $\begingroup$ It is actually quite low probability that the difference between the largest and smallest bucket is bounded by a constant factor. $\endgroup$ – jbapple Nov 24 at 20:13
  • $\begingroup$ How come? Could you elaborate? $\endgroup$ – aioobe Nov 24 at 20:49
  • $\begingroup$ See cmurandomized.wordpress.com/2011/02/02/lecture-8-balls-and-bins $\endgroup$ – jbapple Nov 25 at 14:42
  • $\begingroup$ I've read that page twice without finding any theorem of a formula for shortest path. I'm asking, because the expected length of the longest path, divided by the expected length of the shortest path could still be constant, right? $\endgroup$ – aioobe Nov 25 at 15:14
1
$\begingroup$

Let $h:[U]\to[m]$ be a uniform hash function, and $x_1,\dots,x_n\in[U]$ be the elements currently stored in the hash table, and we want to look up the query $q\in[U]$. Finally let $X_i$ be the indicator variable for the event that $x_i$ collides with $q$: $$X_i = \begin{cases}1 && h(x_i)=h(q)\\0 &&h(x_i)\ne h(q)\end{cases} \Rightarrow P(X_i=1) = \frac{1}{m}$$ Then the time $T$ to loop up $q$ is equal to the number of elements that collide with it: $$T=\sum_{i=1}^n X_i$$ By linearity of expectation we have: $$\mathbb{E} [T] = \sum_i\mathbb{E} X_i = \frac{n}{m}=\alpha$$ And finally by Markov's inequality: $$ P(T>t) \le \frac{\alpha}{t}$$ Now if we set $t:={\alpha}/{\delta}$, we will have: $$P(\text{look-up time}\ge \frac{\alpha}{\delta}) \le \delta$$ Think of $\delta$ as the failure rate. So for $\delta=0.1$, the query time is bounded by $10\alpha$ with probability $0.9$. If you strengthen the assumption about $h$ to a $k$-wise independent hash, the failure rate rouhgly drops to $P(\text{fail})\approx\delta^k$ and so you can have a much better complexity/failure-rate trade-off.

Now, a more conservative analysis is, what is the length of the longest chain in your table? In this case, we can define indicator variables $X_{i,j}$ for $i,j\in[n]$, that indicate the event that $x_i$ and $x_j$ collide. Clearly, $P(X_{i,j})=1/m$. Then the expected total number of collisions $S$ is: $$\mathbb{E} S = \sum_{i,j\le n} \mathbb{E} X_{i,j} = \binom{n}{2}\frac{1}{m}$$ If the maximum chain length is $\ell$, there is $\binom{\ell}{2}$ pairs of elements in that chain that collide with each other, implying $S\ge \binom{\ell}{2}$. We have: $$P(\text{max chain length}\ge \ell) \le P\left(S\ge \binom{\ell}{2}\right) \le\frac{ \mathbb{E} S} { \binom{\ell}{2}}\approx (n^2/m\ell^2) $$ If you set $\ell:=n^2/m\delta$ the probablistic bound is: $$P(\text{max chain length}\ge \ell) \le \delta \qquad \text{ for } \ell = \frac{n}{\delta \sqrt{m}}$$ So if $m\approx n$, your maximum length grows like $\ell\approx \sqrt{n}/\delta$ with $1-\delta$ probability. This is because the simple uniform hash assumption is really weak. The more independence you assume, the better the guarantee. For a universally uniform (meaning completely iid) hash function, you will get a logarithmic maximum chain $log(n)$ for $m\approx n$.

$\endgroup$
  • $\begingroup$ Nice. But this is a probabilistic analysis, right? I don't think it matters when discussing worst-case scenarios. $\endgroup$ – aioobe Nov 25 at 18:54
  • $\begingroup$ Yes, that's what I tried to point out in the last paragraph. Typically, these kinds of probabilistic statements are the randomized equivalent of worst-case analysis for deterministic algorithms. In the actual worst-case analysis, there is always a non-zero probability that your hash function produces a chain of length $n$, but that would be very unlikely, and is not very meaningful. $\endgroup$ – Ameer Jewdaki Nov 25 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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