Algorithm Design
Computational Geometry
Data Structures
Grid Algorithms
Problem Solving

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.

Practice algorithms

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:

  1. Iterate over every cell in the grid as the potential top-left corner of a square.
  2. For each cell, attempt to form the largest possible square such that all cells within the square are 1.
  3. 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 O(n4)O(n^4), where nn 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

  1. 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.
  2. Filling the DP Table:
    • Traverse through each cell in the grid.
    • If the cell contains a 1, update the DP table using: dp[i][j]=min(dp[i1][j],dp[i][j1],dp[i1][j1])+1dp[i][j] = \min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
    • If the cell contains a 0, set `dp[i][j] = 0`.
  3. 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.
  4. 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 O(n2)O(n^2), making it feasible for larger grids.

Example

Consider the following grid:


Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.