Peak finding

来源:互联网 发布:淘宝代购敲诈应对技巧 编辑:程序博客网 时间:2024/04/30 07:01

1-Dimention peak-finding first.
For a n-length array arr, if exist arr[i-1] <= arr[i] >= a[i+1], i belong to [0,n-1], we consider i is a peak of this array.
If we divide the array in the middle for one comparation and recursively run.
The T(n) = T(n/2) + 2. T(n) = O(logn)

2-Dimention peak-finding.
Here’s one idea, when there’s plenty of numbers and we just want to find one of them that is suit for our needs, what we should do to optimize the algo is partition and pruning.
Now we will use the following algo as an example.
Assume there’s one m times n array. And our task is to find the peak for which the adjacent number is not great than.
Clear right.
Then we choose the middle column and find the max which we denote as g of this column subarray. Comparing this max with its left and right number.
If no greater than g, the g is a peak.
Otherwise, recursively run ther above step in the left half or right half which is determind by the which side has the greater value.
It take T(n) = T(n/2) + m and T(n) = mlogn = O(nlogn)

Prove the correctness:
The m times n array
Here we find the max value of mid column array, g.
If g is a peak, finish the program, right.
Otherwise h is greater than g, that means h is greater than any number of mid column right. Now we find the max value in the same column as h, using k to denote. No doublt k is greater than the number on its right side.
So the peak is k if there’s no column at the right side or the right number is not greater than k. Or the peak is not k, then we redo the above step to do the same work for k as we did for g.
Finally you can find, there’s only two case, not touch the left most column, but we find a peak, touch the left most column but the max of this column is a peak.
The right side proving is almost the same.
Proved.

3-D peak-finding.
Personally speaking, 3-D is similar to 2-D, but find the maximun of the middle plane, and the following steps are the same as 2-D.

0 0