The Hong Kong University of Science & Technology
COMP 271: Design and Analysis of Algorithms
Fall 2007
Tutorial 2 D&C: Partition & Selection (Solution)
Below are the solutions of the problems of the tutorial.
Question 1
: Let
N
(
n, i
) be the expected number of times RandomizedSelect
is called (first call and recursive calls) when finding the
i
th smallest ele
ments in an array of
n
elements. Set up a recurrence equation for
N
(
n, i
).
Solution:
(Note: The question is asking about the number of times the function is called,
not the number of comparisons as in the lecture notes.)
Suppose we have chosen the
k
th smallest element in the first call of
RandomizedSelect:
For
i
=
k
, expected number of calls to RandomizedSelect is 1.
For
i > k
, expected number of calls to RandomizedSelect is:
1 +
N
(
n

k, i

k
)
For
i < k
, expected number of calls to RandomizedSelect is:
1 +
N
(
k

1
, i
)
Therefore, the expected number of call to RandomizedSelect is:
1
n
ˆ
1 +
i

1
X
k
=1
(1 +
N
(
n

k, i

k
)) +
n
X
k
=
i
+1
(1 +
N
(
k

1
, i
))
!
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.
 Spring '07
 ARYA
 Algorithms, Selection algorithm, th smallest element, ith smallest elements

Click to edit the document details