5

For an array a: a1, a2, … ak, … an, ak is a peak if and only if ak-1 ≤ ak ≥ ak+1 when 1 < k and k < n. a1 is a peak if a1 ≥ a2, and an is a peak if an-1 ≤ an. The goal is to find one peak from the array.

A divide and conquer algorithm is given as follows:

find_peak(a,low,high):
    mid = (low+high)/2
    if a[mid-1] <= a[mid] >= a[mid+1] return mid // this is a peak;
    if a[mid] < a[mid-1] 
        return find_peak(a,low,mid-1) // a peak must exist in A[low..mid-1]
    if a[mid] < a[mid+1]
        return find_peak(a,mid+1,high) // a peak must exist in A[mid+1..high]

Why this algorithm is correct? I think it may suffer from losing half of the array in which a peak exists.

Andre Silva
  • 4,782
  • 9
  • 52
  • 65
roland luo
  • 1,561
  • 4
  • 19
  • 24
  • It really comes down to the assertions; is it true that "a peak must exist in A[low...mid-1]" if a[mid] < a[mid-1]? – Oliver Charlesworth Jun 02 '13 at 21:07
  • 3
    This algorithm assumes an awful lot about the way the data is structured.. – christopher Jun 02 '13 at 21:08
  • 1
    re "I think it may suffer from losing half of the array in which a peak exists." You said earlier you only wanted to find _one_ peak. So it doesn't matter if you lose half the array so long as there is a peak in the other half. Also, have you tried coding up the algorithm and running it in a debugger with some different test cases? Might give you some intuition as to what it is doing. – TooTone Jun 02 '13 at 21:22
  • 1
    This is assuming that the array is holding a smoothing changing function. – Raedwald Jul 12 '13 at 09:32

1 Answers1

7

The algorithm is correct, although it requires a bit of calculus to prove. First case is trivial → peak. Second case is a "half peak", meaning that it has the down slope, but not up.

We have 2 possibilities here:

  • The function is monotonically decreasing till amid → a1 ≥ a2 is the peak.
  • The function is not monotonically decreasing till amid → There is a 1≥k≥mid, for which ak ≥ ak-1. Let's choose the largest k for which this statement is true → ak+1 < ak ≥ ak-1 → and that's the peak.

Similar argument can be applied for the third case with the opposite "half peak".

icktoofay
  • 126,289
  • 21
  • 250
  • 231
SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85