Randomized Algorithms: Generating a Random Permutation in Computer Science, Study notes of Health sciences

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

Pre 2010

Uploaded on 08/31/2009

koofers-user-yim
koofers-user-yim 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The University of Texas at Austin Lecture 5
Department of Computer Sciences
Professor Vijaya Ramachandran Randomized algorithms; random permutation
CS357: ALGORITHMS, Spring 2007
Randomized algorithms
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, ab.
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 nis 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 pif, for every input
of length n, the probability that the algorithm terminates in time t(n) is at least p.
pf3

Partial preview of the text

Download Randomized Algorithms: Generating a Random Permutation in Computer Science and more Study notes Health sciences in PDF only on Docsity!

The University of Texas at Austin Lecture 5 Department of Computer Sciences Professor Vijaya Ramachandran Randomized algorithms; random permutation CS357: ALGORITHMS, Spring 2007

Randomized algorithms

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.

Generating a Random Permutation

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:

  • At the start of the ith iteration of the for loop, the subarray A[1..i − 1] contains each (i − 1)-permutation of the elements of the array A with probability (n− ni+1)!!.

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