# 5.3: Hash Codes

- Page ID
- 8458

\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

\( \newcommand{\Span}{\mathrm{span}}\)

\( \newcommand{\id}{\mathrm{id}}\)

\( \newcommand{\Span}{\mathrm{span}}\)

\( \newcommand{\kernel}{\mathrm{null}\,}\)

\( \newcommand{\range}{\mathrm{range}\,}\)

\( \newcommand{\RealPart}{\mathrm{Re}}\)

\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

\( \newcommand{\Argument}{\mathrm{Arg}}\)

\( \newcommand{\norm}[1]{\| #1 \|}\)

\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)

\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)

\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

\( \newcommand{\vectorC}[1]{\textbf{#1}} \)

\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)The hash tables discussed in the previous section are used to associate data with integer keys consisting of \(\mathtt{w}\) bits. In many cases, we have keys that are not integers. They may be strings, objects, arrays, or other compound structures. To use hash tables for these types of data, we must map these data types to \(\mathtt{w}\)-bit hash codes. Hash code mappings should have the following properties:

- If \(\mathtt{x}\) and \(\mathtt{y}\) are equal, then \(\texttt{x.hashCode()}\) and \(\texttt{y.hashCode()}\) are equal.
- If \(\mathtt{x}\) and \(\mathtt{y}\) are not equal, then the probability that \(\texttt{x.hashCode()}=\texttt{y.hashCode()}\) should be small (close to \(1/2^{\mathtt{w}}\)).

The first property ensures that if we store \(\mathtt{x}\) in a hash table and later look up a value \(\mathtt{y}\) equal to \(\mathtt{x}\), then we will find \(\mathtt{x}\)--as we should. The second property minimizes the loss from converting our objects to integers. It ensures that unequal objects usually have different hash codes and so are likely to be stored at different locations in our hash table.

## \(\PageIndex{1}\) Hash Codes for Primitive Data Types

Small primitive data types like \(\mathtt{char}\), \(\mathtt{byte}\), \(\mathtt{int}\), and \(\mathtt{float}\) are usually easy to find hash codes for. These data types always have a binary representation and this binary representation usually consists of \(\mathtt{w}\) or fewer bits. (For example, in Java, \(\mathtt{byte}\) is an 8-bit type and \(\mathtt{float}\) is a 32-bit type.) In these cases, we just treat these bits as the representation of an integer in the range \(\{0,\ldots,2^\mathtt{w}-1\}\). If two values are different, they get different hash codes. If they are the same, they get the same hash code.

A few primitive data types are made up of more than \(\mathtt{w}\) bits, usually \(c\mathtt{w}\) bits for some constant integer \(c\). (Java's \(\mathtt{long}\) and \(\mathtt{double}\) types are examples of this with \(c=2\).) These data types can be treated as compound objects made of \(c\) parts, as described in the next section.

## \(\PageIndex{2}\) Hash Codes for Compound Objects

For a compound object, we want to create a hash code by combining the individual hash codes of the object's constituent parts. This is not as easy as it sounds. Although one can find many hacks for this (for example, combining the hash codes with bitwise exclusive-or operations), many of these hacks turn out to be easy to foil (see Exercises 5.7-5.9). However, if one is willing to do arithmetic with \(2\mathtt{w}\) bits of precision, then there are simple and robust methods available. Suppose we have an object made up of several parts \(P_0,\ldots,P_{r-1}\) whose hash codes are \(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1}\). Then we can choose mutually independent random \(\mathtt{w}\)-bit integers \(\mathtt{z}_0,\ldots,\mathtt{z}_{r-1}\) and a random \(2\mathtt{w}\)-bit odd integer \(\mathtt{z}\) and compute a hash code for our object with

\[ h(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})=

\left(

\left( \mathtt{z} \sum_{i=0}^{r-1}\mathtt{z}_i\mathtt{x}_i \right )

\bmod 2^{2\mathtt{w}}

\right)

\textrm{div } 2^{\mathtt{w}}

\enspace . \nonumber\]

Note that this hash code has a final step (multiplying by \(\mathtt{z}\) and dividing by \(2^{\mathtt{w}}\)) that uses the multiplicative hash function from Section 5.1.1 to take the \(2\mathtt{w}\)-bit intermediate result and reduce it to a \(\mathtt{w}\)-bit final result. Here is an example of this method applied to a simple compound object with three parts \(\mathtt{x0}\), \(\mathtt{x1}\), and \(\mathtt{x2}\):

int hashCode() { // random numbers from rand.org long[] z = {0x2058cc50L, 0xcb19137eL, 0x2cb6b6fdL}; long zz = 0xbea0107e5067d19dL; // convert (unsigned) hashcodes to long long h0 = x0.hashCode() & ((1L<<32)-1); long h1 = x1.hashCode() & ((1L<<32)-1); long h2 = x2.hashCode() & ((1L<<32)-1); return (int)(((z[0]*h0 + z[1]*h1 + z[2]*h2)*zz) >>> 32); }

The following theorem shows that, in addition to being straightforward to implement, this method is provably good:

*Let \(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1}\) and \(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1}\) each be sequences of \(\mathtt{w}\) bit integers in \(\{0,\ldots,2^{\mathtt{w}}-1\}\) and assume \(\mathtt{x}_i \neq \mathtt{y}_i\) for at least one index \(i\in\{0,\ldots,r-1\}\). Then *

\[ \Pr\{

h(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1}) =

h(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1})

\} \le 3/2^{\mathtt{w}} \enspace . \nonumber\]

