



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This is an instance of the Selection problem: V (N,k): given an array of numbers of size N find kth largest (or smallest) element. Our goal here is to design an ...
Typology: Lecture notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Input: An array A[1..N ] of integers and N , the number of elements in the array.
Output: The second largest number in the array.
This is an instance of the Selection problem: V (N, k): given an array of numbers of size N find kth largest (or smallest) element.
Our goal here is to design an efficient algorithms for just the V (N, 2) instance of this this problem. We design this algorithm in two steps.
Step 1. In a V (N, 1) algorithm (i.e., selection of the largest element), the largest element must be compared to the second-largest.
Proof. To find the largest element, we must keep comparing elements of the array to each other, until all but one element lose a comparison. The second largest element must lose a comparison. But it can only lose to the largest element.
Conclusion 1. We can find the second largest element as follows:
10 4 5 8 7 2 12 3 1 6
10
10
10
8
8
12
12
12
12
9 11
9
11
Figure 1: Example of work of FindMaxRecursive() algorithm on an array of 12 elements. Four elements (1, 3, 11, 10: double-circled above) are compared to the largest element (12, highlighted).
Step 2. To design an efficient algorithm for finding the second largest element using the observation above, we need an algorithm for finding the largest element, where the largest element is compared to relatively few other elements.
The simple FindMax() algorithm that uses linear scan is not good because in the worst case (A[1] is the largest element), the largest element partici- pates in all N โ 1 comparisons.
However, the FindMaxRecursive(), that uses the tournament approach to finding the largest number will work. The work of this algorithm on an array of 12 numbers^1 is shown on Figure 1.
The algorithm itself is repeated below.
Algorithm FindMaxRecursive(I,J, A[I..J]) begin if I = J then return A[I]; //base case max1โ FindMaxRecursive(I, I+(J-I)/2, A); max2โ FindMaxRecursive(1+I+(J-I)/2,J, A); if max1>max2 then return max else return max2; end
Proposition. In Algorithm FindMaxRecursive() the largest element par- ticipates in at most โlog 2 (N )โ comparisons.
Proof. We show that any element of the array A participates in no more than โlog 2 (N )โ.
On each recursive step of the algorithm, the algorithm splits the range [I..J] (i.e, J โ I + 1 element) of elements in the array A into two parts. We observe, that the size of the first part (used in the first call) is โ (Jโ 2 I+1) โ, while the size of the second part (used in the second call) is โ Jโ 2 I +1โ. Start- ing with the [1..N ] range of N numbers, the first call (FindMaxRecursive(I,
(^1) We purposefully chose the number that is not a power of 2.
be done carefully. We do not know upfront which array element is going to be the largest, so we will carry information about all comparisons an array element won until this element loses a comparison.
From the technical side, we will change the FindMaxRecursive() algorithm to return an array of integers Compared[0..K]. We will establish the follow- ing convention:
Using these conventions, the Algorithm FindSecondMax() can be written as shown in Figure 2.
Algorithm Analysis: Running time and number of comparisons. From our study of FindMax() and FindMaxRecursive() algorithms, we know that both of them have running time O(N ) and use N โ 1 comparisons for an array of size N.
Using the Proposition from above, we analyze the running time and the number of comparisons for FindSecondMax() as follows:
Note: In general, proving lower bounds on running time of algorithms is very hard, and requires long proofs.
Trivial lower bounds. Time to read input. If size of input is N , and the problem requires all input to be read, than ฮฉ(N ) is a trivial lower bound on the running time for any algorithm solving the problem.
Theorem. Finding the largest number in an array of N numbers cannot be done in less than N โ 1 comparisons.
Proof. Each comparison eliminates one candidate for the largest number. In an array of N numbers N โ 1 numbers must be eliminated. Therefore, at least N โ 1 comparisons must take place.
Note. Compining with the known upper bound on the number of compar- isons, we obtain:
Algorithms FindMax() and FindMaxRecursive() are optimal in terms of number of comparisons.
We state the following theorem without proof.
Theorem. Finding the largest number in an array of N numbers requires at least N + โlog 2 (N )โ โ 2 comparisons.
Note. Combined with our upper bound we obtain the following:
Algorithm FindSecondMax() is optimal in the number of compar- isons.