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, which is .
- β‘ 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 β repeatedly step to the largest neighbour. It terminates (values strictly increase, the grid is finite) but a snake-shaped ridge makes it traverse half the matrix β matrix 1 of the sheet starting from the top-left is exactly that adversary.
- Splitting on the middle column alone is β deciding which side to keep costs a column scan, and the sub-matrix is β the column never gets shorter. The level-sum is repeated times, with no geometric decay.
- The diagnostic β a target requires the per-call work itself to halve; only cutting both axes achieves that β Solving Recurrences (Telescoping), the root-dominated regime.
2. The cross method and its correctness argument
- Step 1 β scan the cross β take the middle row and middle column ( cells, ) and let be their maximum.
- Step 2 β test β if beats its four neighbours, return its coordinates; done.
- 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 the cross carves out.
- 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 every step, and every cross cell is , so the walk can never cross back over the cross; the grid is finite, so it halts at a peak, and that peak is inside . Discarding three quadrants therefore discards nothing that was 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 it. Sheet witness: at recursion level 2 of matrix 2, beats its sub-matrix neighbours 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 of the current sub-matrix, and maximise over all six lines.
- Why it works β the frame includes the sub-matrix boundary, so the winner is compared against cells that border the outside world; nothing beyond the frame can quietly dominate it.
- Cost is unchanged β 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 | per call, 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 from a cleverer scan. The two variants perform identical work at the root; they diverge because only the cross version makes level cost , turning the level sum from an arithmetic repetition into a convergent Geometric Series.
π Exam Execution Trace
On sheet matrix 1 (, -indexed). Every neighbour test is made against the ORIGINAL matrix , never against 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 have scanned cells at every level.
β οΈ Common Mistakes
- π‘ Claiming is impossible because the input has cells β reading the whole input is only mandatory when the answer depends on every cell. A peak is a local property, so the algorithm legitimately certifies an answer after touching cells.
- π‘ 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 and the recursion can return a non-peak.
- π‘ Comparing against sub-matrix neighbours β the -vs- failure. A candidate must be tested against its neighbours in ; the frame exists precisely to make that test meaningful.
- π‘ Assuming a unique peak β the spec asks for any local maximum. Arguments that quietly rely on there being one global answer prove nothing, and 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 β the work is for all levels, summing to .
- 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, so the decay never starts.
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 the walk terminate β so a peak of exists inside the quadrant holding .
- Why: The cross is a one-way barrier β is the cross maximum, so leaving the quadrant would require stepping down, which uphill movement forbids. The argument certifies existence without locating the peak, which is 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 (the sheetβs against ), so the algorithm can return a non-peak.
- Why: Correctness needs the boundary in scope β including the first and last rows/columns makes the recursionβs candidate comparable against cells outside the sub-matrix, restoring the invariant βthe reported cell beats its neighbours in β. Six lines instead of two is a constant factor, so the bound is untouched.