Notes on Linear time selection - Algorithms Design and Analysis | CS 422, Study notes of Algorithms and Programming

Material Type: Notes; Professor: Kowalczyk; Class: Algorithms Design And Analysis; Subject: Computer Science; University: Northern Michigan University; Term: Winter 2008;

Typology: Study notes

Pre 2010

Uploaded on 02/25/2010

koofers-user-pqv
koofers-user-pqv 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithm Design and Analysis 2/25/2008
Lecture 19 - Linear time selection
Instructor: Mike Kowalczyk
1 Linear time selection
1.1 Define the problem
First, let’s define the problem carefully.
Selection problem:
Input: Array of nelements, and index 0 kn1.
Output: Returns A0[k], where A0is the sorted version of A.
Note that our problem definition doesn’t necessarily say that we actually sort the array; it’s
just a way of specifying the output.
1.2 Divide and conquer algorithm
Our idea starts with grouping the nelements in sets of 5 each. Then, sort each group of 5, take
the median of each of group of 5, and recursively find the median of those medians-of-5. Now,
we know that the median-of-medians is going to be greater than approximately 3n/10 elements.
Similarly, it’s less than 3n/10 elements, so the median-of-medians is somewhere from the 3/10 mark
to the 7/10 mark. Because of this, we can use the median-of-medians as a pivot, and split up the
remaining work, just like Quicksort. The fact that the pivot is roughly in the middle means that it
should be fast, but we’ll prove this to make sure.
selection(array A, index k) {
Group A into sets of 5 elements each.
Sort each set of 5.
Let M be the set of medians of those sets of 5.
if (M is size 1) return M[0];
p = selection(M, M.length / 2); //finds the median of M
//Use the pivot, p, to split A into arrays S and B.
Let S = the elements of A that are smaller than p
Let B = the elements of A that are bigger than p
if (|S| == k) { //|S| denotes the number of elements in S
return p;
}
else if (k < |S|) {
return selection(S, k);
}
else { //k > |S|
1
pf2

Partial preview of the text

Download Notes on Linear time selection - Algorithms Design and Analysis | CS 422 and more Study notes Algorithms and Programming in PDF only on Docsity!

Algorithm Design and Analysis 2/25/

Lecture 19 - Linear time selection

Instructor: Mike Kowalczyk

1 Linear time selection

1.1 Define the problem

First, let’s define the problem carefully.

Selection problem: Input: Array of n elements, and index 0 ≤ k ≤ n − 1. Output: Returns A′[k], where A′^ is the sorted version of A.

Note that our problem definition doesn’t necessarily say that we actually sort the array; it’s just a way of specifying the output.

1.2 Divide and conquer algorithm

Our idea starts with grouping the n elements in sets of 5 each. Then, sort each group of 5, take the median of each of group of 5, and recursively find the median of those medians-of-5. Now, we know that the median-of-medians is going to be greater than approximately 3n/10 elements. Similarly, it’s less than 3n/10 elements, so the median-of-medians is somewhere from the 3/10 mark to the 7/10 mark. Because of this, we can use the median-of-medians as a pivot, and split up the remaining work, just like Quicksort. The fact that the pivot is roughly in the middle means that it should be fast, but we’ll prove this to make sure.

selection(array A, index k) { Group A into sets of 5 elements each. Sort each set of 5. Let M be the set of medians of those sets of 5. if (M is size 1) return M[0]; p = selection(M, M.length / 2); //finds the median of M //Use the pivot, p, to split A into arrays S and B. Let S = the elements of A that are smaller than p Let B = the elements of A that are bigger than p if (|S| == k) { //|S| denotes the number of elements in S return p; } else if (k < |S|) { return selection(S, k); } else { //k > |S|

return selection(B, k - |S|); } }

1.3 Runtime analysis

At level i, we have that the total size of all nodes’ arrays added up is ( 109 )in. Each node takes only cn time for the splitting up of the problem and so forth (everything but the recursive calls), where c is a constant. We have depth is i where ( 109 )in = 1. This is equivalent to n = ( 109 )i. So i = log 10 / 9 n. So the total runtime is

log ∑ 10 / 9 n

i=

)icn = cn

log ∑ 10 / 9 n

i=

)i

≤ cn

∑^ ∞

i=

)i

= 10 cn = Θ(n).