
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Maximum size square submatrix with all 1s
When a binary matrix is given, our task is to find a square matrix whose all elements are 1.
For this problem, we will make an auxiliary size matrix, whose order is the same as the given matrix. This size matrix will help to represent, in each entry Size[i, j], is the size of a square matrix with all 1s. From that size matrix, we will get the maximum number to get the size of the biggest square matrix.
Input and Output
Input: The binary matrix. 0 1 1 0 1 1 1 0 1 0 0 1 1 1 0 1 1 1 1 1 0 0 0 0 0 Output: The largest submatrix with all 1’s.
Algorithm
subMatWithOne(given matrix)
Input − The main matrix.
Output − Display the square matrix with all 1, which one is the largest.
Begin define subMat whose order is same as given matrix copy first row and first column of given matrix to subMat for all row i (1 to n), do for all column j (1 to n), do if matrix[i, j] = 1, then subMat[i, j] := 1 + minimum of subMat[i, j-1] and subMat[i-1, j-1] else subMat[i, j] := 0 done done maxSize := subMat[0, 0], iMax := 0 and jMax := 0 for all row i and column j, do if maxSize < subMat[i, j], then maxSize := subMat[i, j] iMax := i, jMax := j done print sub matrix from row = iMax to (iMax - maxSize), and column jMax to (jMax - maxSize) End
Example
#include<iostream> #define ROW 6 #define COL 5 using namespace std; int matrix[ROW][COL] = { {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} }; int min(int a, int b, int c) { return ((a<b?a:b))?((a<c)?a:c):((b<c)?b:c); } void subMatWithOne() { int subMat[ROW][COL]; int maxSize, iMax, jMax; for(int i = 0; i < ROW; i++) //copy first row of matrix to sub matrix subMat[i][0] = matrix[i][0]; for(int j = 0; j < COL; j++) //copy first column of matrix to sub matrix subMat[0][j] = matrix[0][j]; for(int i = 1; i < ROW; i++) { for(int j = 1; j < COL; j++) { if(matrix[i][j] == 1) //find minimum of left, top and diagonal element + 1 subMat[i][j] = min(subMat[i][j-1], subMat[i-1][j], subMat[i-1][j-1]) + 1; else subMat[i][j] = 0; //if item is 0, put only 0 } } maxSize = subMat[0][0]; iMax = 0; jMax = 0; for(int i = 0; i < ROW; i++) { //find the order of sub square matrix for(int j = 0; j < COL; j++) { if(maxSize < subMat[i][j]) { maxSize = subMat[i][j]; iMax = i; jMax = j; } } } cout << "Subsquare matrix: "<<endl; for(int i = iMax; i > iMax - maxSize; i--) { //print the submatrix using max size for(int j = jMax; j > jMax - maxSize; j--) { cout << matrix[i][j]<<" "; } cout << endl; } } int main() { subMatWithOne(); }
Output
Subsquare matrix: 1 1 1 1 1 1 1 1 1
- Related Articles
- Maximum size rectangle binary sub-matrix with all 1s in C++
- Maximum size rectangle binary sub-matrix with all 1s in C++ Program
- Finding the maximum square sub-matrix with all equal elements in C++
- Find the row with maximum number of 1s using C++
- Print maximum sum square sub-matrix of given size in C Program.
- Maximum consecutive 1s after n swaps in JavaScript
- Maximum size subset with given sum in C++
- Subsequence of size k with maximum possible GCD
- Program to count substrings with all 1s in binary string in Python
- Program to find largest submatrix with rearrangements in Python
- Maximum of all Subarrays of size k using set in C++ STL
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++ program
- JavaScript program for Size of the Subarray with Maximum Sum
- Count Square Submatrices with All Ones in C++

Advertisements