随机算法 (Spring 2014)/Random Recurrence

From EtoneWiki
Jump to: navigation, search

Random Quicksort

Given as input a set of numbers, we want to sort the numbers in in increasing order. One of the most famous algorithm for this problem is the Quicksort algorithm.

  • if do:
    • pick an as the pivot;
    • partition into , , and , where all numbers in are smaller than and all numbers in are larger than ;
    • recursively sort and ;

The time complexity of this sorting algorithm is measured by the number of comparisons.

For the deterministic quicksort algorithm, the pivot is picked from a fixed position (e.g. the first number in the array). The worst-case time complexity in terms of number of comparisons is .

We consider the following randomized version of the quicksort.

  • if do:
    • uniformly pick a random as the pivot;
    • partition into , , and , where all numbers in are smaller than and all numbers in are larger than ;
    • recursively sort and ;

Analysis of Random Quicksort

Our goal is to analyze the expected number of comparisons during an execution of RandQSort with an arbitrary input . We achieve this by measuring the chance that each pair of elements are compared, and summing all of them up due to Linearity of Expectation.

Let denote the th smallest element in . Let be the random variable which indicates whether and are compared during the execution of RandQSort. That is:

Elements and are compared only if one of them is chosen as pivot. After comparison they are separated (thus are never compared again). So we have the following observations:

Observation 1: Every pair of and are compared at most once.

Therefore the sum of for all pair gives the total number of comparisons. The expected number of comparisons is . Due to Linearity of Expectation, . Our next step is to analyze for each .

By the definition of expectation and ,

We are going to bound this probability.

Observation 2: and are compared if and only if one of them is chosen as pivot when they are still in the same subset.

This is easy to verify: just check the algorithm. The next one is a bit complicated.

Observation 3: If and are still in the same subset then all are in the same subset.

We can verify this by induction. Initially, itself has the property described above; and partitioning any with the property into and will preserve the property for both and . Therefore Claim 3 holds.

Combining Observation 2 and 3, we have:

Observation 4: and are compared only if one of is chosen from .

And,

Observation 5: Every one of is chosen equal-probably.

This is because the Random Quicksort chooses the pivot uniformly at random.

Observation 4 and 5 together imply:

Remark: Perhaps you feel confused about the above argument. You may ask: "The algorithm chooses pivots for many times during the execution. Why in the above argument, it looks like the pivot is chosen only once?" Good question! Let's see what really happens by looking closely.

For any pair and , initially are all in the same set (obviously!). During the execution of the algorithm, the set which containing are shrinking (due to the pivoting), until one of is chosen, and the set is partitioned into different subsets. We ask for the probability that the chosen one is among . So we really care about "the last" pivoting before is split.

Formally, let be the random variable denoting the pivot element. We know that for each , with the same probability, and with an unknown probability (remember that there might be other elements in the same subset with ). The probability we are looking for is actually , which is always , provided that is uniform over .

The conditional probability rules out the irrelevant events in a probabilistic argument.

Summing all up:

is the th Harmonic number. It holds that

.

Therefore, for an arbitrary input of numbers, the expected number of comparisons taken by RandQSort to sort is .

Random Recurrence

We consider the selection problem: given as input a set of distinct numbers and an integer , find the -th smallest number in .

We know that this problem can be solved deterministically by the median-of-median algorithm or randomly by the LazySelect algorithm, both in linear time. In the same spirit of random QuickSort, we propose the following random QuickSelection algorithm, called the RandomQS.

RandomQS(,)
if return the only element in ;
pick a uniform random pivot ;
construct and ;
if : return RandomQS;
if : return RandomQS;

It can be verified as an exercise problem that the expected number of comparisons of RandomQS is . Alternatively, we ask the following question:

  • How many recursive calls to RandomQS are made in expectation in a running of the algorithm?

Let denote the number of recursive calls in a running of RandomQS. Obviously is a random variable and it satisfies the following recurrence:

where is a random variable representing the number of elements in the input which are thrown away in the current call. Specifically, when , and when . We are looking for an upper bound on the expectation .

