Finding Second Largest Element in an Array The Problem ..., Lecture notes of Design

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

2022/2023

Uploaded on 03/01/2023

dyanabel
dyanabel ๐Ÿ‡บ๐Ÿ‡ธ

4.7

(20)

287 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
. .
Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar
. .
Finding Second Largest Element in an Array
The Problem
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.
Problem Analysis
This is an instance of the Selection problem: V(N, k): given an array of
numbers of size Nfind 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:
1. Find the largest element.
2. Collect all elements of the array that were directly compared to the
largest element.
3. Find the largest element among them.
1
pf3
pf4
pf5

Partial preview of the text

Download Finding Second Largest Element in an Array The Problem ... and more Lecture notes Design in PDF only on Docsity!

Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar

Finding Second Largest Element in an Array

The Problem

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.

Problem Analysis

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:

  1. Find the largest element.
  2. Collect all elements of the array that were directly compared to the largest element.
  3. Find the largest element among them.

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:

  • Compared[0] = K (i.e., the 0th element of the array holds the length of the array);
  • Compared[1] = max (i.e., the first element of the array is the number that FindMaxRecursive() would return;
  • Compared[2],... , Compared[K] are all numbers with which max has been compared thus far.

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:

  • The first call to FindMaxTournament() uses N โˆ’1 comparisons and has running time O(N ).
  • The second call to FindMaxTournament() passes an array of size at most โŒˆlog 2 (N )โŒ‰ and therefore, it uses โŒˆlog 2 (N )โŒ‰ โˆ’ 1 comparisons and runs in O(log 2 (N )).
  • Therefore, FindSecondMax():
    • Uses N โˆ’ 1 + โŒˆlog 2 (N )โŒ‰ โˆ’ 1 = N + โŒˆlog 2 (N )โŒ‰ โˆ’ 2 comparisons;
    • Has running time O(N ) + O(log 2 (N )) = O(N ).

Lower Bounds

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.

Lower Bound for Finding Largest Number

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.

Lower Bound for Finding Second Largest Number

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.