*Proof*. We will first ignore the final multiplicative hashing step and see how that step contributes later. Define:

\[ h'(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})=

\left(\sum_{j=0}^{r-1}

\mathtt{z}_j\mathtt{x}_j

\right)\bmod 2^{2\mathtt{w}} \enspace . \nonumber\]

Suppose that \(h'(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1}) = h'(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1})\). We can rewrite this as:

\[\mathtt{z}_i(\mathtt{x}_i-\mathtt{y}_i) \bmod 2^{2\mathtt{w}} = t\label{bighash-x}\]

where

\[ t = \left(

\sum_{j=0}^{i-1} \mathtt{z}_j( \mathtt{y}_j-\mathtt{x}_j)+

\sum_{j=i+1}^{r-1}\mathtt{z}_j( \mathtt{y}_j-\mathtt{x}_j)

\right) \bmod 2^{2\mathtt{w}} \nonumber\]

If we assume, without loss of generality that \(\mathtt{x}_i> \mathtt{y}_i\), then (\(\ref{bighash-x}\)) becomes

\[\mathtt{z}_i(\mathtt{x}_i-\mathtt{y}_i) = t\enspace ,\label{bighash-xx}\]

since each of \(\mathtt{z}_i\) and \((\mathtt{x}_i-\mathtt{y}_i)\) is at most \(2^{\mathtt{w}}-1\), so their product is at most \(2^{2\mathtt{w}}-2^{\mathtt{w}+1}+1 < 2^{2\mathtt{w}}-1\). By assumption, \(\mathtt{x}_i-\mathtt{y}_i\neq 0\), so (\(\ref{bighash-xx}\)) has at most one solution in \(\mathtt{z}_i\). Therefore, since \(\mathtt{z}_i\) and \(t\) are independent ( \(\mathtt{z}_0,\ldots,\mathtt{z}_{r-1}\) are mutually independent), the probability that we select \(\mathtt{z}_i\) so that \(h'(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})=h'(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1})\) is at most \(1/2^{\mathtt{w}}\).

The final step of the hash function is to apply multiplicative hashing to reduce our \(2\mathtt{w}\)-bit intermediate result \(h'(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})\) to a \(\mathtt{w}\)-bit final result \(h(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})\). By Theorem \(\PageIndex{1}\), if \(h'(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})\neq h'(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1})\), then \(\Pr\{h(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1}) = h(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1})\} \le 2/2^{\mathtt{w}}\).

To summarize,

