(Solved) : Remember Peak Finding Problem Week One Devised Following Divide Conquer Strategy Eliminate Q44112350 . . .
Remember the “peak finding problem” from week one?
We devised the following divide and conquer strategy in which weeliminate about half of the rows in each step.
We “probe” the middle row (among thoseremaining) and find the largest element in that row by bruteforce.
If it happens to also be a peak forthe matrix, we are done!
Otherwise we recursively find a peakamong the elements among the rows in an “uphill” direction(eliminating the others).
Below is pseudo-code describing thisalgorithm.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// m: the 2d matrix
// n: the matrix dimension (nxn)
// lowRow/hiRow: range of rows under consideration
find_peakB(m, n, lowRow, hiRow) {
if(lowRow==hiRow) {
returnlargest entry in m[lowRow]
}
else {
r =(lowRow + hiRow)/2;
findlargest entry in row r
if ithappens to be a peak in m return it
else {
if(theentry above it is larger)
returnfind_peakB(m, n, lowRow, r-1)
else{
//entry below it must be larger
returnfind_peakB(m, n, r+1, hiRow)
}
}
}
This approach has a worst case runtime of (nn): The recursionexamines about n rows in total and, for each such row spends (n)time to scan it to find the max
Can we improve the runtime toO(n)?
We suspect an O(n)worst case runtime might be within reach. Hereare some observations:
-
While the “problem size” shrinks during recursion, the number ofcolumns remains fixed — and the time required to scan the “proberow” at each recursive step is also fixed. Maybe if we can find away to shrink the time taken by the “scan” step?
-
Columns vs. Rows: as presented, the algorithm probes and scansrows, but could have just as easily probed and scanned columns.
Here’s an idea:
Instead of always probing and scanningthe middle row in the remaining sub-matrix, we alternateprobing/scanning middle rows and middle columns. The policydetermining the sub-matrix is logically the same. For example, ifwe start with a 15×15 matrix, after one probe and scan, we have a7x15 submatrix; then we probe and scan the middle column of this7x15 submatrix to yield a 7×7 submatrix and so on.
TODO A (10 pts): The good news: this approachwhich alternates horizontal and vertical “cuts” does indeed resultin O(n) runtime. Your job: show that this is indeed true.
TODO B (15 pts): The bad news: unfortunately,this approach is buggy! Your job: Devise a 7×7 instance of the peakfinding problem on which this alternating approach fails. Walk thereader through what happens on your instance (via annotateddiagrams) and why the result is incorrect.
TODO C (25 pts): Devise a correct (n)time algorithm for the peak-finding in a matrix problem. Hint: theidea of making sure the “time-to-scan” shrinks with recursion assuggested above is sound; give some thought to alternative “things”to probe/scan.
Expert Answer
Answer to Remember the “peak finding problem” from week one? We devised the following divide and conquer strategy in which we elim…
OR