Competitive programming sometimes uses randomized algorithms. Their correctness and time and space complexity usually depend on the premise that “the probability of certain random events is very small.” For example, the complexity of quicksort depends on the event that “the chosen pivot is almost the minimum or maximum element” being rare.
This article briefly introduces several tools for analyzing randomized algorithms and gives a few simple applications.
Union Bound
Let be random events. Then
That is, the probability that at least one event in a collection occurs is no greater than the sum of the probabilities of the individual events.
In fact, this conclusion can be strengthened slightly:
- The probability that at least one event in a collection occurs is at least the sum of the probabilities of the individual events, minus the sum of the probabilities that each pair occurs simultaneously.
- The probability that at least one event in a collection occurs is at most the sum of the probabilities of the individual events, minus the sum of the probabilities that each pair occurs simultaneously, plus the sum of the probabilities that each triple occurs simultaneously.
- …
As the number of levels increases, the alternating upper and lower bounds become tighter. This series of conclusions is formally similar to the inclusion–exclusion principle, and its proof is similar as well; we omit it here.
Markov Inequality
Let be a nonnegative random variable. Then for any positive real number ,
In fact, because Markov's inequality itself uses no distributional information about the random variable beyond its expectation, the bound obtained by applying it directly is usually quite loose.
Proof
Let be the indicator function of the event . Then
Consequently,
Chebyshev Inequality
Let be a random variable. Then for any ,
In particular, when is set to ,
where is the standard deviation of .
Proof
By the given condition,
Since is nonnegative, Markov's inequality gives
Chernoff Inequality
The general Chernoff inequality can be obtained by applying Markov's inequality directly to the random variable :
Let be a random variable. Then for any ,
Similarly, when ,
Chernoff Inequality for a Sum of Poisson Trials
The random variables involved in competitive programming are usually not so “general”; we can describe them using Poisson trials from probability theory.
A Poisson trial is a random experiment with only two possible outcomes.
The outcome of one Poisson trial can be represented by a random variable taking the value or , denoted , with probability distribution
For Poisson trials, we have the following result:
For independent Poisson trials , let and . Then for any ,
Hoeffding Inequality
If are mutually independent real random variables with , and , then
Both the Chernoff and Hoeffding inequalities bound how far a random variable can deviate from its expected value. Their proofs are rather lengthy; interested readers can consult the relevant chapters of Probability and Computing.
Empirically, if is not too close to , the bound given by this inequality is often relatively tight. If it is very close—for example, in UOJ #72 New Solution—the bound is often quite loose, and Chernoff's inequality is a better choice.
Example Applications
Example: Estimating Pi by Random Point Sampling
Consider the following algorithm for estimating the exact value of :
In the square region , generate points uniformly at random. Let the number of points that fall inside the unit disk be . Then can be used as an approximation to .
Question: to guarantee that, with probability at least , the algorithm returns a result with relative error at most , how should be chosen?
Solution
Let indicate the event that the -th randomly generated point lies inside the unit disk. Then the total number of points inside the disk is . We need to find a suitable such that
The above is equivalent to
By the Chernoff inequality, it suffices to require
From this we obtain
Thus the desired accuracy can be achieved when .
Example: Lottery Problem
A box contains balls, exactly of which correspond to prizes. You repeatedly draw a ball independently and uniformly at random, returning it to the box after each draw. How many draws guarantee, with probability at least , that every prize ball is drawn at least once?
Solution
If there is only one prize ball, then draws suffice, because the probability that all draws miss it is
Now suppose there are prize balls. By the Union Bound, it suffices to ensure that the probability of missing each prize ball is at most . Therefore the answer is .
Example: Randomly Selecting Half of the Elements
Give an algorithm that selects from elements, uniformly at random, a subset of size , assuming is even. The only random source available is a fair coin; try to minimize the number of coin flips (the minimum is not required).
Solution
First consider the following algorithm:
- By flipping coins, choose one subset uniformly at random from all subsets.
- Repeat this process until the selected subset has size exactly .
- Notice that subsets of size account for at least of all subsets, so the expected number of repetitions is .
This algorithm requires an expected coin flips.
Another algorithm is as follows:
- We can implement a uniformly random choice of one element using an expected coin flips from elements.
- Specifically, generate a binary number with bits uniformly at random. If it is greater than or equal to , generate again; otherwise, select the element with that number (numbered from 0) and finish.
- Then choose one element from all elements, then one from the remaining elements, and so on, until elements have been selected.
This algorithm requires an expected coin flips.
Combine the two algorithms:
- First use the first algorithm to obtain a random subset.
- If the subset has fewer than elements, repeatedly add elements using the second algorithm until its size reaches .
- If the subset has more than elements, repeatedly remove elements using the second algorithm until its size decreases to .
Try to analyze the number of operations required by the second and third steps, that is, the number of additions or removals:
- Let the 0–1 random variable indicate whether is selected into the initial subset, and let be the subset size. Then the number of operations required by the second and third steps is . Taking in Hoeffding's inequality, where is any constant, gives . In other words, by allowing a deviation on the order of , we can obtain an arbitrarily small constant failure probability.
We have now shown that, with high probability, the algorithm uses at most coin flips.
-
Here comes from the coin flips used to obtain the initial subset; is the total cost of the additions or removals.
Expected Complexity
Let us analyze the expected number of coin flips from another perspective.
Use Hoeffding's inequality to bound the expected number of operations in the second and third steps:
Therefore, the expected number of coin flips needed by the second and third steps is .
In total, this algorithm requires an expected coin flips.
Exercise: Balls and Bins
Throw balls independently and uniformly at random into boxes. Prove that, with probability at least , the number of balls in the fullest box is at least .