Randomized Algorithms (Spring 2010)/Fingerprinting

From EtoneWiki
Jump to: navigation, search

Checking identities

Many applications in Computer Science require to efficiently check whether two complex objects are identical, while the objects are presented implicitly (e.g., as black-boxes). We consider two examples. One is to check the result of multiplying two matrices, and the other is to check the identity of two polynomials.

Example: Checking matrix multiplication

Consider the following problem:

  • Given as the input three matrices and ,
  • check whether .

We could compute and compare the result to . The time complexity of fastest matrix multiplication algorithm (in theory) is , and so is the time complexity of this method.

Here’s a very simple randomized algorithm, due to Freivalds, that runs in only time:

Algorithm (Freivalds)
  • pick a vector uniformly at random;
  • if then return "yes" else return "no";

The running time of the algorithm is because it does only 3 matrix-vector multiplications.

If then for any , thus the algorithm always returns "yes". But if then the algorithm will make a mistake if it happens to choose an for which . This, however, is unlikely:

If then for a uniformly random ,
Let . We will show that if , then for a uniform random .

Since , it must have at least one non-zero entry, say . The -th entry of is


If to the contrary, , then , which is equivalent to that


i.e. once for have been chosen, there is only one value of that would give us a zero . However, there are two possible values for which are equal-probable, so with at least probability, the choice of fails to give us a zero .

Example: Checking polynomial identities

Consider the following problem:

  • Given as the input two multivariate polynomials and ,
  • check whether the two polynomials are identical, denoted .

Obviously, if are written out explicitly, the question is trivially answered in linear time just by comparing their coefficients. But in practice they are usually given in very compact form (e.g., as determinants of matrices), so that we can evaluate them efficiently, but expanding them out and looking at their coefficients is out of the question.


Consider the polynomial

Show that evaluating at any given point can be done efficiently, but that expanding out to find all its coefficients is computationally infeasible even for moderate values of .

Here is a very simple randomized algorithm, due to Schwartz and Zippel. Testing is equivalent to testing , where .

Algorithm (Schwartz-Zippel)
  • pick independently and uniformly at random from a set ;
  • if then return “yes” else return “no”;

This algorithm requires only the evaluation of at a single point. And if it is always correct.

In the Theorem below, we’ll see that if then the algorithm is incorrect with probability at most , where is the maximum degree of the polynomial .

Theorem (Schwartz-Zippel)
Let be a multivariate polynomial of degree defined over a field . Fix any finite set , and let be chosen independently and uniformly at random from . Then
The theorem holds if is a single-variate polynomial, because a single-variate polynomial of degree has at most roots, i.e. there are at most many choices of having , so the theorem follows immediately.

For multi-variate , we prove by induction on the number of variables .

Write as

where is the largest exponent of in . So by our definition of , and its degree is at most .

Thus by the induction hypothesis we have that .

Conditioning on the event , the single-variate polynomial has degree and , thus


Therefore, due to the law of total probability,

The idea of fingerprinting

Suppose we want to compare two items and . Instead of comparing them directly, we compute random fingerprints and and compare these. The fingerprints has the following properties:

  • is a function, which means that if then .
  • If then is small.
  • It is much more to compute and compare the fingerprints than to compare and directly.

For Freivald's algorithm, the items to compare are two matrices and , and given an matrix , its random fingerprint is computed as for a uniformly random .

For the Schwartz-Zippel algorithm, the items to compare are two polynomials and , and given a polynomial , its random fingerprint is computed as for chosen independently and uniformly at random from some fixed set .

For different problems, we may have different definitions of .

Communication complexity

Alice and Bob are two entities. Alice has a private input and Bob has a private input . Together they want to compute a function by communicating with each other. This is the model of communication complexity introduced by Yao in 1979.

In the communication complexity model, the local computational costs are ignored. The complexity of algorithms (also called communication protocols here) are measured by the number of bits communicated between Alice and Bob.

A basic function is EQ, defined as

This function corresponds to the problem that two far apart entities Alice and Bob, each has a copy of a database (Alice's copy is , and Bob's copy is ), and they want to compare whether their copies of the database are identical.

A trivial way to solve EQ is to let Bob send to Alice. Supposed that , this costs bits of communications.

It is known that for deterministic communication protocols, this is the best we can get for computing EQ.

Theorem (Yao 1979)
Any deterministic communication protocol computing EQ on two -bit strings costs bits of communication in the worst-case.

This theorem is much more nontrivial to prove than it looks, because Alice and Bob are allowed to interact with each other in arbitrary ways. How to prove such lower bounds is not today's topic.

If the randomness is allowed, we can use the idea of fingerprinting to solve this problem with significantly less communications. The general framework for the algorithm is as follows:

  • Alice choose a random fingerprint function and compute the fingerprint of her input ;
  • Alice sends both the description of and the value of to Bob;
  • Bob computes and check whether .

So the question is, how to design this random fingerprint function to guarantee:

  1. A random can be described succinctly.
  2. The range of is small, so the fingerprints are succinct.
  3. If , the probability is small.

The fingerprint function we choose is as follows: by treating the input string as the binary representation of a number, let for some random prime . The prime can uniquely specify a random fingerprint function , thus can be used as a description of the function, and alos the range of the fingerprints is , thus we want the prime to be reasonably small, but still has a good chance to distinguish different and after modulo .

A randomized protocol for EQ

Alice does:

for some parameter (to be specified),
  • choose uniformly at random a prime ;
  • send and to Bob;

Upon receiving and , Bob does:

  • check whether .

The number of bits to be communicated is . We then bound the probability of error for , in terms of .

Suppose without loss of generality . Let . Then since , and for . It holds that if and only if is dividable by . Note that since . We only need to bound the probability

for , where is a random prime chosen from .

The probability is computed directly as


For the numerator, we have the following lemma.

The number of distinct prime divisors of any natural number less than is at most .
Each prime number is . If an has more than distinct prime divisors, then .

Due to this lemma, has at most prime divisors.

We then lower bound the number of primes in . This is given by the celebrated Prime Number Theorem (PNT).

Prime Number Theorem
Let denote the number of primes less than . Then as .

Therefore, by choosing for some , we have that for a , and a random prime ,


We can make this error probability polynomially small and the number of bits to be communicated is still .

Application: Randomized pattern matching

Consider the following problem of pattern matching, which has nothing to do with communication complexity.

  • Input: a string and a "pattern" .
  • Determine whether the pattern is a contiguous substring of . Usually, we are also asked to find the location of the substring.

A naive algorithm trying every possible match runs in time. The more sophisticated KMP algorithm inspired by automaton theory runs in time.

A simple randomized algorithm, due to Karp and Rabin, uses the idea of fingerprinting and also runs in time.

Let denote the substring of of length starting at position .

Algorithm (Karp-Rabin)
pick a random prime ;
for to do
if then report a match;
return "no match";

So the algorithm just compares the and for every , with the same definition of fingerprint function as in the communication protocol for EQ.

By the same analysis, by choosing , the probability of a single false match is


By the union bound, the probability that a false match occurs is .

The algorithm runs in linear time if we assume that we can compute for each in constant time. This outrageous assumption can be made realistic by the following observation.

Let .
It holds that

So the equation holds on the finite field modulo .

Due to this lemma, each fingerprint can be computed in an incremental way, each in constant time. The running time of the algorithm is .

Checking distinctness

Consider the following problem:

  • Given a sequence , check whether every member of appears exactly once.

This problem is called detecting duplicate or checking distinctness. It can be solved in linear time and linear space by a straightforward algorithm.

For many real applications, the is enormously large, and we would like to have an algorithm using very limited extra space.

Fingerprinting multisets

A randomized algorithm due to Lipton, checks distinctness by solving a more general problem fingerprinting multisets.

Given a multiset (each member may appear more than once) , its fingerprint is defined as

where is a random prime chosen from the interval , and is chosen uniformly at random from .

We first see that the space reuqired to compute is only bits. We do not need to compute the value of the product . Instead, all the computation can be done in the finite field (you need some knowledge in algebra to understand this). So the space requirement is only .

It is easy to see that the above fingerprint function is invariant under permutations, thus one multiset has only one fingerprint. The next theorem due to Lipton, states that the probability that two distinct multisets have the same fingerprint is small.

Theorem (Lipton 1989)
Let and be two multisets whose members are from . If , then
Let and , and let .

If , then the polynomials and are not identical, and the polynomial . We only need to show that is small for our choice of random and .

We first show that the probability that is small. Then apply the Schwartz-Zippel technique to show that conditioning on that , the probability is small.

Expand in form of . It can be verified by induction on that for any coefficient , it holds that .

Since , it has at least one nonzero coefficient , and it holds that . Then with the same analysis as in the problem of EQ, by our choice of random ,


Conditioning on that after modulo , since the degree of is at most , has at most roots. Since , therefore is uniformly distributed over a set of size at least . Therefore, the probability that conditioning on that is at most .

By the union bound, .

Checking distinctness by fingerprinting multisets

We now have a fingerprint function for multisets, which is useful for checking the identities of multisets. To solve the problem of checking distinctness, we just check whether the input multiset is identical to the set .