\[\begin{align}

&

\Pr\left\{\begin{array}{l} h(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})\\

\quad =h(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1}) \end{array}\right\}\nonumber\\

&

= \Pr \left\{\begin{array}{ll}

\mbox{$h'(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})=h'(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1})$ or}\\

h'(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})\ne h'(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1})\\

\quad\mbox{and $\mathtt{z}h'(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})\text{ div }2^{\mathtt{w}}=\mathtt{z}h'(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1})\text{ div } 2^{\mathtt{w}}$}

\end{array}\right\}\nonumber\\

&

\le 1/2^{\mathtt{w}} + 2/2^{\mathtt{w}} = 3/2^{\mathtt{w}} \enspace .\nonumber

\end{align}\nonumber\]

## \(\PageIndex{3}\) Hash Codes for Arrays and Strings

The method from the previous section works well for objects that have a fixed, constant, number of components. However, it breaks down when we want to use it with objects that have a variable number of components, since it requires a random \(\mathtt{w}\)-bit integer \(\mathtt{z}_i\) for each component. We could use a pseudorandom sequence to generate as many \(\mathtt{z}_i\)'s as we need, but then the \(\mathtt{z}_i\)'s are not mutually independent, and it becomes difficult to prove that the pseudorandom numbers don't interact badly with the hash function we are using. In particular, the values of \(t\) and \(\mathtt{z}_i\) in the proof of Theorem \(\PageIndex{1}\) are no longer independent.

A more rigorous approach is to base our hash codes on polynomials over prime fields; these are just regular polynomials that are evaluated modulo some prime number, \(\mathtt{p}\). This method is based on the following theorem, which says that polynomials over prime fields behave pretty-much like usual polynomials:

*Let \(\mathtt{p}\) be a prime number, and let *\(f(\mathtt{z}) = \mathtt{x}_0\mathtt{z}^0+\mathtt{x}_1\mathtt{z}^1 + \cdots + \mathtt{x}_{r-1}\mathtt{z}^{r-1}\)* be a non-trivial polynomial with coefficients \(\mathtt{x}_i\in\{0,\ldots,\mathtt{p}-1\}\). Then the equation \(f(\mathtt{z})\bmod \mathtt{p} = 0\) has at most \(r-1\) solutions for \(\mathtt{z}\in\{0,\ldots,p-1\}\).*

To use Theorem \(\PageIndex{2}\), we hash a sequence of integers \(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1}\) with each \(\mathtt{x}_i\in \{0,\ldots,\mathtt{p}-2\}\) using a random integer \(\mathtt{z}\in\{0,\ldots,\mathtt{p}-1\}\) via the formula

\[ h(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})=

\left(

\mathtt{x}_0\mathtt{z}^0

+ \cdots

+ \mathtt{x}_{r-1}\mathtt{z}^{r-1}

+(\mathtt{p}-1)\mathtt{z}^r

\right)\bmod \mathtt{p} \enspace . \nonumber\]

Note the extra \((\mathtt{p}-1)\mathtt{z}^r\) term at the end of the formula. It helps to think of \((\mathtt{p}-1)\) as the last element, \(\mathtt{x}_r\), in the sequence \(\mathtt{x}_0,\ldots,\mathtt{x}_{r}\). Note that this element differs from every other element in the sequence (each of which is in the set \(\{0,\ldots,\mathtt{p}-2\}\)). We can think of \(\mathtt{p}-1\) as an end-of-sequence marker.

The following theorem, which considers the case of two sequences of the same length, shows that this hash function gives a good return for the small amount of randomization needed to choose \(\mathtt{z}\):

*Let \(\mathtt{p}>2^{\mathtt{w}}+1\) be a prime, let \(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1}\) and \(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1}\) each be sequences of \(\mathtt{w}\)-bit integers in \(\{0,\ldots,2^{\mathtt{w}}-1\}\), and assume \(\mathtt{x}_i \neq \mathtt{y}_i\) for at least one index \(i\in\{0,\ldots,r-1\}\). Then *

\[ \Pr\{ h(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})

=h(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1}) \}

