Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Algorithm
3.
Method 1
4.
Method 2
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Maximum size square sub-matrix

Author Aditi
0 upvote

Introduction

In this blog, we will learn how to find the square maximum submatrix of all 1's in a binary matrix. So, before we dive down first, we should know about the binary matrix. A binary matrix is a two-dimensional array having values only 0 and 1. We need to find the size of a small square matrix formed by only 1 in this binary matrix.

Also read -  Decimal to Binary c++

Algorithm

Let the given matrix by matrix[r][c]. The concept of the algorithm is to construct another matrix of the same dimension sum[][] in which each entry sum[i][j] represents the size of the sub-matrix with 1's as its value and square in nature.

  • Construct a matrix that will contain the sum for a given matrix matrix[r][c]. Let's say, sum[r][c].
    1. Copy the first row and column as it is from matrix[][] to sum[][].
    2. For rest entries the following expressions will be used to construct sum[][] :-

If matrix[i][j] is 1 then

sum[i][j] = min(sum[i][j-1], sum[i-1][j], sum[i-1][j-1])

Else

sum[i][j] = 0

 

  • Find maximum entry in sum[r][c]
  • The value and coordinate will be used to find the maximum submatrix in matrix[][]

Method 1

#include <bits/stdc++.h>
#define R 6
#define C 5
using namespace std;

void printMaxSubMatrix(int matrix[R][C])
{
    int i,j;
    int sum[R][C];
    int maxSum, max_row, max_col;
    //Copying first row and column
    for(i = 0; i < R; i++)
        sum[i][0] = matrix[i][0];
    for(j = 0; j < C; j++)
        sum[0][j] = matrix[0][j];
    //Constructing other entries of S[][]
    for(i = 1; i < R; i++)
    {
        for(j = 1; j < C; j++)
        {
            if(matrix[i][j] == 1)
                sum[i][j] = min({sum[i][j-1], sum[i-1][j], sum[i-1][j-1]}) + 1;
            else
                sum[i][j] = 0;
        }
    }
    //Finding maximum entry from sum[][] matrix an indexes also
    maxSum = sum[0][0]; max_row = 0; max_col = 0;
    for(i = 0; i < R; i++)
    {
        for(j = 0; j < C; j++)
        {
            if(maxSum < sum[i][j])
            {
                maxSum = sum[i][j];
                max_row = i;
                max_col = j;
            }
        }
    }
    //Printing the square maximum submatrix
    cout<<"Maximum size square sub-matrix is: \n";
    for(i = max_row; i > max_row - maxSum; i--)
    {
        for(j = max_col; j > max_col - maxSum; j--)
        {
            cout << matrix[i][j] << " ";
        }
        cout << "\n";
    }
}
int main()
{
    int matrix[R][C] = {{0, 1, 1, 0, 1},
                    {1, 1, 1, 1, 0},
                    {1, 1, 1, 1, 0},
                    {1, 1, 1, 1, 0},
                    {1, 1, 1, 1, 1},
                    {0, 0, 0, 0, 0}};
    printMaxSubMatrix(matrix);
}
You can also try this code with Online C++ Compiler
Run Code

Output :

Maximum size square sub-matrix:
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
You can also try this code with Online C++ Compiler
Run Code

The program to find square maximum submatrix of all 1’s has time complexity O(m*n) and space complexity is also O(m*n). Here m represents the number of rows and n number of columns, respectively.

Also read, Application of Oops

Method 2

We can optimize our solution of finding the square maximum submatrix by reducing the space complexity. We need only the current row and previous row to compute an entry in the matrix.

#include <bits/stdc++.h>
using namespace std;
#define R 6
#define C 5

void printMaxSubMatrix(int M[R][C])
{
    int sum[2][C], MaxValue = 0;
    memset(sum, 0, sizeof(sum));    //Initializing sum matrix with 0

    // Construct the entries
    for (int i = 0; i < R;i++)
        for (int j = 0; j < C;j++){
            //Current entry computation
            int entry = M[i][j];
            if(entry)
                if(j)
                    entry = 1 + min(sum[1][j - 1], min(sum[0][j - 1], sum[1][j]));

            // Save last entry and add new entry
            sum[0][j] = sum[1][j];
            sum[1][j] = entry;

            //Track for maximum length
            MaxValue = max(MaxValue, entry);
        }

    //Printing the square maximum submatrix
    cout << "Maximum size square sub-matrix: \n";
    for (int i = 0; i < MaxValue; i++)
    {
        for (int j = 0; j < MaxValue;j++)
            cout << "1 ";
        cout <<endl;
    }
}

int main ()
{
    int matrix[R][C] = {{0, 1, 1, 0, 1},
                    {1, 1, 1, 1, 0},
                    {1, 1, 1, 1, 0},
                    {1, 1, 1, 1, 0},
                    {1, 1, 1, 1, 1},
                    {0, 0, 0, 0, 0}};
    printMaxSubMatrix(matrix);
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Maximum size square sub-matrix:
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
You can also try this code with Online C++ Compiler
Run Code

Time complexity is the same as the previous code to find maximum submatrix, i.e., O(m*n), but space complexity is reduced to O(n). Here n is the number of columns and m number of rows, respectively.

Try and compile by yourself with the help of online C++ Compiler for better understanding.

Check out this problem - Two Sum Problem

FAQs

  1. What does the memset function do?
    Memset function is used to initialize the sum matrix with all entries as 0 in the program to find the square maximum submatrix of all 1’s.
     
  2. What is a binary matrix?
    A binary matrix is a two dimensional array with entries as 0 or 1 only.

Key Takeaways

In this article, we have extensively discussed the methods to find a maximum square submatrix with all 1's from a given matrix and its implementation in C++. The blog contains the algorithm to find the maximum submatrix of all 1’s and different approaches to achieve the result.

We hope that this blog has helped you enhance your knowledge regarding maximum submatrix and if you would like to learn more, check out our articles on the Maximum Area of the Square Submatrix in C++. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass