Randomized Algorithms (Spring 2010)/Balls and bins
Today's materiels are the common senses for randomized algorithms.
Probability Basics
In probability theory class we have learned the basic concepts of events and random variables. The following are some examples:
event : random variable : : 75 out of 100 coin flips are HEADs is the number of HEADs in the 100 coin flips : the result of rolling a 6-sided die is an even number (2, 4, 6) is the result of rolling a die : tomorrow is rainy if tomorrow is rainy and if otherwise
Events and random variables can be related:
- An event can be defined from random variables by a predicate . (: X>Y)
- A boolean random variable can be defined from an event as follows:
We say that indicates the event . We will later see that this indicator trick is very useful for probabilistic analysis.
The formal definitions of events and random variables are related to the foundation of probability theory, which is not the topic of this class. Here we only give an informal description (you may ignore them if you feel confused):
- A probability space is a set of elementary events, also called samples. (6 sides of a die)
- An event is a subset of , i.e. a set of elementary events. (: side 2, 4, 6 of the die)
- A random variable is not really a variable, but a function! The function maps events to values. ( maps the first side of the die to 1, the second side to 2, the third side to 3, ...)
The axiom foundation of probability theory is laid by Kolmogorov, one of the greatest mathematician of the 20th century, who advanced various very different fields of mathematics.
Independent events
Definition (Independent events) - Two events and are independent if and only if
- Two events and are independent if and only if
This definition can be generalized to any number of events:
Definition (Independent events) - Events are mutually independent if and only if, for any subset ,
- Events are mutually independent if and only if, for any subset ,
The independence can also be defined for variables:
Definition (Independent variables) - Two random variables and are independent if and only if
- for all values and . Random variables are mutually independent if and only if, for any subset and any values , where ,
- Two random variables and are independent if and only if
Note that in probability theory, the "mutual independence" is not equivalent with "pair-wise independence", which we will learn in the future.
The union bound
We are familiar with the principle of inclusion-exclusion for finite sets.
Principle of Inclusion-Exclusion - Let be finite sets. Then
- Let be finite sets. Then
The principle can be generalized to probability events.
Principle of Inclusion-Exclusion - Let be events. Then
- Let be events. Then
The principle is implied by the axiomatic formalization of probability. The following inequality is implied (nontrivially) by the principle of inclusion-exclusion:
Theorem (the union bound) - Let be events. Then
- Let be events. Then
The name of this inequality is Boole's inequality. It is usually referred by its nickname "the union bound". The bound holds for arbitrary events, even if they are dependent. Due to this generality, the union bound is extremely useful for analyzing the union of events.
Linearity of expectation
Let be a discrete random variable. The expectation of is defined as follows.
Definition (Expectation) - The expectation of a discrete random variable , denoted by , is given by
- where the summation is over all values in the range of .
- The expectation of a discrete random variable , denoted by , is given by
Perhaps the most useful property of expectation is its linearity.
Theorem (Linearity of Expectations) - For any discrete random variables , and any real constants ,
- For any discrete random variables , and any real constants ,
Proof. By the definition of the expectations, it is easy to verify that (try to prove by yourself): for any discrete random variables and , and any real constant ,
- ;
- .
The theorem follows by induction.
Example - Supposed that we have a biased coin that the probability of HEADs is . Flipping the coin for n times, what is the expectation of number of HEADs?
- It looks straightforward that it must be np, but how can we prove it? Surely we can apply the definition of expectation to compute the expectation with brute force. A more convenient way is by the linearity of expectations: Let indicate whether the -th flip is HEADs. Then , and the total number of HEADs after n flips is . Applying the linearity of expectation, the expected number of HEADs is:
- .
The real power of the linearity of expectations is that it does not require the random variables to be independent, thus can be applied to any set of random variables. For example:
However, do not exaggerate this power!
- For an arbitrary function (not necessarily linear), the equation does not hold generally.
- For variances, the equation does not hold without further assumption of the independence of and .
Law of total probability(expectation)
In probability theory, the word "condition" is a verb. "Conditioning on the event ..." means that it is assumed that the event occurs.
Definition (conditional probability) - The conditional probability that event occurs given that event occurs is
- The conditional probability that event occurs given that event occurs is
The conditional probability is well-defined only if .
For independent events and , it holds that
It supports our intuition that for two independent events, whether one of them occurs will not affect the chance of the other.
The following fact is known as the law of total probability. It computes the probability by averaging over all possible cases.
Theorem (law of total probability) - Let be mutually disjoint events, and is the sample space.
- Then for any event ,
Proof. Since are mutually disjoint and , events are also mutually disjoint, and . Then which according to the definition of conditional probability, is .
Conditional expectation can be accordingly defined:
Definition (conditional expectation) - For random variables and ,
- where the summation is taken over the range of .
- For random variables and ,
There is also a law of total expectation.
Theorem (law of total expectation) - Let and be two random variables. Then
- Let and be two random variables. Then
The law of total probability (expectation) provides us a standard tool for breaking a probability (or an expectation) into sub-cases. Sometimes, it helps the analysis.
Distributions of coin flips
We introduce several important distributions induced by independent coin flips (independent probabilistic experiments), including: Bernoulli trial, geometric distribution, binomial distribution.
- Bernoulli trial (Bernoulli distribution)
- Bernoulli trial describes the probability distribution of a single (biased) coin flip. Suppose that we flip a (biased) coin where the probability of HEADS is . Let be the 0-1 random variable which indicates whether the result is HEADS. We say that follows the Bernoulli distribution with parameter . Formally,
- .
- Geometric distribution
- Suppose we flip the same coin repeatedly until HEADS appears, where each coin flip is independent and follows the Bernoulli distribution with parameter . Let be the random variable denoting the total number of coin flips. Then has the geometric distribution with parameter . Formally, .
- For geometric , . This can be verified by directly computing by the definition of expectations. There is also a smarter way of computing , by using indicators and the linearity of expectations. For , let be the 0-1 random variable such that if and only if none of the first coin flipings are HEADS, thus . A key observation is that . Thus, due to the linearity of expectations,
- Binomial distribution
- Suppose we flip the same (biased) coin for times, where each coin flip is independent and follows the Bernoulli distribution with parameter . Let be the number of HEADS. Then has the binomial distribution with parameters and . Formally, .
- A binomial random variable with parameters and is usually denoted by .
- As we saw above, by applying the linearity of expectations, it is easy to show that for an .
Balls-into-bins model
Imagine that balls are thrown into bins, in such a way that each ball is thrown into a bin which is uniformly and independently chosen from all bins.
There are several information we would like to know about this random process. For example:
- the probability that there is no bin with more than one balls (the birthday problem)
- the number of balls thrown to make sure there is no empty bins (coupon collector problem)
- the distribution of the loads of bins (occupancy problem)
Why is this model useful anyway? It is useful because it is not just about throwing balls into bins, but a uniformly random function mapping to . So we are actually looking at the behaviors of random mappings.
Birthday paradox
There are students in the class. Assume that for each student, his/her birthday is uniformly and independently distributed over the 365 days in a years. We wonder what the probability that no two students share a birthday.
Due to the pigeonhole principle, it is obvious that for , there must be two students with the same birthday. Surprisingly, for any this event occurs with more than 99% probability. This is called the birthday paradox. Despite the name, the birthday paradox is not a real paradox.
We can model this problem as a balls-into-bins problem. different balls (students) are uniformly and independently thrown into 365 bins (days). More generally, let be the number of bins. We ask for the probability of the following event
- : there is no bin with more than one balls (i.e. no two students share birthday).
We first analyze this by counting. There are totally ways of assigning balls to bins. The number of assignments that no two balls share a bin is .
Thus the probability is given by:
Recall that . Then
There is also a more "probabilistic" argument for the above equation. To be rigorous, we need the following theorem, which holds generally and is very useful for computing the AND of many events.
By the definition of conditional probability, . Thus, . This hints us that we can compute the probability of the AND of events by conditional probabilities. Formally, we have the following theorem: Theorem:
- Let be any events. Then
Proof: It holds that . Thus, let and , then
Recursively applying this equation to until there is only left, the theorem is proved.
- Let be any events. Then
Now we are back to the probabilistic analysis of the birthday problem, with a general setting of students and possible birthdays (imagine that we live in a planet where a year has days).
The first student has a birthday (of course!). The probability that the second student has a different birthday is . Given that the first two students have different birthdays, the probability that the third student has a different birthday from the first two is . Continuing this on, assuming that the first students all have different birthdays, the probability that the th student has a different birthday than the first , is given by . So the probability that all students have different birthdays is the product of all these conditional probabilities:
which is the same as what we got by the counting argument.
There are several ways of analyzing this formular. Here is a convenient one: Due to Taylor's expansion, . Then
The quality of this approximation is shown in the Figure.
Therefore, for , the probability that .
Coupon collector's problem
Suppose that a chocolate company releases different types of coupons. Each box of chocolates contains one coupon with a uniformly random type. Once you have collected all types of coupons, you will get a prize. So how many boxes of chocolates you are expected to buy to win the prize?
The coupon collector problem can be described in the balls-into-bins model as follows. We keep throwing balls one-by-one into bins (coupons), such that each ball is thrown into a bin uniformly and independently at random. Each ball corresponds to a box of chocolate, and each bin corresponds to a type of coupon. Thus, the number of boxes bought to collect coupons is just the number of balls thrown until none of the bins is empty.
Theorem - Let be the number of balls thrown uniformly and independently to bins until no bin is empty. Then , where is the th harmonic number.
Proof. Let be the number of balls thrown while there are exactly nonempty bins, then clearly . When there are exactly nonempty bins, throwing a ball, the probability that the number of nonempty bins increases (i.e. the ball is thrown to an empty bin) is
is the number of balls thrown to make the number of nonempty bins increases from to , i.e. the number of balls thrown until a ball is thrown to a current empty bin. Thus, follows the geometric distribution, such that
For a geometric random variable, .
Applying the linearity of expectations,
where is the th Harmonic number, and . Thus, for the coupon collectors problem, the expected number of coupons required to obtain all types of coupons is .
Only knowing the expectation is not good enough. We would like to know how fast the probability decrease as a random variable deviates from its mean value.
Theorem - Let be the number of balls thrown uniformly and independently to bins until no bin is empty. Then for any .
Proof. For any particular bin , the probability that bin is empty after throwing balls is By the union bound, the probability that there exists an empty bin after throwing balls is
Occupancy problem
Now we ask about the loads of bins. Assuming that balls are uniformly and independently assigned to bins, for , let be the load of the th bin, i.e. the number of balls in the th bin.
An easy analysis shows that for every bin , the expected load is equal to the average load .
Because there are totally balls, it is always true that .
Therefore, due to the linearity of expectations,
Because for each ball, the bin to which the ball is assigned is uniformly and independently chosen, the distributions of the loads of bins are identical. Thus is the same for each . Combining with the above equation, it holds that for every , . So the average is indeed the average!
Next we analyze the distribution of the maximum load. We show that when , i.e. balls are uniformly and independently thrown into bins, the maximum load is with high probability.
Theorem - Suppose that balls are thrown independently and uniformly at random into bins. For , let be the random variable denoting the number of balls in the th bin. Then
- Suppose that balls are thrown independently and uniformly at random into bins. For , let be the random variable denoting the number of balls in the th bin. Then
Proof. Let be an integer. Take bin 1. For any particular balls, these balls are all thrown to bin 1 with probability , and there are totally distinct sets of balls. Therefore, applying the union bound, According to Stirling's approximation, , thus
Due to the symmetry. All have the same distribution. Apply the union bound again,
When ,
Therefore,
When , Figure 1 illustrates the results of several random experiments, which show that the distribution of the loads of bins becomes more even as the number of balls grows larger than the number of bins.
Formally, it can be proved that for , with high probability, the maximum load is within , which is asymptotically equal to the average load.
Applications of balls-into-bins
Birthday attack
Suppose that is a cryptographic hash function that maps a large domain of items to a small range, i.e. we have that . As a cryptographic hash function, is deterministic and efficiently computable. The security of the crypto systems built from the hash function are usually based on the following properties of function:
- One-way-ness: Given a hash value , it is hard to generate an that .
- Weak collision resistance: Given an item , it is hard to find an that and . Such is called a collision pair.
- Strong collision resistance: It is hard to generate a collision pair.
It can be seen that the third property is the strongest one. Some digital signature schemes are based on this assumption.
The "birthday attack" is inspired by the birthday paradox. Uniformly and independently pick a number of random items from , with some fair probability, there is a collision pair among these randomly chosen items.
Suppose that we choose items from . We assume that . Since , the probability that we pick the same item twice is (this can be seen as a birthday problem with very small number of students). The following arguments are conditioned on that all chosen items are different.
Therefore, we can view the hash values as bins, and hashing a random to as throwing a ball into a bin. Conditioning on that all chosen items are different, a collision occurs if and only if a bin has more than one balls.
For each , let be the fraction of the domain mapped to hash value . Obviously .
If all are equal that , i.e. the hash function maps the items evenly over its range, then each ball is thrown to a uniform bin. It is exactly as the birthday problem. Due to our analysis of the birthday problem, for some , there is a collision pair with probability .
This argument is conditioned on that all chosen items are different. As we saw the probability that we choose the same item twice is only , by union bound the probability that this occurs or no two balls are thrown to the same bins is . So the probability that a collision pair is found by the birthday attack is .
What if does not distribute items evenly over the range, i.e. 's are different. We can partition each into as many as possible "virtual bins" of probability mass , and sum the remainders to a single "garbage bin". It is easy to see that there are at most virtual bins and the probability mass of the garbage bin is no more than 1/2. Then the problem is turned to throwing each of the balls first to the garbage bin with probability at most 1/2, and if survived, to a uniformly random virtual bin.
Stable marriage
Suppose that there are men and women. Every man has a preference list of women, which can be represented as a permutation of . Similarly, every women has a preference list of men, which is also a permutation of . A marriage is a 1-1 correspondence between men and women. The stable marriage problem or stable matching problem (SMP) is to find a marriage which is stable in the following sense:
- There is no such a man and a woman who are not married to each other but prefer each other to their current partners.
The famous proposal algorithm (求婚算法) solves this problem by finding a stable marriage. The algorithm is described as follows:
- Each round (called a proposal)
- An unmarried man proposes to the most desirable woman according to his preference list who has not already rejected him.
- Upon receiving his proposal, the woman accepts the proposal if:
- she's not married; or
- her current partner is less desirable than the proposing man according to her preference list. (Her current partner then becomes available again.)
The algorithm terminates when the last available woman receives a proposal. The algorithm returns a marriage, because it is easy to see that:
- once a woman is proposed to, she gets married and stays as married (and will only switch to more desirable men.)
It can be seen that this algorithm always finds a stable marriage:
- If to the contrary, there is a man and a woman prefer each other than their current partners ('s wife) and ('s husband), then must have proposed to before he proposed to , by which time must either be available or be with a worse man (because her current partner is worse than ), which means must have accepted 's proposal.
Our interest is the average-case performance of this algorithm, which is measured by the expected number of proposals, assuming that each man/woman has a uniformly random permutation as his/her preference list.
Apply the principle of deferred decisions, each man can be seen as that at each time, sampling a uniformly random woman from the ones who have not already rejected him, and proposing to her. This can only be more efficient than sampling a uniformly and independently random woman to propose. All men are proposing to uniformly and independently random woman, thus it can be seen as proposals (regardless which men they are from) are sent to women uniformly and independently at random. The algorithm ends when all women have received a proposal. Due to our analysis of the coupon collector problem, the expected number of proposals is .
Chain hashing and load balancing
Consider the following two scenarios:
- A hash table which resolves collisions by chaining, such that all keys mapped to the same slots are arranged by a linked list pointed from the slot.
- Distributed a number of jobs evenly among a set of machines by a random hash function.
With UHA, both scenarios can be viewed as a balls-into-bins game, and the loads of bins give the lengths of chains or the loads of machines. The maximum load of bins is critical, because it gives the worst-case searching time for the hash table and the finishing time for the machines.
By our assignment of the occupancy problem, when , the maximum load is with high probability.