CS 573
Midterm 2 Questions
Fall 2010
This exam lasts 90 minutes.
Write your answers in the separate answer booklet.
Please return this question sheet with your answers.
1.
Assume we have access to a function
R
ANDOM
(
k
)
that returns, given any positive integer
k
, an
integer chosen independently and uniformly at random from the set
{
1
,
2
,...,
k
}
, in
O
(
1
)
time.
For example, to perform a fair coin flip, we could call R
ANDOM
(
2
)
.
Now suppose we want to write an efficient function
R
ANDOM
P
ERMUTATION
(
n
)
that returns a
permutation of the set
{
1
,
2
,...,
n
}
chosen uniformly at random; that is, each permutation must
be chosen with probability 1
/
n
!.
(a)
Prove
that the following algorithm is
not
correct.
[Hint: Consider the case
n
=
3
.]
R
ANDOM
P
ERMUTATION
(
n
)
:
for
i
←
1 to
n
π
[
i
]
←
i
for
i
←
1 to
n
swap
π
[
i
]
↔
π
[
R
ANDOM
(
n
)]
return
π
(b)
Describe and analyze a correct
R
ANDOM
P
ERMUTATION
algorithm that runs in
O
(
n
)
expected
time. (In fact,
O
(
n
)
worstcase time is possible.)
2.
Suppose we have
n
pieces of candy with weights
W
[
1
..
n
]
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 Chekuri,C
 Natural number, π, Approximation algorithm

Click to edit the document details