随机算法 (Fall 2011)/Universal hashing
Limited Independence
k-wise independence
Recall the definition of independence between events:
Definition (Independent events) - Events
are mutually independent if, for any subset ,
- Events
Similarly, we can define independence between random variables:
Definition (Independent variables) - Random variables
are mutually independent if, for any subset and any values , where ,
- Random 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) - 1. Events
are k-wise independent if, for any subset with - 2. Random variables
are k-wise independent if, for any subset with and any values , where ,
- 1. Events
A very common case is pairwise independence, i.e. the 2-wise independence.
Definition (pairwise Independent random variables) - Random variables
are pairwise independent if, for any where and any values
- 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 - For any
and any , - For any
that and any ,
- For any
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 - The random variables
are pairwise independent uniform random variables over .
- The random variables
Proof. We first show that are uniform. That is, we will show that for any ,Due to the law of total probability,
For prime
, for any , there is exact one value in of satisfying . Thus, and the above probability is .We then show that
are pairwise independent, i.e. we will show that for any that and any ,The event
is equivalent to thatDue to the Chinese remainder theorem, there exists a unique solution of
and in to the above linear congruential system. Thus the probability of the event is .
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 - For pairwise independent random variables
,
- For pairwise independent random variables
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 - Let
be mutually independent random variables, be k-wise independent random variables, and for every and any . Let be a multivariate polynomial of degree at most . Then
- Let
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 - Let
be mutually independent random variables. Then
- Let
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 - Let
, where are pairwise independent Poisson trials. Let . - Then
- Let
Application: 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:
Algorithm 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
Hashing
Hashing is one of the oldest tools in Computer Science. Knuth's memorandum in 1963 on analysis of hash tables is now considered to be the birth of the area of analysis of algorithms.
- Knuth. Notes on "open" addressing, July 22 1963. Unpublished memorandum.
The idea of hashing is simple: an unknown set
This idea seems clever: we use a consistent mapping to deal with an arbitrary unknown data set. However, there is a fundamental flaw for hashing.
- For sufficiently large universe (
), for any function, there exists a bad data set , such that all items in are mapped to the same entry in the table.
A simple use of pigeonhole principle can prove the above statement.
To overcome this situation, randomization is introduced into hashing. We assume that the hash function is a random mapping from
Simple Uniform Hash Assumption (SUHA or UHA, a.k.a. the random oracle model):
- A uniform random function
is available and the computation of is efficient.
Families of universal hash functions
The assumption of completely random function simplifies the analysis. However, in practice, truly uniform random hash function is extremely expensive to compute and store. Thus, this simple assumption can hardly represent the reality.
There are two approaches for implementing practical hash functions. One is to use ad hoc implementations and wish they may work. The other approach is to construct class of hash functions which are efficient to compute and store but with weaker randomness guarantees, and then analyze the applications of hash functions based on this weaker assumption of randomness.
This route was took by Carter and Wegman in 1977 while they introduced universal families of hash functions.
Definition (universal hash families) - Let
be a universe with . A family of hash functions from to is said to be -universal if, for any items and for a hash function chosen uniformly at random from , we have
- A family of hash functions
from to is said to be strongly -universal if, for any items , any values , and for a hash function chosen uniformly at random from , we have
- Let
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 is strongly 2-universal.
Proof. In Section 1, we have proved the pairwise independence of the sequence of , for , which directly implies that is strongly 2-universal.
- 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) is 2-universal.
Proof. Due to the definition of , there are many different hash functions in , because each hash function in corresponds to a pair of and . We only need to count for any particular pair of that , the number of hash functions that .We first note that for any
, . This is because would imply that , which can never happen since and (note that for an ). Therefore, we can assume that and for .Due to the Chinese remainder theorem, for any
that , for any that , there is exact one solution to satisfying:After modulo
, every has at most many that but . Therefore, for every pair of that , there exist at most pairs of and such that , which means there are at most many hash functions having for . For uniformly chosen from , for any ,We prove that
is 2-universal.
- 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
In particular, for
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 - If
is chosen uniformly from a 2-universal family of hash functions mapping the universe to where , then for any set of items, where , the probability that there exits a collision pair is
- If
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; otherwise, construct the table;
This is a Las Vegas randomized algorithm, which construct a perfect hashing for a fixed set
FKS perfect hashing
In the last section we see how to use
This was once seemingly impossible, until Yao's seminal paper:
- Yao. Should tables be sorted? Journal of the ACM (JACM), 1981.
Yao's 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.
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,