2D arrays
algorithms
peak finding
computational methods
data structures

Algorithm to find peaks in 2D array

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

The problem of finding peaks in a 2D array is a fascinating computational geometry challenge with applications in image processing, surface analysis, and data visualization. Peaks are locally maximal elements that are greater than or equal to their neighbors. This article outlines an algorithm to find such peaks, including technical explanations, examples, and potential enhancements.

Definition of a Peak in a 2D Array

In a 2D array, formally defined as a matrix A with dimensions m x n, a peak is an element A[i][j] such that:

  • It is not on the boundary, and
  • A[i][j] >= A[i+1][j] (if i+1 < m),
  • A[i][j] >= A[i-1][j] (if i-1 >= 0),
  • A[i][j] >= A[i][j+1] (if j+1 < n),
  • A[i][j] >= A[i][j-1] (if j-1 >= 0).

If a point satisfies the above conditions, it is defined as a peak.

Algorithm to Find Peaks

1. Divide and Conquer Approach

This method breaks down the problem into simpler sub-problems, solves each sub-problem independently, and combines their solutions.

Steps:

  1. Select the Middle Column: Choose the middle column of the matrix. This can be calculated as mid = n // 2, where n is the total number of columns.
  2. Find the Global Maximum in the Column: Find the maximum element in the mid column. Let's say it is at position (max_row, mid).
  3. Compare with Neighbors:
    • If this global maximum is a peak, it's a valid solution.
    • If the right neighbor is greater, recur for the right half of the matrix.
    • If the left neighbor is greater, recur for the left half of the matrix.
  4. Base Case: When you narrow down to a single column or element, it's inherently the peak.

Example with Visualization

Consider a 5x5 matrix:

 
110  8 10 10  6
214 13 12 11 15
311  9 15  7 10
421 20 18 19 12
516 15 14 17 13
  1. Select middle column (n = 5, mid = 2): Column = [10, 12, 15, 18, 14]
  2. Find global maximum: 18 at position (3, 2)
  3. Checking neighbors of 18:
    • Up: 15, Down and on the Right: 19 (larger than 18)
  4. Recur on the right: New matrix focus:
 
115
211
3 7
419
517
  1. Middle column in this reduced array:
    • Column = [11, 7, 19, 17], Max is 19 a peak.

Computational Complexity

The complexity of the Divide and Conquer approach is Θ(mlogn)\Theta(m \log n), where mm is the number of rows and nn is the number of columns. This is because each recursion step involves finding a maximum in a column, which is O(m)O(m) and we reduce the column size logarithmically.

Summary Table

StepDescription
Select MiddleChoose the midpoint column for the given matrix.
Find MaximumIdentify maximum element in the midpoint column.
Compare NeighborsAssess if it’s a peak or determine the next direction of search.
RecurRecurse on subset of the matrix based on comparison results.
Base CaseResolve to a minimal state where peak can be trivially secured.

Advanced Techniques

1. Gradient Ascent Method

An alternative localized method that mimics climbing to a peak by iteratively moving in the direction of the greatest increase among neighbors until a peak is reached.

  • Steps:
    • Start at a random position.
    • Compare values of neighbors.
    • Move to the largest neighbor recursively.
    • Stop when no neighbor has a larger value.

This method is more practical for large data matrices where a single peak is needed rapidly.

2. Multi-Peak Detection

The described techniques can be adapted for detecting multiple peaks by iterating over the elements and applying the same logic to uncover local maxima.

Applications in Real World

  • Image Processing: Identifying high-intensity points in an image.
  • Terrain Analysis: Determining mountain peaks in topographical data.
  • Data Visualization: Highlighting local maxima in heatmaps or other 2D data representations.

By employing a 2D peak-finding algorithm, we efficiently uncover crucial insights embedded in two-dimensional datasets. This article provided a comprehensive dive into an effective and computationally economic strategy that can be further tailored to specific applications or refined with more advanced computational methods.


Course illustration
Course illustration

All Rights Reserved.