Algorithm: Let the given binary matrix be M[R][C]. The idea of the algorithm is to construct an auxiliary size matrix S[][] in which each entry S[i][j] represents the size of the square sub-matrix with all 1s including M[i][j] where M[i][j] is the rightmost and bottom-most entry in sub-matrix.
1) Construct a sum matrix S[R][C] for the given M[R][C].
a) Copy first row and first columns as it is from M[][] to S[][]
b) For other entries, use following expressions to construct S[][]
If M[i][j] is 1 then
S[i][j] = min(S[i][j-1], S[i-1][j], S[i-1][j-1]) + 1
Else /*If M[i][j] is 0*/
S[i][j] = 0
2) Find the maximum entry in S[R][C]
3) Using the value and coordinates of maximum entry in S[i], print
sub-matrix of M[][]
For the given M[R][C] in the above example, constructed S[R][C] would be:
The value of the maximum entry in the above matrix is 3 and the coordinates of the entry are (4, 3). Using the maximum value and its coordinates, we can find out the required sub-matrix.
C++
// C++ code for Maximum size square
// sub-matrix with all 1s
#include <bits/stdc++.h>
#define bool int
#define R 6
#define C 5
usingnamespacestd;
voidprintMaxSubSquare(boolM[R][C])
{
inti,j;
intS[R][C];
intmax_of_s, max_i, max_j;
/* Set first column of S[][]*/
for(i = 0; i < R; i++)
S[i][0] = M[i][0];
/* Set first row of S[][]*/
for(j = 0; j < C; j++)
S[0][j] = M[0][j];
/* Construct other entries of S[][]*/
for(i = 1; i < R; i++)
{
for(j = 1; j < C; j++)
{
if(M[i][j] == 1)
S[i][j] = min({S[i][j-1], S[i-1][j],
S[i-1][j-1]}) + 1; //better of using min in case of arguments more than 2
else
S[i][j] = 0;
}
}
/* Find the maximum entry, and indexes of maximum entry
in S[][] */
max_of_s = S[0][0]; max_i = 0; max_j = 0;
for(i = 0; i < R; i++)
{
for(j = 0; j < C; j++)
{
if(max_of_s < S[i][j])
{
max_of_s = S[i][j];
max_i = i;
max_j = j;
}
}
}
cout<<"Maximum size sub-matrix is: \n";
for(i = max_i; i > max_i - max_of_s; i--)
{
for(j = max_j; j > max_j - max_of_s; j--)
{
cout << M[i][j] << " ";
}
cout << "\n";
}
}
/* Driver code */
intmain()
{
boolM[R][C] = {{0, 1, 1, 0, 1},
{1, 1, 0, 1, 0},
{0, 1, 1, 1, 0},
{1, 1, 1, 1, 0},
{1, 1, 1, 1, 1},
{0, 0, 0, 0, 0}};
printMaxSubSquare(M);
}
// This code is contributed by rathbhupendra
C
// C code for Maximum size square
// sub-matrix with all 1s
#include<stdio.h>
#define bool int
#define R 6
#define C 5
voidprintMaxSubSquare(boolM[R][C])
{
inti,j;
intS[R][C];
intmax_of_s, max_i, max_j;
/* Set first column of S[][]*/
for(i = 0; i < R; i++)
S[i][0] = M[i][0];
/* Set first row of S[][]*/
for(j = 0; j < C; j++)
S[0][j] = M[0][j];
/* Construct other entries of S[][]*/
for(i = 1; i < R; i++)
{
for(j = 1; j < C; j++)
{
if(M[i][j] == 1)
S[i][j] = min(S[i][j-1], S[i-1][j],
S[i-1][j-1]) + 1;
else
S[i][j] = 0;
}
}
/* Find the maximum entry, and indexes of maximum entry
in S[][] */
max_of_s = S[0][0]; max_i = 0; max_j = 0;
for(i = 0; i < R; i++)
{
for(j = 0; j < C; j++)
{
if(max_of_s < S[i][j])
{
max_of_s = S[i][j];
max_i = i;
max_j = j;
}
}
}
printf("Maximum size sub-matrix is: \n");
for(i = max_i; i > max_i - max_of_s; i--)
{
for(j = max_j; j > max_j - max_of_s; j--)
{
printf("%d ", M[i][j]);
}
printf("\n");
}
}
/* UTILITY FUNCTIONS */
/* Function to get minimum of three values */
intmin(inta, intb, intc)
{
intm = a;
if(m > b)
m = b;
if(m > c)
m = c;
returnm;
}
/* Driver function to test above functions */
intmain()
{
boolM[R][C] = {{0, 1, 1, 0, 1},
{1, 1, 0, 1, 0},
{0, 1, 1, 1, 0},
{1, 1, 1, 1, 0},
{1, 1, 1, 1, 1},
{0, 0, 0, 0, 0}};
printMaxSubSquare(M);
getchar();
}
Java
// JAVA Code for Maximum size square
// sub-matrix with all 1s
publicclassGFG
{
// method for Maximum size square sub-matrix with all 1s
staticvoidprintMaxSubSquare(intM[][])
{
inti,j;
intR = M.length; //no of rows in M[][]
intC = M[0].length; //no of columns in M[][]
intS[][] = newint[R][C];
intmax_of_s, max_i, max_j;
/* Set first column of S[][]*/
for(i = 0; i < R; i++)
S[i][0] = M[i][0];
/* Set first row of S[][]*/
for(j = 0; j < C; j++)
S[0][j] = M[0][j];
/* Construct other entries of S[][]*/
for(i = 1; i < R; i++)
{
for(j = 1; j < C; j++)
{
if(M[i][j] == 1)
S[i][j] = Math.min(S[i][j-1],
Math.min(S[i-1][j], S[i-1][j-1])) + 1;
else
S[i][j] = 0;
}
}
/* Find the maximum entry, and indexes of maximum entry
in S[][] */
max_of_s = S[0][0]; max_i = 0; max_j = 0;
for(i = 0; i < R; i++)
{
for(j = 0; j < C; j++)
{
if(max_of_s < S[i][j])
{
max_of_s = S[i][j];
max_i = i;
max_j = j;
}
}
}
System.out.println("Maximum size sub-matrix is: ");
for(i = max_i; i > max_i - max_of_s; i--)
{
for(j = max_j; j > max_j - max_of_s; j--)
{
System.out.print(M[i][j] + " ");
}
System.out.println();
}
}
// Driver program
publicstaticvoidmain(String[] args)
{
intM[][] = {{0, 1, 1, 0, 1},
{1, 1, 0, 1, 0},
{0, 1, 1, 1, 0},
{1, 1, 1, 1, 0},
{1, 1, 1, 1, 1},
{0, 0, 0, 0, 0}};
printMaxSubSquare(M);
}
}
Python3
# Python3 code for Maximum size
# square sub-matrix with all 1s
defprintMaxSubSquare(M):
R =len(M) # no. of rows in M[][]
C =len(M[0]) # no. of columns in M[][]
S =[]
fori inrange(R):
temp =[]
forj inrange(C):
ifi==0orj==0:
temp +=M[i][j],
else:
temp +=0,
S +=temp,
# here we have set the first row and first column of S same as input matrix, other entries are set to 0
# Update other entries
fori inrange(1, R):
forj inrange(1, C):
if(M[i][j] ==1):
S[i][j] =min(S[i][j-1], S[i-1][j],
S[i-1][j-1]) +1
else:
S[i][j] =0
# Find the maximum entry and
# indices of maximum entry in S[][]
max_of_s =S[0][0]
max_i =0
max_j =0
fori inrange(R):
forj inrange(C):
if(max_of_s < S[i][j]):
max_of_s =S[i][j]
max_i =i
max_j =j
print("Maximum size sub-matrix is: ")
fori inrange(max_i, max_i -max_of_s, -1):
forj inrange(max_j, max_j -max_of_s, -1):
print(M[i][j], end =" ")
print("")
# Driver Program
M =[[0, 1, 1, 0, 1],
[1, 1, 0, 1, 0],
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 0]]
printMaxSubSquare(M)
# This code is contributed by Soumen Ghosh
C#
// C# Code for Maximum size square
// sub-matrix with all 1s
usingSystem;
publicclassGFG
{
// method for Maximum size square sub-matrix with all 1s
staticvoidprintMaxSubSquare(int[,]M)
{
inti,j;
//no of rows in M[,]
intR = M.GetLength(0);
//no of columns in M[,]
intC = M.GetLength(1);
int[,]S = newint[R,C];
intmax_of_s, max_i, max_j;
/* Set first column of S[,]*/
for(i = 0; i < R; i++)
S[i,0] = M[i,0];
/* Set first row of S[][]*/
for(j = 0; j < C; j++)
S[0,j] = M[0,j];
/* Construct other entries of S[,]*/
for(i = 1; i < R; i++)
{
for(j = 1; j < C; j++)
{
if(M[i,j] == 1)
S[i,j] = Math.Min(S[i,j-1],
Math.Min(S[i-1,j], S[i-1,j-1])) + 1;
else
S[i,j] = 0;
}
}
/* Find the maximum entry, and indexes of
maximum entry in S[,] */
max_of_s = S[0,0]; max_i = 0; max_j = 0;
for(i = 0; i < R; i++)
{
for(j = 0; j < C; j++)
{
if(max_of_s < S[i,j])
{
max_of_s = S[i,j];
max_i = i;
max_j = j;
}
}
}
Console.WriteLine("Maximum size sub-matrix is: ");
for(i = max_i; i > max_i - max_of_s; i--)
{
for(j = max_j; j > max_j - max_of_s; j--)
{
Console.Write(M[i,j] + " ");
}
Console.WriteLine();
}
}
// Driver program
publicstaticvoidMain()
{
int[,]M = newint[6,5]{{0, 1, 1, 0, 1},
{1, 1, 0, 1, 0},
{0, 1, 1, 1, 0},
{1, 1, 1, 1, 0},
{1, 1, 1, 1, 1},
{0, 0, 0, 0, 0}};
printMaxSubSquare(M);
}
}
PHP
<?php
// PHP code for Maximum size square
// sub-matrix with all 1s
functionprintMaxSubSquare($M, $R, $C)
{
$S= array(array()) ;
/* Set first column of S[][]*/
for($i= 0; $i< $R; $i++)
$S[$i][0] = $M[$i][0];
/* Set first row of S[][]*/
for($j= 0; $j< $C; $j++)
$S[0][$j] = $M[0][$j];
/* Construct other entries of S[][]*/
for($i= 1; $i< $R; $i++)
{
for($j= 1; $j< $C; $j++)
{
if($M[$i][$j] == 1)
$S[$i][$j] = min($S[$i][$j- 1],
$S[$i- 1][$j],
$S[$i- 1][$j- 1]) + 1;
else
$S[$i][$j] = 0;
}
}
/* Find the maximum entry, and indexes
of maximum entry in S[][] */
$max_of_s= $S[0][0];
$max_i= 0;
$max_j= 0;
for($i= 0; $i< $R; $i++)
{
for($j= 0; $j< $C; $j++)
{
if($max_of_s< $S[$i][$j])
{
$max_of_s= $S[$i][$j];
$max_i= $i;
$max_j= $j;
}
}
}
printf("Maximum size sub-matrix is: \n");
for($i= $max_i;
$i> $max_i- $max_of_s; $i--)
{
for($j= $max_j;
$j> $max_j- $max_of_s; $j--)
{
echo$M[$i][$j], " ";
}
echo"\n";
}
}
# Driver code
$M= array(array(0, 1, 1, 0, 1),
array(1, 1, 0, 1, 0),
array(0, 1, 1, 1, 0),
array(1, 1, 1, 1, 0),
array(1, 1, 1, 1, 1),
array(0, 0, 0, 0, 0));
$R= 6 ;
$C= 5 ;
printMaxSubSquare($M, $R, $C);
// This code is contributed by Ryuga
?>
Javascript
<script>
// JavaScript code for Maximum size square
// sub-matrix with all 1s
let R = 6;
let C = 5;
functionprintMaxSubSquare(M) {
let i,j;
let S = [];
for( vary = 0; y < R; y++ ) {
S[ y ] = [];
for( varx = 0; x < C; x++ ) {
S[ y ][ x ] = 0;
}
}
let max_of_s, max_i, max_j;
/* Set first column of S[][]*/
for(i = 0; i < R; i++)
S[i][0] = M[i][0];
/* Set first row of S[][]*/
for(j = 0; j < C; j++)
S[0][j] = M[0][j];
/* Construct other entries of S[][]*/
for(i = 1; i < R; i++)
{
for(j = 1; j < C; j++)
{
if(M[i][j] == 1)
S[i][j] = Math.min(S[i][j-1],Math.min( S[i-1][j],
S[i-1][j-1])) + 1;
else
S[i][j] = 0;
}
}
/* Find the maximum entry, and indexes of maximum entry
in S[][] */
max_of_s = S[0][0]; max_i = 0; max_j = 0;
for(i = 0; i < R; i++)
{
for(j = 0; j < C; j++)
{
if(max_of_s < S[i][j])
{
max_of_s = S[i][j];
max_i = i;
max_j = j;
}
}
}
document.write("Maximum size sub-matrix is: <br>");
for(i = max_i; i > max_i - max_of_s; i--)
{
for(j = max_j; j > max_j - max_of_s; j--)
{
document.write( M[i][j] , " ");
}
document.write("<br>");
}
}
/* Driver code */
let M = [[0, 1, 1, 0, 1],
[1, 1, 0, 1, 0],
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 0]];
printMaxSubSquare(M);
</script>
Output:
Maximum size sub-matrix is:
1 1 1
1 1 1
1 1 1
Time Complexity: O(m*n) where m is the number of rows and n is the number of columns in the given matrix. Auxiliary Space: O(m*n) where m is the number of rows and n is the number of columns in the given matrix. Algorithmic Paradigm: Dynamic Programming
Space Optimized Solution: In order to compute an entry at any position in the matrix we only need the current row and the previous row.
Time Complexity: O(m*n) where m is the number of rows and n is the number of columns in the given matrix. Auxiliary space: O(n) where n is the number of columns in the given matrix.
Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy