The concept of chance has been a subject of philosophical inquiry and scientific investigation for centuries. While classical physics often favored a deterministic view, the 20th century, particularly with quantum mechanics and evolutionary biology, embraced randomness as an inherent aspect of nature. In computer science, too, randomness has emerged not as a sign of incomplete knowledge, but as a powerful tool for designing algorithms that are often simpler, more efficient, and more robust than their deterministic counterparts. Unlike deterministic algorithms that follow a fixed path, randomized algorithms can “explore” possibilities, often finding solutions much faster or handling adversarial inputs more effectively.

In previous chapters, we’ve seen the limitations of deterministic algorithms, especially when facing NP-hard problems or adversarial inputs. Randomized algorithms introduce an element of chance into their logic, allowing them to make choices based on random bits. This unpredictability can help algorithms escape worst-case scenarios, achieve significant speedups, or solve problems that are otherwise intractable.

This chapter explores the fundamental ideas behind randomized algorithms. We will start with a brief review of elementary probability theory, then demonstrate the power of randomization through practical examples: an efficient communication protocol for comparing large databases, a randomized primality test, and a method for checking polynomial equivalence. These examples will illustrate key paradigms like the method of frequent witnesses and the method of fingerprints, showcasing how randomness can lead to surprisingly effective solutions.

8.1 Aims

By the end of this chapter, you will be able to:

  • Understand Elementary Probability: Grasp basic concepts of probability spaces, distributions, and independent events.
  • Model Randomized Algorithms: Understand how randomized algorithms are formally defined and analyzed.
  • Appreciate Efficiency Gains: See how randomization can lead to algorithms that are exponentially more efficient than their deterministic counterparts for certain tasks.
  • Apply the Method of Frequent Witnesses: Learn this paradigm for designing randomized algorithms, exemplified by the Solovay-Strassen primality test.
  • Apply the Method of Fingerprints: Understand this technique for solving equivalence problems, demonstrated through polynomial equivalence testing.
  • Analyze Error Probability: Learn how to analyze and reduce the error probability of randomized algorithms through repetition.

8.2 Elementary Probability Theory

To understand randomized algorithms, a basic grasp of probability is essential.

Definition 8.1 (Probability Space)

A probability space is a pair , where:

  1. is a finite set of elementary events (all possible outcomes of an experiment). (Think of these as the most basic, indivisible results, like “rolling a 3” on a die.)
  2. is a probability distribution (or measure) that assigns a probability to each event (subset of ), satisfying:
    • for every elementary event .
    • . (This means that one of the possible outcomes must occur.)
    • for any two disjoint events .

The probability of an event is the sum of the probabilities of its elementary events: . A uniform probability distribution assigns equal probability to all elementary events: for all .

Exercise 8.1 (Properties of Probability)

Prove the following properties for any probability space :

  1. .
  2. for any .
  3. If , then .
  4. .

Modeling Randomized Algorithms

A randomized algorithm can be viewed in two ways: (These perspectives are equivalent and offer different insights into how randomness is incorporated.)

  1. As a nondeterministic algorithm where each nondeterministic choice is made with a certain probability. The probability of a computation path is the product of the probabilities of its individual random decisions.
  2. As a probability distribution over a set of deterministic algorithms. This is often implemented by providing a deterministic algorithm with a sequence of random bits as an additional input. Each sequence of random bits effectively selects a specific deterministic execution path.

The error probability of a randomized algorithm is typically defined as the maximum probability of producing an incorrect output over all possible inputs of a given size. (This is a crucial metric, as it quantifies the reliability of the algorithm’s probabilistic choices.)


8.3 A Randomized Communication Protocol

Consider the task of comparing the contents of two large databases, and , stored on two different computers, RI and RII. Both databases contain bits. A deterministic protocol would require exchanging bits in the worst case to guarantee correctness. For bits, this is infeasible.

Here’s a randomized protocol that achieves exponential efficiency.

Analysis of Protocol R

  • Communication Complexity: RI sends and . Since , the length of the message is bits. For , this is about bits, a tiny message. (This logarithmic communication is a massive improvement over the bits required by deterministic protocols, especially for very large .)
  • Error Probability:
    • If : Then , so for any . RII always outputs “equal”. Error probability is 0.
    • If : RII outputs “equal” (a wrong answer) only if , which means . This implies divides . (This is the only way for a mismatch to go undetected: if the difference between the numbers happens to be a multiple of the chosen prime .) Let . Since , . Also, . A number has at most distinct prime factors. So has at most prime factors. The probability that a randomly chosen prime divides is at most . For , this error is extremely small, around .

Error Reduction by Repetition

The error probability can be further reduced by repeating the protocol multiple times with independent random choices. If we repeat the protocol times, and all choices of lead to , the error probability drops to . For , the error for becomes less than .

This demonstrates a key advantage of randomized algorithms: they can achieve extremely low error probabilities with minimal communication or computation, often exponentially better than deterministic approaches.


8.4 The Method of Frequent Witnesses and the Randomized Primality Test

