



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS787: Advanced Algorithms Scribe: Shijin Kong and Yiying Zhang Lecturer: Shuchi Chawla Topic: Randomized load balancing and hashing Date: September 19, 2007
Today we give two applications of randomized Algorithms. The first one is load balancing, where the goal is to spread tasks evenly among resources. In order to analyze this problem, we first look at a related problem, the Balls and Bins problem. The second one is hashing. We give a basic randomized algorithm and an improved design.
In the last lecture, we talked about the load balancing problem as a motivation for randomized algorithms. Here we examine this problem more closely. In general, load balancing is a problem to distribute tasks among multiple resources. One example is assigning students to advisors. On any given day, m students will visit n professors. The subset of students visiting professors on any day is not known prior to making the assignment. The goal is to ensure that every advisor gets even load of students. An easy randomized method is to assign each student uniformly at random to any professor.
We can model this as tossing balls into bins. Here, balls can be considered as tasks and bins as resources. We discuss this problem first, since it is a simple way of describing load balancing and is a nice and more comprehensible example to study.
Consider the process of tossing m balls into n bins. The tosses are uniformly at random and independent of each other, which implies that the probability that a ball falls into any given bin is 1 /n. Based on this process, we can ask a variety of questions. Here, we describe several questions of interest and give answers to them.
Before we look at the questions, we define some notation. Let ηpi denote the event of ball i fall into bin p; εij be the event that ball i and ball j collide. Also, note that the collisions mentioned in the questions mean distinct and unordered pairs (e.g. if three balls fall into one bin, three collisions are counted).
Question 1: What is the probability of any two balls falling into one bin?
Alternatively, we can view this question as after the first ball falls into a particular bin p, what is the probability of the second ball falling into bin p. Since balls fall into any bin equally likely and tosses are independent of each other, the probability of the second ball falling into bin p is simply 1 /n. Thus the probability of any two balls falling into one bin is 1/n.
There are two ways to formally prove the correctness of this probability. First, we can use Bayes Rule, which gives
Pr[E 12 ] =
∑^ n
p=
Pr[η 2 p |ηp 1 ]Pr[η 1 p ]
∑^ n
p=
n
Pr[ηp 1 ]
n
We can also sum up over p all the probability of the event that both balls fall into bin p
Pr[E 12 ] =
∑^ n
p=
Pr
ηp 1
ηp 2
∑^ n
p=
n^2
n
Question 2: What is the expected number of collisions when we toss m balls?
To answer this question, we use a indicator random variable Xij to indicate if an event happens. Xij = 1 if Eij happens, Xij = 0 if Eij doesn’t happen. Note that since Xij only takes zero or one as its value, they are Bernoulli variables, and tosses can be considered as a sequence of Bernoulli trials with a probability 1/n of success. We also define X to be the number of collisions, i.e. X =
i 6 =j Xij^. Then we calculate^ E[X] as follow
E[X] =
i 6 =j
E[Xij ]
i 6 =j
Pr[Xij = 1]
n
m 2
The answer to this question demonstrates an interesting phenomenon, Birthday Paradox. How many people must have there be in a room before there is at least 50% chance that two of them were born on the same day of the year? If we look at this problem as a balls and bins problem, people can be viewed as balls and days as bins. A related question is how many balls there need to be in order to get one collision in expectation in 365 bins, i.e. E[X] = 3651
(m 2
= 1. Solving this equation, we get m ≥ 23. The answer is surprisingly few.
Proof: Let k = (^) ln ln3 ln^ n n.
From 7.1.5, we have
Pr[bin i has at least k balls] 6
( (^) e k
e ln ln n 3 ln n
) (^) ln ln3 ln n n
6 exp
3 ln n ln ln n (ln ln ln n − ln ln n)
= exp
−3 ln n + 3 ln n ln ln ln n ln ln n
When n is large enough
Pr[bin i has at least k balls] 6 exp {−2 ln n} =
n^2
Using Union Bound, we have
Pr[any bin has at least k balls] 6 n
n^2
n
which implies
Pr[all bins have at most k balls] > 1 −
n
Note that if we throw balls uniformly at random, balls are not uniformly distributed. But every bin has equal probability of having k balls.
Theorem 7.1.1 shows that if we distribute load or balls independently and uniformly at random, we can get maximum load or largest number of balls in any bin of approximately (^) ln ln3 ln^ n n with high probability. We can also show that
E[maximum load] = ln n ln ln n
Can we improve this number and distribute things more evenly? In the previous process, each time we pick one bin independently and uniformly at random, and then throw a ball. Instead suppose every time we pick two bins independently and uniformly at random, and put a ball into the bin with less balls. This increase the choices into two random choices. Now the expected maximum load is
E[maximum load] = ln ln n ln 2
We see that this is a huge improvement over one random choice. We can also increase to t random choices, where every time we pick t bins independently and uniformly at random, and put a ball into the bin with least balls. We then get an expected max load of ln ln ln t^ n , which doesn’t have much improvement over two random choices. This phenomenon is called “Power of two choices”.
Another load balancing related problem is hashing. It is used to maintain a data structure for a set of elements for fast look-up. Two important applications of hashing are maintaining a dictionary and maintaining a list of easy-breaking passwords. Basically we have elements which are small subsets from a large universe and try to map those elements to an array. This mapping relation is defined as a hash function. The problem is formally described as this:
Given:
A set of elements S with size |S| = m from universe U (|U | = u).
An array with storage for n elements.
Goal:
For n = O(m), design a mapping h from S to array position to achieve O(1) look-up time.
Two simple options to resolve this problem are:
A simple solution for mapping is to use a truly random function. We have total nu^ such functions for mapping: U → [n]. A truly random hash function is picked from this universe uniformly at random. It also implies that any element will be uniformly mapped to any of the n array positions. There can be distinct elements i, j ∈ S for which h(i) = h(j). We say those two elements collide, since they are mapped to same place. A intuitive way to deal with collision is to build a link list under each array position. Then the look-up process for any element i will look like this:
This hashing structure (in Fig 7.2.1) is called chain-hashing. As we know before, the probability for two elements i and j to collide is:
Pr[elements i, j ∈ S collide] =
n
If we fix i, the expected number of collisions for i is:
E[number of collisions with i] =
m − 1 n
There are two key problems with such truly random function h. First, it’s very difficult to simulate such perfect uniformly distributed hash function. Second, the worst case for look-up is not O(1). The worst case look-up time is (^) ln lnln^ n n (from Theorem 7.1.1), which is only slightly better than binary search.
H = { 0 , 1 }lg^ n×lg^ u^ (The set of all lg n × lg u 0-1 matrices)
Then, this hash function is like mapping a long vector into a short vector by multiplying matrix h ∈ H.
For example, for u = 32, n = 8, we can have a h mapping vector x = (1, 1 , 0 , 0 , 1) to i = (0, 1 , 1):
From this matrix formation, we can get two claims on hash functions:
Claim 1:
Fix input x ∈ S, then the probability that any given position in h(x) is 1 is 12.
Proof : Every position in h has 12 probability to be 1. Consider the product of the jth row of h and the vector x, any position of i after multiplication has 12 probability to be 1.
Claim 2:
Fix input x, y ∈ S, (x 6 = y), the probability that the jth position in h(x) is the same as the jth position in h(y) is 12.
Proof : For any position differs in x and y, if the corresponding position in h is 1, then h(x) is different from h(y) at position j. Otherwise, h(x) = h(y) at position j. Therefore the claim.
Theorem 7.2.1 The hash functions H described above is a 2-universal family of hash functions.
Proof: Suppose n = 2t, t ∈ Z. From claim 1 we know that the probability any position in h(x) is 1 is 12. There are totally t positions in h(x). Therefore the probability that h(x) is a particular
value is
2
)t = (^1) n , which satifies the first property of 2-univeral hash functions family (7.2.8).
From claim 2 we know that the probability that any position in h(x) and h(y) are same is 12.
Therefore the probability that h(x) = h(y) is
2
)t = (^) n^1 , which is described as the second property of 2-univeral hash functions family (7.2.9).
Therefore H is 2-universal hash functions family. :
Finally, if mapping integer values: [1,... , u] → [1,... , n] , a widely used hash function is to pick a prime p(p ≥ u), and a, b ∈ [u], a 6 = 0, and ha,b(x) = ((ax + b) mod p) mod n.