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

ApproachRecurrenceBest / Average / WorstAuxiliary spaceWhy it lands there
Gradient ascent (walk uphill)β€” / β€” / a ridge forces a traversal of half the cells
Middle-column split per call, fixed all framescolumn 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:

StepSub-matrixFrame max (position)Beaten by?Action
0 (Init)rows , cols at at recurse into the quadrant holding
1rows , cols at at recurse into the quadrant holding
2row , 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