## CS174 Lecture 2 Summary

### Monte-Carlo vs. Las Vegas

A random algorithm is Las Vegas if it always produces the correct answer. The running time depends on the random choices made in the algorithm. Random Quicksort is a Las Vegas algorithm. It always sorts the elements, but its running time depends on the choices of pivot.

A random algorithm is Monte Carlo if it can give the wrong answer sometimes. The running time usually doesn't depend on the random choices, but for some Monte Carlo algorithms it does.

A useful mnemonic that someone proposed is "Las Vegas will never cheat you". Why they thought that Las Vegas is more trustworthy than Monte Carlo is a mystery.

From the definitions, you can see that a Las Vegas algorithm is also a Monte Carlo algorithm (Monte Carlo is a broader definition because nothing is guaranteed). To turn a Monte-Carlo algorithm into a Las Vegas algorithm, you need to add a means for checking if the answer is correct and retrying if it isn't.

### Random Permutations

Recall that a permutation p is a reordering of elements 1,…,n. It can be specified by the new values. e.g. (2 4 3 5 1) specifies a permutation where p (1)=2, p (2)=4, p (3)=3, p (4)=5 and p (5)=1.

There are n! permutations of n elements. To generate permutations with a uniform random distribution, we can use the following method. Assume an array A with n such that A[i]=i.

```RandomPerm(A, n)

for i = 1 to n-1

choose j uniformly at random from [i,...,n]

swap A[i] and A[j]```

Claim: This algorithm can produce any permutation of {1,...,n}.

Proof: We first show a relationship between this algorithm and sorting (selection sort actually).

Now suppose the array A is initialized with elements that are the representation of a permutation, that is  A[i]= p(i). If we sort the array A, let the final contents of array be A'. The contents of A' are just 1,...,n sorted so A'[i] = i. The permutation we have just made on the elements of A is exactly p, because we have moved the element in position i to p(i). To put it another way, A[i] = A'[p(i)].

Now observe that whatever the initial order of the elements in the array A, the random permutation algorithm might sort them in increasing order. It will do this if every random choice of j happens to pick the smallest element in the sequence A[i]...A[n]. In that case, the permutation algorithm is actually implementing selection sort. The permutation algorithm doesnt look at the contents of A when it runs. The contents of A might be the permutation p. If it is, the random permutation algorithm might sort the array in increasing order. As we saw above, this corresponds to applying the permutation p to the elements of A. In summary, for any permutation , the random permutation algorithm has non-zero probability of applying the permutation p to the elements of A.

There is exactly one set of choices of the random j values that will produce any given permutation. To see that, notice first that there are

n x (n-1) x (n-2)... = n!

possible combinations of random j values.

Since there are n! permutations of n elements, and the random permutation scheme can do n! distinct permutations (based on the j choices), there is a 1 to 1 correspondence between the algorithms permutations and all the permutations of 1...n.

And the probability that that set of choices is made is:

1/n * 1/(n-1) * 1/(n-2) ... = 1/n!

So each permutation has probability 1/n! of coming out of this procedure. That's the uniform distribution on permutations.

### Fixed Points in a Permutation

Here's the first application of "the probabilistic method" we will use often in the course. We first define a random variable X which counts the quantity we are interested in:

X = number of fixed points in the permutation p , which is the cardinality of the set  {i | p(i) = i}

Since p is a random permutation, we are interested in the expected value of X. We could try to compute E[X] directly, but there is a simpler way. Define

Xi = 1 if p(i) = i, 0 otherwise

Then notice that

X  = åi=1..n Xi

and using linearity of expected value, we have that

E[X]  = åi=1..n E[Xi]

there is obvious symmetry between permutations that fix a particular element so all the E[Xi] are equal. Therefore

E[X]  = nE[Xi]

and computing E[Xi] is easy because its an indicator (0-1-valued) random variable.

E[Xi] = 0.Pr[Xi=0] + 1.Pr[Xi=1]

= Pr[Xi=1]

which is just the probability that p(i)=i. This is easy to figure by counting permutations that do fix i and dividing by all permutations:

Pr[Xi=1] = (n-1)!/n! = 1/n

so

E[X]  = nE[Xi] = n * 1/n = 1

And we get the rather surprising fact that the expected number of fixed points in a random permutation is 1, independent of how many elements are being permuted.

### Counting cycles in a random permutation

A harder and more interesting question is the following: What is the expected number of cycles for a random permutation?

As before, we use a random variable:

Y = number of cycles in the permutation p

Defining indicator random variables is trickier this time. We can't count 1 for each point in each cycle, or we will just get a total of n. What we can do is divide the 1 for a cycle among the k elements in the cycle. That is, we define:

Yi = 1/k where k is the length of the cycle containing i

Check for yourself that the Yi are not independent.

Now verify that

Y  = åi=1..n Yi

and using linearity of expected value, we have that

E[Y]  = åi=1..n E[Yi]

all the E[Yi] are equal. Therefore

E[Y]  = nE[Yi]

and

E[Yi] = åj=1..n 1/j Pr[i is in a cycle of length j]

Pr[i is in a cycle of length 1] = 1/n

Pr[i is in a cycle of length 2] = (n-1)/n * 1/(n-1) = 1/n

Pr[i is in a cycle of length 1] = (n-1)/n * (n-2)/(n-1) * 1/(n-2) = 1/n

:

Pr[i is in a cycle of length j] = 1/n   for any j

This is a surprising result: the length of the cycle containing the point has the uniform distribution! i.e. any point is equally likely to be in a cycle of any length up to n. Substituting for this probability, we get:

E[Yi] = åj=1..n 1/j 1/n

E[Yi] = 1/n Hn

where Hn = (1 + 1/2 + 1/3 + ... + 1/n) is the nth harmonic number. So

E[Y] = Hn

Now Hn is approximately the natural log of n, ln n. So the expected number of cycles in a permutation grows as the natural log of the number of elements.