You are on page 1of 2

CSOR W4231.

002 Spring, 2014

Homework 2
Out: Thursday, February 13, 2014
Due: 6pm, Thursday, February 27, 2014
No homework assignments will be accepted past 6pm on the due date. Try to keep your answers
clear and concise. Collaboration is limited to discussion of ideas only. You should write up the
solutions entirely on your own. You should list your collaborators on your write-up. Be sure that
your hand-writing is legible and that your name is clearly written on your homework if you do not
type your solutions.

1. Suppose that a sequence of items passes by one at a time. We want to maintain a sample of
one item with the property that it is uniformly distributed over all the items that we have
seen so far. Moreover we do not know the total number of items in advance and we cannot
store more than one item at any time.
(a) Consider the following algorithm. When the first item appears, we store it. When the
k-th item appears, we replace the stored item with probability 1/k. Show that this
algorithm solves the problem.
(b) Now suppose that when the k-th item appears, we replace the stored item with probability 1/2. What is the distribution of the stored item in this case?
2. Let x1 , . . . , xn be a list of n distinct numbers. We say that ai and aj are inverted if i < j but
ai > aj . The Bubblesort sorting algorithm swaps pairwise adjacent inverted numbers in the
list until there are no more inversions (hence the list is sorted). Suppose that the input to
Bubblesort is a uniformly at random selected permutation of the set {x1 , . . . , xn }. Find the
expected number of inversions that need to be corrected by Bubblesort.
3. (a) Consider the set of numbers {1, . . . , n} and a random permutation on this set. Find
the expected number of positions i such that (i) = i.
(b) Suggest a method for generating a random permutation of size n, assuming that you
have access to a source of independent and unbiased random bits. Give a lower bound
for the number of random bits used by your algorithm. Now assume that you may use
function random(a,b), where random(a,b) returns a random integer between a and b,
inclusive. Further assume that a call to random(a,b) requires constant time. Analyze
the running time of your algorithm.
4. Problem 7-2 in the textbook.
5. Problem 7-4 in the textbook.
(If necessary, read pages 232-233 to refresh your memory on the definition of a stack.)

6. Consider an algorithm for integer multiplication of two n-bit numbers where each number is
split into three parts, each with n/3 bits.
(a) Similar to the integer multiplication algorithm presented in class, design and explain
an algorithm which multiplies the two integers using only six multiplications of n/3-bit
numbers (instead of the straightforward nine multiplications).
(b) Determine the asymptotic running time of your algorithm. Would you rather split it
into two parts or three parts?
(c) Suppose you could use only five multiplications instead of six. What is the asymptotic
running time of such an algorithm? In this case, would you rather split it into two parts
or three parts?
(d) Extra credit: Explain how you could use only five multiplications.

You might also like