

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of randomized algorithms and presents a simple randomized algorithm, randomize-in-place, for generating a random permutation of an array. The document also includes the proof of correctness of the algorithm and the running time analysis. From a lecture in the computer science department at the university of texas at austin, in the course cs357: algorithms, during the spring 2007 academic year.
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


The University of Texas at Austin Lecture 5 Department of Computer Sciences Professor Vijaya Ramachandran Randomized algorithms; random permutation CS357: ALGORITHMS, Spring 2007
Definition: A randomized algorithm is an algorithm that can make calls to a random number generator during the execution of the algorithm.
These calls will be of the form x := Random(a, b), where a, b are integers, a ≤ b.
A call to Random(a, b) returns an integer in [a, b], with every integer in the range being equally likely to occur.
Successive calls to Random(∗, ∗) are assumed to be mutually independent.
Definition: The expected running time of a randomized algorithm on a given input is the average running time of the algorithm over all outcomes of calls to the random number generator.
The expected running time of a randomized algorithm for inputs of length n is the maximum expected running time of the algorithm over all inputs of length n.
A randomized algorithm terminates in time t(n) with probability at least p if, for every input of length n, the probability that the algorithm terminates in time t(n) is at least p.
The need to generate a random permutation arises in many situations, so let us look at a simple randomized algorithm to generate a random permutation.
Randomize-in-place(A)
Input. An array A[1..n] of n elements. Output. A rearrangement of the elements of array A, with every permutation of the n elements equally likely to occur.
for i := 1 to n do swap A[i] and A[Random(i, n)] rof
In order to prove correctness of this algorithm we need the following definition:
Definition. A k-permutation of an n-element set S is a sequence of k elements from the set S.
How many k-permutations of an n-element set are there?
We claim there are (^) (n−n!k)! of them. (Why?)
Proof of correctness of Randomize-in-place.
We use the following loop invariant:
Initialization. For i = 1 the statement refers to the empty subarray and the 0-permutation, and is trivially true.
Maintenance. Assume the loop invariant holds at the start of iteration i. We will now show that it must hold at the start of the (i + 1)th iteration. To see this, consider any i-permutation X =< x 1 , x 2 , · · · , xi > of the input elements.
Let E 1 be the event that the first i − 1 iterations of the for loop placed the sequence < x 1 , x 2 , · · · , xi− 1 > in subarray A[1..(i − 1)].
Let E 2 be the event that the ith iteration of the for loop placed xi in A[i].
Page 2