The ChainedHashTable data structure uses an array of lists, where the th list stores all elements such that . An alternative, called open addressing is to store the elements directly in an array, , with each array location in storing at most one value. This approach is taken by the LinearHashTable described in this section. In some places, this data structure is described as open addressing with linear probing.
The main idea behind a LinearHashTable is that we would, ideally, like to store the element with hash value in the table location . If we cannot do this (because some element is already stored there) then we try to store it at location ; if that's not possible, then we try , and so on, until we find a place for .
There are three types of entries stored in :
To summarize, a LinearHashTable contains an array,
, that stores
data elements, and integers
that keep track of the number of
data elements and non-
, respectively. Because many
hash functions only work for table sizes that are a power of 2, we also
keep an integer
and maintain the invariant that
operation in a LinearHashTable is simple. We start
at array entry
and search entries
, and so on,
until we find an index
such that, either,
In the former case we return
. In the latter case, we conclude
is not contained in the hash table and return
operation is also fairly easy to implement. After checking
is not already stored in the table (using
), we search
and so on, until we find a
at that location,
, if appropriate.
By now, the implementation of the
operation should be obvious.
, and so on until we find an index
. In the former case, we set
. In the latter case we conclude that
was not stored in the
table (and therefore cannot be deleted) and return
The correctness of the , , and methods is easy to verify, though it relies on the use of values. Notice that none of these operations ever sets a non- entry to . Therefore, when we reach an index such that , this is a proof that the element, , that we are searching for is not stored in the table; has always been , so there is no reason that a previous operation would have proceeded beyond index .
method is called by
when the number of non-
when the number of
data entries is less than
methods in other array-based data structures.
We find the smallest non-negative integer
. We reallocate the array
so that it has size
and then we insert all the elements in the old version of
newly-resized copy of
. While doing this, we reset
since the newly-allocated
Notice that each operation, , , or , finishes as soon as (or before) it discovers the first entry in . The intuition behind the analysis of linear probing is that, since at least half the elements in are equal to , an operation should not take long to complete because it will very quickly come across a entry. We shouldn't rely too heavily on this intuition, though, because it would lead us to (the incorrect) conclusion that the expected number of locations in examined by an operation is at most 2.
For the rest of this section, we will assume that all hash values are independently and uniformly distributed in . This is not a realistic assumption, but it will make it possible for us to analyze linear probing. Later in this section we will describe a method, called tabulation hashing, that produces a hash function that is ``good enough'' for linear probing. We will also assume that all indices into the positions of are taken modulo , so that is really a shorthand for .
We say that a run of length that starts at occurs when all the table entries are non- and . The number of non- elements of is exactly and the method ensures that, at all times, . There are elements that have been inserted into since the last operation. By our assumption, each of these has a hash value, , that is uniform and independent of the rest. With this setup, we can prove the main lemma required to analyze linear probing.
In the following derivation we will cheat a little and replace with . Stirling's Approximation (Section 1.3.2) shows that this is only a factor of from the truth. This is just done to make the derivation simpler; Exercise 5.4 asks the reader to redo the calculation more rigorously using Stirling's Approximation in its entirety.
The value of is maximized when is minimum, and the data structure maintains the invariant that , so
Using Lemma 5.4 to prove upper-bounds on the expected running time of , , and is now fairly straightforward. Consider the simplest case, where we execute for some value that has never been stored in the LinearHashTable. In this case, is a random value in independent of the contents of . If is part of a run of length , then the time it takes to execute the operation is at most . Thus, the expected running time can be upper-bounded by
If we ignore the cost of the operation, then the above analysis gives us all we need to analyze the cost of operations on a LinearHashTable.
First of all, the analysis of given above applies to the operation when is not contained in the table. To analyze the operation when is contained in the table, we need only note that this is the same as the cost of the operation that previously added to the table. Finally, the cost of a operation is the same as the cost of a operation.
In summary, if we ignore the cost of calls to , all operations on a LinearHashTable run in expected time. Accounting for the cost of resize can be done using the same type of amortized analysis performed for the ArrayStack data structure in Section 2.1.
The following theorem summarizes the performance of the LinearHashTable data structure:
Furthermore, beginning with an empty LinearHashTable, any sequence of and operations results in a total of time spent during all calls to .
While analyzing the LinearHashTable structure, we made a very strong
assumption: That for any set of elements,
the hash values
are independently and
uniformly distributed over the set
. One way to
achieve this is to store a giant array,
, of length
where each entry is a random
-bit integer, independent of all the
other entries. In this way, we could implement
-bit integer from
Here, , is the bitwise right shift operator, so extracts the most significant bits of 's -bit hash code.
Unfortunately, storing an array of size
is prohibitive in terms
of memory usage. The approach used by tabulation hashing is to,
-bit integers as being comprised of
integers, each having only
bits. In this way, tabulation hashing
arrays each of length
. All the entries
in these arrays are independent random
-bit integers. To obtain the value
and use these as indices into these arrays. We then combine all these
values with the bitwise exclusive-or operator to obtain
The following code shows how this works when
In this case, is a two-dimensional array with four columns and rows. Quantities like , used above, are hexadecimal numbers whose digits have 16 possible values 0-9, which have their usual meaning and a-f, which denote 10-15. The number . The symbol is the bitwise and operator, so code like extracts bits with index 8 through 15 of .
One can easily verify that, for any , is uniformly distributed over . With a little work, one can even verify that any pair of values have independent hash values. This implies tabulation hashing could be used in place of multiplicative hashing for the ChainedHashTable implementation.
However, it is not true that any set of distinct values gives a set of independent hash values. Nevertheless, when tabulation hashing is used, the bound of Theorem 5.2 still holds. References for this are provided at the end of this chapter.