2D Local Maximum (Peak Finding)
Context: FIT2004_MOC Β· Applied 2 Problem 6 β the Divide and Conquer exercise where the naive halving is not aggressive enough, and the correctness argument (not the code) carries the marks. Parent Framework: Divide and Conquer
Quick Revision
- π― Objective: in an matrix of distinct numbers, return the coordinates of any element larger than its (2β4) orthogonal neighbours β in worst case.
- π¦ Core Components: scan a window frame (first/middle/last row column) β its max is a peak, or a neighbour lies in one quadrant β recurse into that quadrant, .
- β‘ Key Constraint: on an input of cells means the algorithm cannot read most of the matrix β so the subproblem must shrink in both dimensions. Halving one axis leaves the per-call scan at forever and costs .
π How It Works
1. Why the two obvious algorithms fail their bound
- Gradient ascent is β stepping to the largest neighbour terminates (values strictly increase, grid finite) but a snake-shaped ridge forces a traversal of half the matrix β sheet matrix 1 from the top-left is exactly that adversary.
- Splitting on the middle column alone is β the deciding column scan costs and the sub-matrix is β the column never gets shorter, so repeats times with no geometric decay.
- The diagnostic β a target requires the per-call work itself to halve; only cutting both axes achieves that (, root-dominated) β Solving Recurrences (Telescoping).
2. The cross method and its correctness argument
- Step 1 β scan the cross β middle row and middle column ( cells, ); let be their maximum.
- Step 2 β test β if it beats its four neighbours, return its coordinates.
- Step 3 β follow the escape β otherwise some neighbour exists, and must lie off the cross (everything on it is ) βΉ sits in one of the four quadrants.
- Step 4 β recurse into that quadrant β size .
- β‘ The correctness claim β is guaranteed to contain a local maximum of the whole matrix . Run gradient ascent from : values strictly increase, and every cross cell is , so the walk can never cross back; the grid is finite, so it halts at a peak inside . Discarding three quadrants therefore discards nothing needed.
3. The window-frame repair
- The bug in plain cross recursion β the max of a sub-matrixβs cross may beat its neighbours within the sub-matrix yet lose to a cell just outside. Sheet witness: beats but is crushed by , which belonged to the previous levelβs cross.
- The fix β scan a window frame: the first, middle and last rows plus the first, middle and last columns, maximising over all six lines β the boundary is then in scope, so nothing beyond the frame can quietly dominate the winner.
- Cost is unchanged β six lines of length is still , so the recurrence and the bound survive.
βοΈ Complexity
| Approach | Recurrence | Best / Average / Worst | Auxiliary space | Why it lands there |
|---|---|---|---|---|
| Gradient ascent (walk uphill) | β | / β / | a ridge forces a traversal of half the cells | |
| Middle-column split | , fixed | all | frames | column length never shrinks βΉ scans of cost |
| Cross / window frame | all | frames | βΉ root-dominated |
When It Flips: the entire gain comes from the second axis, not a cleverer scan. Both variants do identical work at the root; only the cross version makes level cost , turning the level sum into a convergent Geometric Series.
π Exam Execution Trace
On sheet matrix 1 (, -indexed). Every neighbour test is made against the ORIGINAL matrix , never the sub-matrix β that is the window-frame discipline:
| Step | Sub-matrix | Frame max (position) | Beaten by? | Action |
|---|---|---|---|---|
| 0 (Init) | rows , cols | at | at | recurse into the quadrant holding |
| 1 | rows , cols | at | at | recurse into the quadrant holding |
| 2 | row , col | at | none ( below, left) | return |
Read-off: three levels, scans of length β the halving of the scan is the whole point; a column-only split would scan cells at every level.
β οΈ Common Mistakes
- π‘ Claiming is impossible because the input has cells β reading the whole input is mandatory only when the answer depends on every cell. A peak is a local property, so cells suffice to certify one.
- π‘ Recursing into the quadrant with the largest cross value instead of the one containing β the correctness argument is built on specifically ( every cross cell); any other quadrant carries no such guarantee.
- π‘ Comparing against sub-matrix neighbours β the -vs- failure; a candidate must be tested against its neighbours in .
- π‘ Assuming a unique peak β the spec asks for any local maximum; multiple peaks is the normal case.
π§ Active Recall
Splitting on the middle column halves the matrix every call, yet the algorithm is rather than . Where does the argument break?
- Hint: Write out the per-level work, not the per-level subproblem count.
Answer
- Short answer: the sub-matrix is , so the deciding scan is a full-length column at every level β for all levels βΉ .
- Why: Only shrinking work sums geometrically β collapses to only when the shrinks with the argument; halving one axis halves the cell count but not the scan length.
Justify discarding three of the four quadrants after finding a neighbour .
- Hint: Imagine walking uphill from and ask what would have to happen to escape.
Answer
- Short answer: a strictly-increasing walk from cannot re-enter the cross (every cross cell is ), and a finite grid of distinct values makes it terminate β so a peak of exists inside the quadrant holding .
- Why: The cross is a one-way barrier β leaving the quadrant would require stepping down, which uphill movement forbids. The argument certifies existence without locating the peak β exactly what a D&C correctness proof needs.
Why replace the cross with a window frame, given both cost ?
- Hint: Ask what a sub-matrix does not know about itself.
Answer
- Short answer: without the boundary rows and columns, a sub-matrixβs local winner may be beaten by a cell just outside it ( against ), so the algorithm can return a non-peak.
- Why: Correctness needs the boundary in scope β including the first and last rows/columns restores the invariant βthe reported cell beats its neighbours in β; six lines instead of two is a constant factor.