The token game

The above recursion can be seen as the following token game:

  • Initially we have tokens.
  • In each round, we independently draw a random number which depends on the current number of tokens and throw away tokens.
  • The game terminates when there is no token left.

The number of rounds of the game is given by the random variable .

Needless to say, this procedure has some generality. It is actually pretty common in randomized algorithms. The followings are some examples:

Survival game for coins (with an application in Skip List)
Suppose we start with biased coins, each with probability being a HEAD. In each round, we independently flip every coins and remove those coins whose outcome is TAIL. We play the game until no coin left. The number of rounds is given by , satisfying the recursion
where gives the number of TAILs, which follows the binomial distribution .
This quantity has an application in the analysis of the Skip List, a randomized data structure. A Skip List is a multi-level linked list, in which we start with a standard linked list of nodes, and each node in the list is lifted to the upper level independently with a fixed probability . This lifting process is recursively applied until there is no node left. The number of levels of a Skip List is given by .
Skiplist.png
Coupon collector
Suppose a coupon collector collecting totally coupons by uniform independent trials. For each , let be the number of trials to take when there are coupons remaining to collect. satisfies the recursion

where is the number of trials taken when there are exactly coupons remaining to collect, which we know satisfy that

Geometric distribution
Any geometric random variable can be described as a defined as follows
and
where is a Bernoulli random variable.

Karp-Upfal-Wigderson bound

The following theorem was proved by Karp, Upfa, and Wigderson, and later was improved by Karp. Today it is known as the Karp-Upfal-Wigderson bound.

Theorem (Karp-Upfal-Wigderson 1988)
Consider the following recurrence
where is a constant and each is an independent drawn of a nonnegative integral random variable such that .
If there exists a positive-valued non-degreasing function satisfying that for all , then

The theorem gives an upper bound on the expected number of rounds of the token game if we can nicely lower bound the expectation of each . The theorem itself is quite intuitive: for each , gives the rate at which the number of tokens drops from . At this rate it takes rounds to cross from to tokens. From the point of view of an interval , we do not know which we are using, but for any the average rate is lower bounded by . Taking an integration, it is not surprising that it takes at most rounds on average to consume all but tokens. The only issue in this informal argument is that we do not have , so this is not a rigorous proof. But with bit care we can still manage to prove the theorem rigorously.

Proof.

We prove the theorem by applying an induction on . The bound is trivially true when . For , assume the induction hypothesis for all smaller . Denote that . Due to the recursion , we have

Denote that . We have because is nonnegative. Thus the above equation becomes

which gives us that

.

For the conditional expectation, we have

On the other hand, due to the total expectation, we have

which gives us that . Substituting this in the above inequality gives us that

Applications of Karp-Upfal-Wigderson bound

Random QuickSelect

As shown above, the number of recursive calls in a running of RandomQS satisfies the recurrence:

where is a random variable representing the number of elements in the input which are thrown away in the current call.

Recall that is the number of elements in the current input no larger than the pivot and is uniformly chosen from at random. It is easy to see that no matter for what it holds that where is uniformly random over . An easy calculation gives us that . By the Karp-Upfal-Wigderson bound, we have

Skip List

As discussed above, the number of levels of a Skip List for elements is given by satisfying that

where is the number of TAILs when independently flip biased coins, where each coin turns HEAD with probability . Clearly follows the binomial distribution , and its expectation is .

By the Karp-Upfal-Wigderson bound, we have

Here the will cause a problem. We can fix the problem by observing that for integral , the expectation is actually

Thus the real Karp-Upfal-Wigderson bound for this process is

where is the harmonic number.

Coupon collector

We have represented the number of trials in a coupon collector game with coupons as defined by the following recursion

where is the number of trials taken when there are exactly coupons remaining to collect, which we know satisfy that

Thus . By the Karp-Upfal-Wigderson bound, we have

Geometric distribution

We know that a geometric random variable can be described as a defined as follows

and

where is a Bernoulli random variable being 1 with probability . Then . By the Karp-Upfal-Wigderson bound, we have