views:

161

answers:

5

I have been pondering about my homework question for a while. I welcome (and prefer) any suggestions or approach on how to attack this problem.

Basically, I have an array A of size N. We do not know the elements but we know they are distinct. The only thing that I have is a person who will take two indices (i,j) in N. This person will then tell me whether A[j] is < or > A[i]. I want to find the algorithm for finding the index of the 2nd smallest element by asking <= n + log n questions to this person.

A: 

Attacking such problems is often best done by "divide and conquer". I.e. try to simplify/divide the problem, solve the simpler problem(s), then see if it gave any insight which helps you solving the original question. If the simpler problem is still too hard, try to simplify it further, etc.

In this case, you could start by finding the smallest element from the array. How would you do that?

Péter Török
hmm i like that approach, finding the smallest first ....
denniss
A: 

Look into sorting algorithms like merge sort, which has a worst case complexity of O(n log n). The "person" telling you if A[j] > A[i] is true or false is obviously a comparison function.

Merge sort works by recursively dividing your array into two smaller arrays half the size of the original array, then applying the merge sort algorithm to these arrays again. If you arrive at a final step of two arrays with only one element, you ask the person/comparison function to tell you how to sort these arrays/elements. From this step up, you begin to merge your sub-arrays back into your orginal, but now sorted array.

At the end, you can simply return the second element of the sorted array, this being the second smallest.

Jim Brissom
If I understand correctly, the OP can not rearrange the elements in the array.
Péter Török
OP is looking for n + log n, not n * log n.
Henrik
n+log n is kind of hard (i.e., n*log n is a lower bound for comparison sorting) using only binary comparisons, wouldn't you agree?
Jim Brissom
A: 

First, find the smallest element. You can do this with n-1 comaparisons in such a way, that each element is compared to at most log(n) other elements. Now look which elements have been compared to the smallest element and find the smallest of those.

Henrik
Your algorithm, same as mine has a worst case scenario of 2*n
Ivan Ferić
@Ivan: no it's not. It's same as what sandris calls "championship tree' and it's n + log n.
Henrik
A: 
  1. Use 2 variables - varSmallest and var2ndSmallest
  2. Ask person to compare 1st and 2nd value in array - set index of smaller to varSmallest, and the other to var2ndSmallest
  3. Take the next index in sequence and name it varIndexToCheck
  4. Compare values of varIndexToCheck and of var2ndSmallest - if value of varIndexToCheck is larger than value of var2ndSmallest go to step 3
  5. Compare values of varIndexToCheck and of varSmallest - if value of varIndexToCheck is larger than value of varSmallest set var2ndSmallest = varIndexToCheck and go to step 3
  6. Else, set var2ndSmallest = varSmallest and varSmallest = varIndexToCheck and then go to step 3

Repeat until there are no more indexes. After that, resulting index is inside var2ndSmallest variable and it is O(n log n) complexity

Ivan Ferić
But this has a worst case complexity of about 2n; OP is looking for n + log n.
Henrik
It has O(n + log n) and I believe that the real question was in fact "Find the O(n + log n) algorithm that can find 2nd smallest element in array", but the OP wanted to create a story out of the question.
Ivan Ferić
OP wants n + log n; without O(). Note that O(n + log n) is the same as O(n).
Henrik
+7  A: 

This answer describes how to find the second greatest element; finding the second smallest can be done analogously. For simplicity, we also assume that all numbers are different.

In order to find the greatest element, let us build a 'championship' tree: pair up the elements, decide which is greater (that one is the 'winner') then pair up the winners, decide which is greater, and so on, until you find the 'champion', which is the greatest element. This takes n steps. Now, the second greatest element must have been compared to the champion. (because only the champion could defeat it). log n elements have been compared to the champion, so from these, pick the greatest; this takes log n steps.

As an example, let us see how this works for the number tuple [6,4,3,5,2,1] . In the first round, pairs are (6,4), (3,5), (2,1). Winners are the greater elements in each pair, that is, 6,5,2. In the second round pairs are (6,5), 2. (2 has no pair here so it will get promoted to the next round automatically). Winners of the second round are 6 and 2, in the third round the only pair is (6,2), 6 is the winner. Now, by pairing up elements and choosing a winner we have built up a (rooted, binary) tree: alt text

This tree has the property that for a node x and its children y,z we have x>=y, x>=z, so we know that the greatest element is the one at the top (at the root). We also know that the second greatest element w did not make it to the top, so it has a parent in the tree. But its parent is greater than or equal to w, so at some level of the tree, one of the children of the greatest element is w. (In other words, the second greatest element could only be 'defeated' by the greatest element). So all we have to do is go back on the path the greatest element has taken and collect all direct children, we know the second largest is among them. In our case, these are the elements 2,5,4 . (In general, there are about log n of them, where log denotes base two logarithm, because the tree is about log n high.). From these elements we pick the largest with any method that takes log n steps, and we found the second largest.

All this may remind us to a championship, where numbers denote how 'good' each team is, hence the term 'championship tree'.

sandris
`n + n/2 + n/4 + ... ` is not `n` steps, it's `2n`.
IVlad
@IVlad: in the first round, you start with n/2 comparisons, not n.
Henrik
yeah, well, not n but n-1 if we really want to speak strictly
sandris
@Henrik - how so? `1 2 3 4 5` - compare 1-2, 2-3, 3-4, 4-5, which is `n - 1` comparisons. @sandris - true, but still more than `n + log n`
IVlad
@IVlad: no, I believe he means 1-2 3-4 5-6 7-8, and then semi-finals and finals. n/2 + n/4 +... + 1 for n being a power of 2 is n-1. When n is not such a power, you can adjust things easily.
Eyal Schneider
@IVlad: Do you mean that log n denotes the natural logarithm of n rather than the base two logarithm? I don't think that's attainable.
sandris
is this really going to work? supose i have the following array [6,4,3,5,2,1]. On the lowest level of the tree I will have (6,4) (3,5) (2,1). THe second largest is 2 but when I get The min of each pair i will get (4,3) (2,1) => (3,1) and the second largest is 3?
denniss
@My previous response: getMax also won't do it for this.
denniss
@denniss: Sorry, there's some confusion: my answer aimed at finding the greatest element rather than the smallest as your question required. I edited the answer and added some clarification.
sandris
Example implementation in Erlang: http://pastebin.com/HW8nmVwV
Mazen Harake