cos423: Theory of Algorithms handout #15
Michael Goldwasser
Princeton University Tuesday, March 24, 1998

Homework #5: Randomized Algorithms
Due Date: Tuesday, March 31, 1998 (5:00pm)


Revised Late Policy


For the first half of the course, homeworks were due at 9:00 a.m. on the due date, and late homeworks were accepted up until 5:00 p.m. that same day, with a penalty.

For the second half of the course, we will accept homeworks up until 5:00 p.m. with no penalty (i.e. they are now due at 5:00 p.m.). However, we will not accept homeworks any later than that time, except with previous arrangements.

Collaboration Policy


You may collaborate with others on this entire homework. However, please re-read the policy guidelines from Handout #1.

Reading


Review Ch. 6.2, 6.3, and 6.4 of CLR for a review on probability, if necessary. Read Ch. 8.3, 8.4, 10.2, and 13.4 of CLR.

Practice


Recall, these exercises are purely for your own practice. You need only turn in the official problems.

Problems


1.
Variant of CLR 6-2 (25 points).
The following program determines the maximum value in an unordered array A[1..n].

Find-Max(A,n) 
1		Randomly-Permute(A,n)
2		$max \leftarrow - \infty$ 
3		for $i \leftarrow 1$ to n
4				do $\triangleright$ Compare A[i] to max.
5						if A[i] > max 
6								then $max \leftarrow A[i]$

The call in line 1 re-orders the elements in the array by choosing a uniformly random permutation of the items (see Extra Credit problem). We want to determine the expected number of times the assignment in line 6 is executed. Assume that all array entries are distinct.

2.
(30 points)
In this problem, we give yet another proof that randomized Quicksort runs in $O(n \lg n)$ expected time. The text gives a proof of this fact in Chapter 8.4, by giving a recurrence relation which involves a summation over the possible partitions. In lecture, we say a simpler argument based on labelling partitions as ``good'' or ``bad''. In this problem, we will get a tighter upper bound on the expected number of comparisons which are performed, by looking at the chance that any given two elements are directly compared to each other.


Input: $A = \{a_1, \cdots, a_n\}$. 
RQUICKSORT(A).
		 if |A| = 1, then return A.
		 else
		 		 Choose pivot element uniformly at random from A.
		 		 Compare pivot to all other elements. Let
		 		 		 $A_1 = \{ \mbox{elements smaller than {\em pivot}} \}$		 		 		 $A_2 = \{ \mbox{elements larger than {\em pivot}} \}$.		 		 return $\{ \mbox{\sc RQuicksort}(A_1),
\mbox{\em pivot}, \mbox{\sc RQuicksort}(A_2) \}$.end.

Assume that the sorted order of the elements is given by $\Pi =
\{\pi(1), \pi(2), \ldots, \pi(n) \}$. For each possible pair of unique elements i and j with $1 \leq i < j \leq n$, we define a 0-1 random variable xij as follows:

\begin{displaymath}
x_{ij} = \left\{
\begin{array}
{lll}
1 & \hspace{.2in} & \mb...
 ...her}\\  
0 & \hspace{.3in} & \mbox{otherwise}\end{array}\right.\end{displaymath}

3.
Do CLR 11-3. (45 points)
You do not need to do part (d). You should use the results as a given for other parts. Before starting this problem, make sure you understand the multiple-array representation of objects on p. 209.

Extra Credit


Page 161 described a function Random(a,b), which returns an integer between a and b inclusive, with each such integer being equally likely. If programming in C, this would be similar to getting the value $a+(random()\%(b-a+1))$.