5. Probabilistic Analysis and Randomized Algorithms
Exercises
5.1-1
We prove the contrapositive, if we don't have total order on the ranks of the candidates, then we cannot always determine which candidate is best, in line 4 of procedure Hire-Assistant.
A lack of total order implies that there are incomparable candidates. Assume one such pair is denoted by indices i<j and currently best=i. When we reach candidate j, we cannot evaluate the condition in line 4. Consequently, we are unable to decide should we keep i or switch to j.
★ 5.1-2 🌟
This exercise introduces the technique called rejection sampling.
Let n=b−a+1 be a size of the interval [a,b] and assume that calling Random(0,1) takes Θ(1) time.
One simple method is to perform a binary search over the interval, using the basic RNG at each step to choose the direction. However, this produces a uniform distribution only when n is a power of two.
The algorithm proceeds as follows:
Compute k=⌈lgn⌉, the number of bits required to represent n−1 (distance of b from a).
Generate a random k-bit number, d, by making k calls to
RANDOM(0, 1). Treat each call as one bit of d, thus d∈[0,2k−1].If d<n, we accept the result and return a+d.
Otherwise, we reject the sample and goto step 2.
Because every number in the range [0,n−1] has the exact same probability of being generated in step 2 (specifically, 1/2k), and we only accept numbers in this range, the final accepted value is guaranteed to be uniformly distributed.
Step 2 requires Θ(k) time. We can view each iteration as a Bernoulli trial, where “success” means that the iteration returns a value. The probability of success at step 3 is 1/2<p=n/2k≤1, so the expected number of iterations follows a geometric distribution and equals 1/p<2. Therefore, the total expected running time is Θ(lg(b−a+1)).
The + 1 is intentionally retained here to ensure the domain of the logarithm is strictly positive and the math remains sound across all valid inputs (including a=b).
★ 5.1-3
Available in the latest revision of the IM.
5.2-1
Available in the latest revision of the IM.
5.2-2
Available in the latest revision of the IM.
5.2-3
Available in the latest revision of the IM.
5.2-4
Available in the latest revision of the IM.
5.2-5
Available in the latest revision of the IM.
5.2-6
Available in the latest revision of the IM.
5.3-1
Available in the latest revision of the IM.
5.3-2
Available in the latest revision of the IM.
5.3-3
Available in the latest revision of the IM.
5.3-4
Available in the latest revision of the IM.
5.3-5 🌟
Describes an algorithm for generating a random sample of size 1≤m≤n from n elements in Θ(m) time.
Available in the latest revision of the IM.
5.4-1
In both subproblems, we assume that birthdays are independent and uniformly distributed.
Subproblem 1
We follow a similar analysis as for the birthday paradox from the book. Let Ai be the event that person i has a different birthday than you do, so Pr{Ai}=1−1/n. Let Bk=⋂i=1kAi be the event that none of the k people has the same birthday as you do. Observe that Pr{Ak∣Bk−1}=Pr{Ak}. We iteratively apply the recurrence (5.8) to obtain
We need Pr{Bk}≤1/2. This gives k=⌈log1−1/n(1/2)⌉. For n=365 it is k=253.
Subproblem 2
Let the random variable Xk denote the number of people (out of k) having a birthday on July 4. This variable follows the binomial distribution, so Pr{Xk=i}=b(i;k,1/n). We are looking for the smallest k such that Pr{Xk=0}+Pr{Xk=1}<1/2. For n=365 this gives k=613.
5.4-2
We need to find k such that Pr{Bk}≤1−0.99=0.01. Solving for k gives
For n=365 it is k=59. Plugging these values into the formula 2nk(k−1), the the expected number of pairs of people with the same birthday is approximately 4.69.
5.4-3
Observe that the first toss will land in an empty bin. Let X be a random variable denoting how many additional tosses are needed until some bin contains two balls. Obviously Pr{X≥1}=1, as we surely need at least one extra toss. Pr{X≥2}=(b−1)/b, since we must have 1 miss. In general, at least k tosses are required, when none of the first k−1 balls ended up in an occupied bin, so
We know that Pr{X=k}=Pr{X≥k}−Pr{X≥k+1} and Pr{X≥b+1}=0. When a random variable X takes on values from the set of natural numbers, then we have a nice formula for its expectation (see equation (C.28) on page 1193 in the book). Therefore, the expected number of tosses is
The expression above is exactly the same as for finding an average number of people to get at least one shared birthday. You can read more about it here, where you can also find an explanation for the asymptotic growth.
★ 5.4-4
It is important that the birthdays be mutually independent for the probability analysis, but for the expected value analysis, pairwise independence is completely sufficient thanks to the linearity of expectation (see Exercise 5.2-4).
The conditional probability chain, obtained by iteratively applying the recurrence (5.8), depends on ensuring that, at each step, the birthday variable bi has the same uniform distribution, unaffected by the previous choices apart from not colliding with earlier birthdays. This chain is only valid when the entire joint assignment is described by uniform, mutually independent random variables. Dependencies among larger groups can profoundly alter probabilities for intersections and unions of events. On page 1188 of the book, you have a concrete example about the difference between pairwise and mutual independence.
★ 5.4-5
OEIS A014088 lists solutions for variations of the birthday problem depending on how many people should share the same birthday. For this exercise, it is k=88. Let us see how to derive it analytically.
We assume that birthdays are mutually independent and uniformly distributed, so any sequence of k birthdays occurs with probability 1/nk. For k>1 and i=0,1,…,⌊k/2⌋, let Qi be an event that among k people exactly i pairs of people have matching birthdays, which implies that no triples of people share birthdays. Let Tk be an event that among k people there are triples sharing birthdays. The probability of this event is
We can select i days out of n, and assign these to i unallocated pairs of people, in (in) ways. The remaining n−i days can be assigned to the remaining k−2i people in (k−2in−i) ways. Since people are distinguishable, the total number of allocations should be multiplied by k!. We also need to divide the resulting number by 2i, since people sharing the same birthday are relatively unordered. After combining these pieces, we get
You can verify that k=88 entails Pr{Tk}≥1/2.
★ 5.4-6
We can form nk k-strings over a set of size n. Any k-string with distinct elements is a k-permutation. By equation (C.1), there are n(n−1)⋯(n−k+1) possible k-permutations. If we denote by Bk the event that k people have distinct birthdays, then the probability that a k-string over a set of size n forms a k-permutation equals Pr{Bk}; on page 141 of the book you have this in expanded form.
★ 5.4-7
Available in the latest revision of the IM.
It is an amazing fact that the expected number of empty bins matches the number of applicants to reject for maximizing the probability of hiring the best candidate in the on-line variant of the hiring problem! This shows how frequently the same limit definition limn→∞(1−n1)n=e1 appears in various contexts.
★ 5.4-8
Let's partition the n coin flips into at least ⌊n/s⌋ groups of s=⌊lgn−2lglgn⌋ consecutive flips and bound the probability that no group comes up all heads. By equation (5.9), the probability that the group starting in position i comes up all heads is
The probability that a streak of heads of length at least s does not begin in position i is therefore at most 1−(lg2n)/n. Since the ⌊n/s⌋ groups are formed from mutually exclusive, independent coin flips, the probability that everyone of these groups fails to be a streak of length s is at most
Using the same reasoning as for deriving equation (5.13) (see page 148 in the book), the probability that the longest streak equals or exceeds s is at least 1−1/n.
Problems
5-1 Probabilistic counting
Available in the latest revision of the IM.
The current edition of the IM contains a false claim about pairwise independence of random variables Xj representing increments in each iteration. They are pairwise dependent, but uncorrelated. The latter fact allows the variance of a random variable Vn=∑jXj to be computed as a sum of variances of Xj.
5-2 Searching an unsorted array
Available in the latest revision of the IM.
A more rigorous treatment of tasks (e) and (f) is based on the law of total expectation. Let Xi=I{A[i] is examined} and X=∑i=1nXi equal the number of indices of A that are examined. Define S={i:A[i]=x} and S={i:A[i]=x}, so that ∣S∣=k and ∣S∣=n−k. A position i∈S is examined only if it’s the first position in S, which occurs with probability 1/k. A position i∈/S is examined only if, out of position i and all k positions in S, i is the first position, which occurs with probability 1/(k+1). Let CS be an event that the indices of elements we are searching for belong in S. We have
Last updated