\le (r-1)/\mathtt{p} \} \enspace . \nonumber\]

*Proof*. The equation \(h(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1}) = h(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1})\) can be rewritten as

\[\left( (\mathtt{x}_0-\mathtt{y}_0)\mathtt{z}^0

+ \cdots +

(\mathtt{x}_{r-1}-\mathtt{y}_{r-1})\mathtt{z}^{r-1} \right)

\bmod \mathtt{p} = 0.

\label{strhash-eqlen}\]

Since \(\mathtt{x}_\mathtt{i}\neq \mathtt{y}_\mathtt{i}\), this polynomial is non-trivial. Therefore, by Theorem \(\PageIndex{2}\), it has at most \(r-1\) solutions in \(\mathtt{z}\). The probability that we pick \(\mathtt{z}\) to be one of these solutions is therefore at most \((r-1)/\mathtt{p}\).

Note that this hash function also deals with the case in which two sequences have different lengths, even when one of the sequences is a prefix of the other. This is because this function effectively hashes the infinite sequence

\[ \mathtt{x}_0,\ldots,\mathtt{x}_{r-1}, \mathtt{p}-1,0,0,\ldots \enspace . \nonumber\]

This guarantees that if we have two sequences of length \(r\) and \(r'\) with \(r > r'\), then these two sequences differ at index \(i=r\). In this case, (\(\ref{strhash-eqlen}\)) becomes

\[ \left( \sum_{i=0}^{i=r'-1}(\mathtt{x}_i-\mathtt{y}_i)\mathtt{z}^{i}

+(\mathtt{x}_{r'}-\mathtt{p}+1)\mathtt{z}^{r'}

+\sum_{i=r'+1}^{i=r-1}\mathtt{x}_i\mathtt{z}^{i}

+(\mathtt{p}-1)\mathtt{z}^{r} \right)

\bmod \mathtt{p} = 0 \enspace , \nonumber\]

which, by Theorem \(\PageIndex{2}\), has at most \(r\) solutions in \(\mathtt{z}\). This combined with Theorem \(\PageIndex{3}\) suffice to prove the following more general theorem:

*Let \(\mathtt{p}>2^{\mathtt{w}}+1\) be a prime, let \(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1}\) and \(\mathtt{y}_0,\ldots,\mathtt{y}_{r'-1}\) be distinct sequences of \(\mathtt{w}\)-bit integers in \(\{0,\ldots,2^{\mathtt{w}}-1\}\). Then *

\[ \Pr\{ h(\mathtt{x}_0,\ldots,\mathtt{x}_{r-1})

=h(\mathtt{y}_0,\ldots,\mathtt{y}_{r-1}) \}

\le \max\{r,r'\}/\mathtt{p} \enspace . \nonumber\]

The following example code shows how this hash function is applied to an object that contains an array, \(\mathtt{x}\), of values:

int hashCode() { long p = (1L<<32)-5; // prime: 2^32 - 5 long z = 0x64b6055aL; // 32 bits from random.org int z2 = 0x5067d19d; // random odd 32 bit number long s = 0; long zi = 1; for (int i = 0; i < x.length; i++) { // reduce to 31 bits long xi = (x[i].hashCode() * z2) >>> 1; s = (s + zi * xi) % p; zi = (zi * z) % p; } s = (s + zi * (p-1)) % p; return (int)s; }

The preceding code sacrifices some collision probability for implementation convenience. In particular, it applies the multiplicative hash function from Section 5.1.1, with \(\mathtt{d}=31\) to reduce \(\texttt{x[i].hashCode()}\) to a 31-bit value. This is so that the additions and multiplications that are done modulo the prime \(\mathtt{p}=2^{32}-5\) can be carried out using unsigned 63-bit arithmetic. Thus the probability of two different sequences, the longer of which has length \(r\), having the same hash code is at most

\[ 2/2^{31} + r/(2^{32}-5) \nonumber\]

rather than the \(r/(2^{32}-5)\) specified in Theorem \(\PageIndex{4}\).