Randomized Algorithms (Spring 2010)/Hashing, limited independence
Limited Independence
k-wise independence
Recall the definition of independence between events:
Definition (Independent events):
|
Similarly, we can define independence between random variables:
Definition (Independent variables):
|
Mutual independence is an ideal condition of independence. The limited notion of independence is usually defined by the k-wise independence.
Definition (k-wise Independenc):
|
A very common case is pairwise independence, i.e. the 2-wise independence.
Definition (pairwise Independent random variables):
|
Note that the definition of k-wise independence is hereditary:
- If
are k-wise independent, then they are also -wise independent for any . - If
are NOT k-wise independent, then they cannot be -wise independent for any .
Construction via XOR
Suppose we have
Enumerate all the nonempty subsets of
where
0 0 0 0 1 1 1 0 1 1 1 0
There are
.
Sometimes,
We claim that
Theorem:
|
The proof is left for your exercise.
Therefore, we extract exponentially many pairwise independent uniform random bits from a sequence of mutually independent uniform random bits.
Note that
Construction via modulo a prime
We now consider constructing pairwise independent random variables ranging over
Let
Theorem:
|
Proof: We first show that
Due to the law of total probability,
For prime
We then show that
The event
Due to the Chinese remainder theorem, there exists a unique solution of
Tools for limited independence
For random viables with limited independence, we are not able to directly use the probability tools which rely on the independence of random variables, such as the Chernoff bounds. On the positive side, there are tools that require less independence.
In lecture 4, we show the following theorem of linearity of variance for pairwise independent random variables.
Theorem:
|
We proved the theorem by showing that the covariances of pairwise independent random variables are 0. The theorem is actually a consequence of a more general statement.
Theorem 1:
|
This phenomenon is sometimes called that the k-degree polynomials are fooled by k-wise independence. In other words, a k-degree polynomial behaves the same on the k-wise independent random variables as on the mutual independent random variables.
This theorem is implied by the following lemma.
Lemma:
|
The lemma can be proved by directly compute the expectation. We omit the detailed proof.
By the linearity of expectation, the expectation of a polynomial is reduced to the sum of the expectations of terms. For a k-degree polynomial, each term has at most
Since the
Chebyshev's inequality:
|
Two-point sampling
Consider a Monte Carlo randomized algorithm with one-sided error for a decision problem
- If
, then , where the probability is taken over the random choice of . - If
, then for any .
We call
For the
We can boost the accuracy (equivalently, reduce the error) of any Monte Carlo randomized algorithm with one-sided error by running the algorithm for a number of times.
Suppose that we sample
- return
;
- return
That is, return 1 if any instance of
Sampling
The following scheme reduces the error significantly with the same number of random bits:
Choose two independent uniform random number and from .Construct
random number by:Run
.
Due to the discussion in the last section, we know that for
By the linearity of expectations,
Since
Applying Chebyshev's inequality, we have that for any
The error is reduced to
Fooling a bounded depth circuit
Hashing
In a hash table,
For hash tables, the hash function is a random mapping from keys to values. To simplify the analysis, we assume that the hash function is uniformly random function
Families of universal hash functions
Carter and Wegman 1977
Definition (universal hash families):
|
In particular, for a 2-universal family
For a strongly 2-universal family
This behavior is exactly the same as uniform random hash functions on any pair of inputs. For this reason, a strongly 2-universal hash family are also called pairwise independent hash functions.
Construction of 2-universal family of hash functions
The construction of pairwise independent random variables via modulo a prime introduced in Section 1 already provides a way of constructing a strongly 2-universal hash family.
Let
and the family is
Lemma
|
Proof: In Section 1, we have proved the pairwise independence of the sequence of
- The original construction of Carter-Wegman
What if we want to have hash functions from
Suppose that the universe is
and the family
Note that unlike the first construction, now
Lemma (Carter-Wegman)
|
- A construction used in practice
The main issue of Carter-Wegman construction is the efficiency. The mod operation is very slow, and has been so for more than 30 years.
The following construction is due to Dietzfelbinger et al. It was published in 1997 and has been practically used in various applications of universal hashing.
The family of hash functions is from
and the family
This family of hash functions does not exactly meet the requirement of 2-universal family. However, Dietzfelbinger et al proved that
So
The function is extremely simple to compute in c language.
We exploit that C-multiplication (*) of unsigned u-bit numbers is done
h_a(x) = (a*x)>>(u-v)
The bit-wise shifting is a lot faster than modular. It explains the popularity of this scheme in practice than the original Carter-Wegman construction.
Collision number
Consider a 2-universal family
As in the balls-into-bins with full independence, we are curious about the questions such as the birthday problem or the maximum load. These questions are interesting not only because they are natural to ask in a balls-into-bins setting, but in the context of hashing, they are closely related to the performance of hash functions.
The old techniques for analyzing balls-into-bins rely too much on the independence of the choice of the bin for each ball, therefore can hardly be extended to the setting of 2-universal hash families. However, it turns out several balls-into-bins questions can somehow be answered by analyzing a very natural quantity: the number of collision pairs.
A collision pair for hashing is a pair of elements
The total number of collision pairs among the
Since
The expected number of collision pairs is
Birthday problem
In the context of hash functions, the birthday problem ask for the probability that there is no collision at all. Since collision is something that we want to avoid in the applications of hash functions, we would like to lower bound the probability of zero-collision, i.e. to upper bound the probability that there exists a collision pair.
The above analysis gives us an estimation on the expected number of collision pairs, such that
When
Theorem:
|
Recall that for mutually independent choices of bins, for some
Maximum load
Suppose that a fixed set
For uniform random hash function, this is exactly the maximum load in the balls-into-bins game. And we know that for
For pairwise independent hash functions, neither of previous techniques works any more. Nevertheless, we find that a bound on the maximum load can be directly implied by our analysis of collision number.
Let
By our previous analysis, the expected number of collision pairs is
which implies that
In particular, when
- Alon, Dietzfelbinger, Miltersen, Petrank, and Tardos. Linear hash functions. Journal of the ACM (JACM), 1999.
Perfect hashing
Perfect hashing is a data structure for storing a static dictionary. In a static dictionary, a set
This problem can be solved by binary search on a sorted table or balanced search trees in
The idea of perfect hashing is that we use a hash function
- search for
in table :
- retrieve
from a fixed location in the table; - if
return ; else return NOT_FOUND;
This scheme works as long as that the hash function satisfies the following two conditions:
- The description of
is sufficiently short, so that can be stored in one entry (or in constant many entries) of the table. has no collisions on , i.e. there is no pair of items that are mapped to the same value by .
The first condition is easy to guarantee for 2-universal hash families. As shown by Carter-Wegman construction, a 2-universal hash function can be uniquely represented by two integers
Our discussion is now focused on the second condition. We find that it relies on the perfectness of the hash function for a data set
A hash function
We have shown by the birthday problem for 2-universal hashing that when
for a table of
The construction of perfect hashing is straightforward then:
- For a set
of elements:
- uniformly choose an
from a 2-universal family ; (for Carter-Wegman's construction, it means uniformly choose two integer and for a sufficiently large prime .) - check whether
is perfect for ; - if
is NOT perfect for , start over again; else construct the table;
This is a Las Vegas randomized algorithm, which construct a perfect hashing for a fixed set
Advanced Hash Tables
In the last section we see how to use
This was once a seemingly impossible task, until Yao published a seminal paper in 1981:
- Yao. Should tables be sorted? Journal of the ACM (JACM), 1981.
The paper shows a possibility of achieving linear space and constant time at the same time by exploiting the power of hashing, but assumes an unrealistically large universe.
FKS perfect hashing
Inspired by Yao's work, Fredman, Komlós, and Szemerédi discover the first linear-space and constant-time static dictionary in a realistic setting:
- Fredman, Komlós, and Szemerédi. Storing a sparse table with O(1) worst case access time. Journal of the ACM (JACM), 1984.
The idea of FKS hashing is to arrange hash table in two levels:
- In the first level,
items are hashed to buckets by a 2-universal hash function .
- Let
be the set of items hashed to the th bucket.
- In the second level, construct a
-size perfect hashing for each bucket .
The data structure can be stored in a table. The first few entries are reserved to store the primary hash function
It is easy to see that the search time is constant. To search for an item
- Retrieve
. - Retrieve the address for bucket
. - Search by perfect hashing within bucket
.
Each line takes constant time. So the worst-case search time is constant.
We then need to guarantee that the space is linear to
For a fixed set
Since each bucket
We will show that
Note that a bucket of
Therefore, the sum of squares of the sizes of buckets is related to collision number by:
By our analysis of the collision number in the last section, we know that for
Due to Markov's inequality,
Cuckoo hashing*
Cuckoo hashing also achieves constant search time on a linear space, and after acquiring the hash functions, the accesses to the table can be done in parallel. Cuckoo hashing is considered as a more "modern" hashing technique than FKS by some researchers.
The data structure is defined as follows.
For a set
- A table
of size . - Two hash functions
and are stored separately in fixed places. - For any item
, either or stores .
To search for an item
- Retrieve
and from where they are stored. - If
, returns ; If , returns ; otherwise, return NOT_FOUND.
After retrieving the hash functions, it accesses the table twice, which can be done in parallel.
We only need to guarantee that it is possible to find a placement of the
- each item is placed in one of the two entries it is hashed to;
- no two items are placed into one entry.
This problem can be formulated as a matching problem:
Suppose we have a bipartite graph, where there are
It was shown by Pagh that such a matching exists with constant probability for linear table size
Bloom filters
Suppose that instead of actually finding the item
- "
"
This is called the membership problem, or membership query.
In many applications, the data set can be enormously large, thus the space limit is stringent; on the other hand, the answers need not to be 100% correct, which defines the approximate membership problem. Bloom filter is a space-efficient hash table that solves the approximate membership problem with one-sided error.
Given a set
Assumption:
- We use the Simple Uniform Hash Assumption and assume
are independent uniform random functions from to .
The Bloom filter is constructed as follows:
- Initially, all bits in
are 0s. - For each
, let for all .
To check if an item
When all
With the Simple Uniform Hash Assumption, each individual
After all
For a query
This probability is minimized when
Bloom filter solves the membership query with a small constant error of false positives with linear number of bits (instead of linear number of entries).