Efficiently finding the largest surrounding square in 2D grid
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In computational geometry and computer graphics, efficiently finding the largest surrounding square in a 2D grid is a common problem. Such a grid could represent a variety of scenarios, such as pixels in an image, cells in a matrix, or tiles in a game map. The problem becomes relevant when you want to identify the largest possible square that adheres to certain constraints within a grid, such as being completely filled or empty, depending on the application.
Problem Definition
Given a 2D grid consisting of binary values (0s and 1s), the objective is to determine the largest square that contains only 1s. This problem can be tackled using several approaches, ranging from naive brute-force searches to more efficient dynamic programming techniques.
Naive Approach
The naive solution involves checking every possible square within the grid. Here's an outline of how a simple brute-force algorithm might work:
- Iterate over every cell in the grid as the potential top-left corner of a square.
- For each cell, attempt to form the largest possible square such that all cells within the square are 1.
- Keep track of the largest square found during this process.
While simple to implement, this approach is inefficient on larger grids. Its computational complexity is , where is the dimension of the grid. This high complexity is due to checking each possible square for every single cell.
Dynamic Programming Approach
An efficient method to solve the largest square problem is to use dynamic programming (DP). This approach drastically reduces computational overhead by breaking down the problem into subproblems that build upon each other.
Algorithm Steps
- Initialization:
- Create a DP table, `dp`, where `dp[i][j]` will store the side length of the largest square whose bottom-right corner is cell `(i, j)`.
- Initialize the table with zeros.
- Filling the DP Table:
- Traverse through each cell in the grid.
- If the cell contains a 1, update the DP table using:
- If the cell contains a 0, set `dp[i][j] = 0`.
- Determine the Largest Square:
- Throughout this process, keep track of the maximum value in the DP table, which represents the side length of the largest square found.
- Result:
- The area of the largest square is the square of the largest side length obtained.
The DP approach has a significantly reduced complexity of , making it feasible for larger grids.
Example
Consider the following grid:
Related reading
- Efficiently getting all divisors of a given number
- Efficiently implementing erode/dilate
- Efficiently randomly shuffling the bits of a sequence of words
- Efficiently selecting a set of random elements from a linked list
- Ehcache - using a ListInteger as the cache value
- Election Algorithms - A ring algorithm
- Efficiently summing log quantities
- Eigenvectors of a large sparse matrix in Tensorflow

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.