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](ifi+1 < m),A[i][j] >= A[i-1][j](ifi-1 >= 0),A[i][j] >= A[i][j+1](ifj+1 < n),A[i][j] >= A[i][j-1](ifj-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:
- Select the Middle Column: Choose the middle column of the matrix. This can be calculated as
mid = n // 2, wherenis the total number of columns. - Find the Global Maximum in the Column: Find the maximum element in the
midcolumn. Let's say it is at position(max_row, mid). - 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.
- Base Case: When you narrow down to a single column or element, it's inherently the peak.
Example with Visualization
Consider a 5x5 matrix:
- Select middle column (n = 5, mid = 2): Column =
[10, 12, 15, 18, 14] - Find global maximum:
18at position (3, 2) - Checking neighbors of 18:
- Up: 15, Down and on the Right: 19 (larger than 18)
- Recur on the right: New matrix focus:
- Middle column in this reduced array:
- Column =
[11, 7, 19, 17], Max is19a peak.
Computational Complexity
The complexity of the Divide and Conquer approach is , where is the number of rows and is the number of columns. This is because each recursion step involves finding a maximum in a column, which is and we reduce the column size logarithmically.
Summary Table
| Step | Description |
| Select Middle | Choose the midpoint column for the given matrix. |
| Find Maximum | Identify maximum element in the midpoint column. |
| Compare Neighbors | Assess if it’s a peak or determine the next direction of search. |
| Recur | Recurse on subset of the matrix based on comparison results. |
| Base Case | Resolve 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.

