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

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

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 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