I was looking into MIT's open courseware first lecture on an introduction to algorithms and there is something that is not terribly obvious to me. You cant start watching the lecture at 24:30 here and the lecture notes with all the details of the 1D peak problem definition and solution in here
The problem goes:
for an array of "n" integer elements find a peak
and gives an example array of size 8: example = [6,7,4,3,2,1,4,5]
Definition of a peak
For the example
array above example[1]
and example[7]
are "peaks" because those numbers are greater or equal than their adjacent element and a special condition for the last element of the array applies that it only needs to be greater than or equal to the element preceding it. that is:
example[1] >= example[0] && example[1] >= example[2] #=> is true and therefore a peak
example[7] >= example[6] #=> is true and therefore a peak
Important observations From this example we can appreciate that the array may be unsorted, that it may contain duplicates and that it may contain more than one peak and to my interpretation it may even not contain any single peak.
So far so good but my troubles started when he goes to argue that a definition of splitting the array in binary search tree makes to find a peak, ** that may be terribly obvious to everyone in that class but not for me, it seems arbitrary or I failed to understand something very important**
The professor goes to define in pseudo-code a binary search algorithm to find a peak:
My questions/concerns
- Given the condition above in
A
why go to the left? instead of the right? - Given the condition above in
B
why go to the right? instead of the left? - The binary search algorithm assumes we start from a sorted array, so how come it makes sense to apply it to data that may be unsorted?
- Does the solution guarantees that for cases when there is only one "peak" we don't initially discard the half that contains the peak? If so how/why?
Since the array can be unsorted and it may contain duplicates I don't understand where is the guarantee that if conditions in either A
or B
hold to be true it would make sense to go look for either right or left, it seems arbitrary to me and that if you choose wrong you could discard the half of the array that actually might have the only peak
Am I missing something important? If so what?