algorithm
binary matrix
dynamic programming
computational geometry
problem solving

Find largest rectangle containing only zeros in an N×N binary matrix

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

Problem Overview

Finding the largest rectangle containing only zeros in an N×NN \times N binary matrix is a classic algorithmic problem. Given a matrix where each element is either a 0 or a 1, the goal is to determine the dimensions and position of the largest rectangle consisting entirely of 0s.

Problem Formulation

Given a binary matrix M of size N×NN \times N, each entry M[i][j]M[i][j] is either 0 or 1. The aim is to identify the largest sub-matrix entirely composed of 0s and return its size.

Mathematical Foundation and Algorithmic Approach

Key Observations

  1. Rectangle as a Sub-matrix: A rectangle is effectively a contiguous sub-matrix determined by four boundaries: top, bottom, left, and right. Identifying the largest such rectangle requires assessing all rows and columns.
  2. Treat Rows as Histogram Heights: The primary technique involves interpreting consecutive rows of 0s as histogram heights. The challenge then transforms into finding the largest rectangle in a histogram, which can be efficiently solved using a stack-based approach.

Step-by-Step Algorithm

  1. Initialize Height Array: Start with a height array initialized to zeros of size NN, which will store the height of histograms (connected zeros vertically) for each column.
  2. Iterate Through Each Row:
    • For each row, update the height array.
      • If M[i][j]M[i][j] is 0, increment the height; otherwise, reset it to zero.
    • Apply the largest rectangle in histogram technique to the height array to find the maximum area rectangle possible ending at the current row.
  3. Histogram Rectangle Calculation:
    • Use a stack to store indices of the height array.
    • Iterate through each column and manage the stack to compute the largest rectangle.
    • For each index, while the stack is not empty and the current height is less than the height represented by the index on the top of the stack:
      • Pop the value from the stack and calculate the potential maximum area using the current index as the right boundary.

Complexity Analysis

  • Time Complexity: The solution involves traversing the matrix, which takes O(N2)O(N^2), and applying the histogram rectangle approach, each operation in the stack happens in constant time. Thus, the overall complexity is O(N2)O(N^2).
  • Space Complexity: The additional space is primarily used for the height array and the stack, leading to O(N)O(N) complexity.

Technical Illustration

Consider the following matrix:

  • For the first row: Update H = [1, 0, 0, 1] and evaluate the histogram to find the largest rectangle.
  • For the second row: Update H = [2, 1, 0, 2].
  • Continue similarly for subsequent rows.
  • This problem is extendable to M×NM \times N rectangular matrices.
  • Adapting the algorithm for maximal rectangles with 1s instead of 0s involves changing conditions for updating the height array.
  • The algorithm demonstrates the utility of converting traditional 2D problems into manageable 1D problems using histograms, which is a versatile approach in computational geometry and image processing.

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.