The efficiency of Protocol R stems from the method of frequent witnesses.

  • A witness for a property is additional information that allows efficient deterministic verification of that property. (Think of it as a piece of evidence that quickly confirms a claim.)
  • For a randomized algorithm, we need a set of witness candidates such that, for any input, a sufficiently large fraction of these candidates are actual witnesses. (This “frequent witnesses” property is what makes random sampling effective: you’re likely to pick a witness by chance.)

The Primality Test

Determining the primality of a large number is a fundamental problem in cryptography. The naive deterministic algorithm (trial division up to ) is too slow.

Fermat’s Little Theorem provides a basis for a randomized test:

Theorem 8.1 (Fermat's Little Theorem)

For every prime number and every integer such that , it holds that .

If is composite, it is possible that for some . Such are called Carmichael numbers. (These numbers are “Fermat pseudoprimes” and can fool the basic Fermat test, making it unreliable for proving primality.) A stronger test is needed.

For an odd number with odd (i.e., ):

Theorem 8.2

  1. If is prime, then for all .
  2. If is composite, then for at least half of the numbers .

This theorem defines a “witness” for compositeness: an such that .

Error Analysis of Solovay-Strassen

  • If is prime: The algorithm always outputs “prime number”. Error probability is 0.
  • If is composite: The probability that a randomly chosen is not a witness (i.e., ) is at most . So the error probability is at most .

By repeating the test times with independent choices of , the error probability for composite drops to . For , the error is less than . This makes the test highly reliable for practical purposes.


8.5 The Method of Fingerprints and the Equivalence of Two Polynomials

The method of fingerprints is a special application of the frequent witnesses paradigm, particularly useful for solving equivalence problems between large objects. (Think of a fingerprint as a much smaller, unique-enough identifier for a large object, allowing for quick comparisons without needing to compare the entire objects.)

The key idea is that fingerprints are significantly shorter representations of , making their comparison much faster. The risk of error arises if but (a “collision”). The set must be chosen such that collisions are rare for non-equivalent objects. (This rarity is crucial; if collisions were common, the fingerprint wouldn’t be a reliable indicator of equivalence.)

Equivalence of Two Polynomials

Consider two polynomials and over a finite field . We want to check if , meaning they evaluate to the same value for all inputs . Comparing coefficients directly requires converting to normal form, which can be exponentially slow.

Error Analysis of AQP

  • If : The algorithm always outputs “equivalent”. Error probability is 0.
  • If : Let . Since , is not identically zero. The algorithm makes an error if for the randomly chosen . A key result (Theorem 8.4) states that for a non-zero polynomial of variables and degree at most , the number of roots in is at most . Thus, the probability of choosing a root (and making an error) is at most . If we choose , this error probability is less than . Again, repeating the test times reduces the error to .

This method provides an efficient randomized solution for a problem for which no polynomial-time deterministic algorithm is known.


Summary

  • Randomized algorithms leverage chance to achieve efficiency, simplicity, and robustness, often outperforming deterministic algorithms.
  • They are modeled either as nondeterministic algorithms with probabilistic choices or as deterministic algorithms with random bit sequences as input.
  • The Stochastic Communication Protocol R for database comparison demonstrates exponential efficiency gains over deterministic protocols, achieving extremely low error probabilities with minimal communication.
  • The Method of Frequent Witnesses is a paradigm where an algorithm relies on finding a “witness” that efficiently verifies a property. If witnesses are frequent among candidates, random sampling is effective.
  • The Solovay-Strassen Primality Test is a randomized algorithm based on frequent witnesses, efficiently determining if a number is prime with a controllable error probability.
  • The Method of Fingerprints is a specialized technique for equivalence problems, where large objects are mapped to small “fingerprints” via random functions. Comparing fingerprints is efficient, with a small, controllable risk of collision.
  • The Algorithm AQP for polynomial equivalence testing uses fingerprints to efficiently determine if two polynomials are equivalent, a problem for which deterministic polynomial-time algorithms are not known.
  • Error amplification through repetition is a common technique to reduce the error probability of randomized algorithms to arbitrarily low levels.

Randomization is a powerful tool in algorithm design, offering practical solutions to problems that are otherwise intractable.


Previous Chapter: Chapter 7 - Algorithmics for Hard Problems Next Up: Chapter 9 - Communication and Cryptography

Exercises

Exercise 8.1 (Coin Tossing)

Model the experiment of five-fold coin tossing. What is the probability that the number of heads and the number of tails differ by at most 1?

Exercise 8.2 (Database Comparison)

Two computers have each stored a word of 18 bits. They use Protocol R to check for equality.

  1. From how many prime numbers is one chosen randomly?

  2. How many bits are communicated?

  3. What is the error probability if the words are different?

Exercise 8.3 (Solovay-Strassen Error Reduction)

How far can one reduce the error probability of the Solovay-Strassen algorithm if, instead of one attempt, independent attempts are made?

Exercise 8.4 (Polynomial Equivalence)

Are and equivalent over ? Describe how Algorithm AQP would test this.