5.4 Discussion and Exercises

Hash tables and hash codes represent an enormous and active field of research that is just touched upon in this chapter. The online Bibliography on Hashing [10] contains nearly 2000 entries.

A variety of different hash table implementations exist. The one described in Section 5.1 is known as hashing with chaining (each array entry contains a chain (List) of elements). Hashing with chaining dates back to an internal IBM memorandum authored by H. P. Luhn and dated January 1953. This memorandum also seems to be one of the earliest references to linked lists.

An alternative to hashing with chaining is that used by open addressing schemes, where all data is stored directly in an array. These schemes include the LinearHashTable structure of Section 5.2. This idea was also proposed, independently, by a group at IBM in the 1950s. Open addressing schemes must deal with the problem of collision resolution: the case where two values hash to the same array location. Different strategies exist for collision resolution; these provide different performance guarantees and often require more sophisticated hash functions than the ones described here.

Yet another category of hash table implementations are the so-called perfect hashing methods. These are methods in which $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ operations take $ O(1)$ time in the worst-case. For static data sets, this can be accomplished by finding perfect hash functions for the data; these are functions that map each piece of data to a unique array location. For data that changes over time, perfect hashing methods include FKS two-level hash tables [31,24] and cuckoo hashing [55].

The hash functions presented in this chapter are probably among the most practical methods currently known that can be proven to work well for any set of data. Other provably good methods date back to the pioneering work of Carter and Wegman who introduced the notion of universal hashing and described several hash functions for different scenarios [14]. Tabulation hashing, described in Section 5.2.3, is due to Carter and Wegman [14], but its analysis, when applied to linear probing (and several other hash table schemes) is due to P{\v{a\/}}\kern.05emtra{\c{s\/}}cu and Thorup [58].

The idea of multiplicative hashing is very old and seems to be part of the hashing folklore [48, Section 6.4]. However, the idea of choosing the multiplier $ \ensuremath{\ensuremath{\mathit{z}}}$ to be a random odd number, and the analysis in Section 5.1.1 is due to Dietzfelbinger et al. [23]. This version of multiplicative hashing is one of the simplest, but its collision probability of $ 2/2^{\ensuremath{\ensuremath{\ensuremath{\mathit{d}}}}}$ is a factor of two larger than what one could expect with a random function from $ 2^{\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}}\to
2^{\ensuremath{\ensuremath{\ensuremath{\mathit{d}}}}}$. The multiply-add hashing method uses the function

$\displaystyle h(\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}) = ((\ensurem...
...ensuremath{\ensuremath{2w}}-\ensuremath{\ensuremath{\ensuremath{\mathit{d}}}}}
$

where $ \ensuremath{\ensuremath{\mathit{z}}}$ and $ \ensuremath{\ensuremath{\mathit{b}}}$ are each randomly chosen from $ \{0,\ldots,2^{\ensuremath{\ensuremath{2w}}}-1\}$. Multiply-add hashing has a collision probability of only $ 1/2^{\ensuremath{\ensuremath{\ensuremath{\mathit{d}}}}}$ [21], but requires $ 2\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$-bit precision arithmetic.

There are a number of methods of obtaining hash codes from fixed-length sequences of $ \ensuremath{\ensuremath{\mathit{w}}}$-bit integers. One particularly fast method [11] is the function

\begin{displaymath}\begin{array}{l}
h(\ensuremath{\ensuremath{\ensuremath{\math...
...2\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}}
\end{array}\end{displaymath}

where $ r$ is even and $ \ensuremath{\ensuremath{\ensuremath{\mathit{a}}}}_0,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{a}}}}_{r-1}$ are randomly chosen from $ \{0,\ldots,2^{\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}}\}$. This yields a $ 2\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$-bit hash code that has collision probability $ 1/2^{\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}}$. This can be reduced to a $ \ensuremath{\ensuremath{\mathit{w}}}$-bit hash code using multiplicative (or multiply-add) hashing. This method is fast because it requires only $ r/2$ $ 2\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$-bit multiplications whereas the method described in Section 5.3.2 requires $ r$ multiplications. (The $ \bmod$ operations occur implicitly by using $ \ensuremath{\ensuremath{\mathit{w}}}$ and $ 2\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$-bit arithmetic for the additions and multiplications, respectively.)

The method from Section 5.3.3 of using polynomials over prime fields to hash variable-length arrays and strings is due to Dietzfelbinger et al. [22]. Due to its use of the $ \bmod$ operator which relies on a costly machine instruction, it is, unfortunately, not very fast. Some variants of this method choose the prime $ \ensuremath{\ensuremath{\mathit{p}}}$ to be one of the form $ 2^{\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}}-1$, in which case the $ \bmod$ operator can be replaced with addition ($ +$) and bitwise-and ($ \wedge $) operations [47, Section 3.6]. Another option is to apply one of the fast methods for fixed-length strings to blocks of length $ c$ for some constant $ c>1$ and then apply the prime field method to the resulting sequence of $ \lceil r/c\rceil$ hash codes.

Exercise 5..1   A certain university assigns each of its students student numbers the first time they register for any course. These numbers are sequential integers that started at 0 many years ago and are now in the millions. Suppose we have a class of one hundred first year students and we want to assign them hash codes based on their student numbers. Does it make more sense to use the first two digits or the last two digits of their student number? Justify your answer.

