Вы находитесь на странице: 1из 5

Lecture 4 and 5

Worst case linear time algorithm to find the ith


minimum
Scribe prepared by Praveen Kumar ( 07CS1016 )

Teacher: Prof. Partha Bhowmick


Department of Computer Science and Engineering
IIT Kharagpur
August 4, 2008

1
August 4, 2008 Section 0

Contents
1 Algorithm 3

2 Time complexity 3

2
August 4, 2008 Section 2

Abstract
In this lecture, we examine a selection algorithm whose running time is O(n)
in the worst case. Like the previous algorithm in which the list is partitioned
randomly and the ith minimum is found recursively, the algorithm SELECT also
finds the desired element by recursively partitioning the input array. However the
partition in this case is not done randomly but in such a way so as to guarantee a
good split.

1 Algorithm
Algorithm : SELECT
1. The n elements of the list L[1, 2, ...., n] are grouped into ⌈n/5⌉ groups of 5 elements
each and at most one group having the remaining n mod 5 elements.
2. The median of each of the ⌈n/5⌉ groups of elements is determined.
For determining the median, we check each element in the group. If for a particular
element, there are exactly 2 elements less than it in the group (and hence exactly 2 ele-
ments greater than it,considering distinct elements) then it is the median of that group.
3. Using SELECT recursively, we find the median µ of the ⌈n/5⌉ medians found in
previous step.µ is called the median-of-medians.
In case of even number of elements we consider the larger one (of the two medians) as
the median.
4. The input list L[1.....n] is partitioned into lists L1 and L2 using the median-of-medians
µ as the pivot element.Let k be one more than the number of elements on the low side
of the partition (L1 ), so that µ is the kth smallest element and there are n - k elements
on the high side of the partition(L2 ).
5. If i = k, then µ is the ith minimum. Hence, return µ. Otherwise, use SELECT
recursively to find the ith smallest element on the low side (L1 ) if i < k, or the (i - k)th
smallest element on the high side(L2 ) if i > k.

2 Time complexity
For calculating the time complexity of the algorithm, we need to find the number of
comparisions involved in each step of the algorithm.
In step 1, since the group is in-place divided into ⌈n/5⌉ groups the number of compari-
sions is O(⌈n/5⌉).
In step 2, the number of comparisions invoved in finding the median of a particular group
is O(1) as the number of elements in the group is always ≤ 5 (constant) and since there
are ⌈n/5⌉ such groups the total number of comparisions = ⌈n/5⌉ * O(1).

3
August 4, 2008 Section 2

In step 3, the median of ⌈n/5⌉ numbers is to be found recursively, hence number of


comparisions = T(⌈n/5⌉).
In step 4, partitioning is done, so number of comparisions is O(n).
In step 5 : We do recursion on L1 or L2 , but the pivot is choosen so that a good parti-
tioning is guaranteed.
Refer to Figure 1., number of groups = ⌈ n/5 ⌉

Therefore, number of groups with medians ≥ µ = ⌈ 1/2 ⌈ n/5 ⌉ ⌉


(group of µ and all groups lying to its right)

Number of elements strictly greater than µ ≥ 3( ⌈ 1/2 ⌈ n/5 ⌉ ⌉ - 2) , discounting


the last group ( as it may contain less than 3 elements greater than µ ) and the group
of µ.
which is equal to 3 ⌈ n/10 ⌉ - 6.
⇒ The number of elements ≤ µ is atmost n - 3 ⌈ n/10 ⌉ + 6 ≤ 7⌈n/10⌉ + 6.
Similar arguments can be shown for the number of elements ≥ µ.
Since recursion is used therefore number of comparisions is ≤ T(7⌈n/10⌉ + 6).

Now, referring to table 1 and summing up the total number of comparisions,


T(n) ≤ T(⌈ n/5 ⌉) + T(7⌈ n/10 ⌉ + 6) + O(n) since O(n) consumes the two O(⌈ n/5 ⌉)

Applying method of substitution :


Induction Hypothesis : T(m) ≤ cm for m < n.

4
August 4, 2008 Section 2

Step # comparisions
1. Group the n elments into ⌈n/5⌉ groups O(⌈n/5⌉)
2. Find the medians of the groups ⌈n/5⌉ * O[1] = O(⌈n/5⌉)
3. Recursively find µ T(⌈n/5⌉)
4. Partition the list L into L1 and L2 O(n)
5. Recurse on L1 and L2 to find the ith minimum ≤ T(7⌈n/10⌉ + 6)

Table 1: Number of comparisions required in each step

Step : We have to show that T(n) ≤ cn

T(n) ≤ T(⌈n/5⌉) + T(7⌈n/10⌉ + 6) + O(n)


≤ c(n/5 + 1) + c(7n/10 + 7) + 6c + O(n)
= 9nc/10 + 14c + O(n)
= cn - (nc/10 - 14c - O(n) )
≤ cn , if nc/10 - 14c ≥ O(n)

⇒ if nc/10 ≥ 14c
⇒ if n ≥ 140.
which is possible by considering a sufficiently large value of c.

Hence, the algorithm has linear time complexity.

Вам также может понравиться