Exercise 5..2   Consider the hashing scheme in Section 5.1.1, and suppose $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}=2^{\ensuremath{\ensuremath{\ensuremath{\mathit{d}}}}}$ and $ \ensuremath{\ensuremath{\ensuremath{\mathit{d}}}}\le \ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}/2$.
  1. Show that, for any choice of the muliplier, $ \ensuremath{\ensuremath{\mathit{z}}}$, there exists $ \ensuremath{\ensuremath{\mathit{n}}}$ values that all have the same hash code. (Hint: This is easy, and doesn't require any number theory.)
  2. Given the multiplier, $ \ensuremath{\ensuremath{\mathit{z}}}$, describe $ \ensuremath{\ensuremath{\mathit{n}}}$ values that all have the same hash code. (Hint: This is harder, and requires some basic number theory.)

Exercise 5..3   Prove that the bound $ 2/2^{\ensuremath{\ensuremath{\ensuremath{\mathit{d}}}}}$ in Lemma 5.1 is the best possible bound by showing that, if $ x=2^{\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{d}}}}-2}$ and $ \ensuremath{\ensuremath{\ensuremath{\mathit{y}}}}=3\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}$, then $ \Pr\{\ensuremath{\ensuremath{\mathrm{hash}(\ensuremath{\mathit{x}})}}=\ensurem...
...remath{\mathit{y}})}}\}=2/2^{\ensuremath{\ensuremath{\ensuremath{\mathit{d}}}}}$. (Hint look at the binary representations of $ \ensuremath{\ensuremath{\ensuremath{\mathit{zx}}}}$ and $ \ensuremath{\ensuremath{\ensuremath{\mathit{z}}}}3\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}$ and use the fact that $ \ensuremath{\ensuremath{\ensuremath{\mathit{z}}}}3\ensuremath{\ensuremath{\ens...
...x\ensuremath{+\ensuremath{2}}z\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}$.)

Exercise 5..4   Reprove Lemma 5.4 using the full version of Stirling's Approximation given in Section 1.3.2.

Exercise 5..5   Consider the following simplified version of the code for adding an element $ \ensuremath{\ensuremath{\mathit{x}}}$ to a LinearHashTable, which simply stores $ \ensuremath{\ensuremath{\mathit{x}}}$ in the first $ \ensuremath{\ensuremath{\mathit{nil}}}$ array entry it finds. Explain why this could be very slow by giving an example of a sequence of $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$, and $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ operations that would take on the order of $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}^2$ time to execute.
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{add\_slow}(\...
...return}} \ensuremath{\ensuremath{\mathit{true}}}\\
\end{flushleft}\end{leftbar}

Exercise 5..6   Early versions of the Java $ \ensuremath{\mathrm{hash\_code}()}$ method for the String class worked by not using all of the characters found in long strings. For example, for a sixteen character string, the hash code was computed using only the eight even-indexed characters. Explain why this was a very bad idea by giving an example of large set of strings that all have the same hash code.

Exercise 5..7   Suppose you have an object made up of two $ \ensuremath{\ensuremath{\mathit{w}}}$-bit integers, $ \ensuremath{\ensuremath{\mathit{x}}}$ and $ \ensuremath{\ensuremath{\mathit{y}}}$. Show why $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\oplus\ensuremath{\ensuremath{\ensuremath{\mathit{y}}}}$ does not make a good hash code for your object. Give an example of a large set of objects that would all have hash code 0.

Exercise 5..8   Suppose you have an object made up of two $ \ensuremath{\ensuremath{\mathit{w}}}$-bit integers, $ \ensuremath{\ensuremath{\mathit{x}}}$ and $ \ensuremath{\ensuremath{\mathit{y}}}$. Show why $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}+\ensuremath{\ensuremath{\ensuremath{\mathit{y}}}}$ does not make a good hash code for your object. Give an example of a large set of objects that would all have the same hash code.

Exercise 5..9   Suppose you have an object made up of two $ \ensuremath{\ensuremath{\mathit{w}}}$-bit integers, $ \ensuremath{\ensuremath{\mathit{x}}}$ and $ \ensuremath{\ensuremath{\mathit{y}}}$. Suppose that the hash code for your object is defined by some deterministic function $ h(\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}},\ensuremath{\ensuremath{\ensuremath{\mathit{y}}}})$ that produces a single $ \ensuremath{\ensuremath{\mathit{w}}}$-bit integer. Prove that there exists a large set of objects that have the same hash code.

Exercise 5..10   Let $ p=2^{\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}}-1$ for some positive integer $ \ensuremath{\ensuremath{\mathit{w}}}$. Explain why, for a positive integer $ x$

$\displaystyle (x\bmod 2^{\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}}) + ...
... x \bmod (2^{\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}}-1) \enspace .
$

(This gives an algorithm for computing $ x \bmod (2^{\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}}-1)$ by repeatedly setting until $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}} \le 2^{\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}}-1$.)

Exercise 5..11   Find some commonly used hash table implementation such as the ( or the HashTable or LinearHashTable implementations in this book, and design a program that stores integers in this data structure so that there are integers, $ \ensuremath{\ensuremath{\mathit{x}}}$, such that $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ takes linear time. That is, find a set of $ \ensuremath{\ensuremath{\mathit{n}}}$ integers for which there are $ c\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}$ elements that hash to the same table location.

Depending on how good the implementation is, you may be able to do this just by inspecting the code for the implementation, or you may have to write some code that does trial insertions and searches, timing how long it takes to add and find particular values. (This can be, and has been, used to launch denial of service attacks on web servers [17].)

opendatastructures.org