Dynamic Programming — Maximum size square sub-matrix with all 1s from a matrix




  1. How to fill the auxiliary matrix?
  • Create an auxiliary array of the same size as given input array. We will fill the auxiliary array with Maximum size square sub-matrix with all 1’s possible with respect to the particularity cell.
  • Once the auxiliary is fill, scan it and find out the maximum entry in it, this will the size of Maximum size square sub-matrix with all 1’s in the given matrix.
  • Copy the first row and first col­umn from the given array to auxiliary array.
  • For filling rest of cells, check if particularity cell’s value is 0 in given array, if yes then put 0 against to that cell in auxiliary array.
  • check if particularity cell’s value is 1 in given array, if yes then put Minimum (value in the left cell, top cell and left-top diagonal cell) + 1 in auxiliary cell.
  1. Logic of auxiliary matrix?
    Where the position of 1 is matched then go to
    minimum of this position data by using and adding plus 1
    auxiliaryMatrix[i][j] = min(auxiliaryMatrix[i - 1][j - 1],auxiliaryMatrix[i][j - 1],auxiliaryMatrix[i - 1][j]) + 1;
  1. 0
    1
    0
    1
    0
    1
    1
    0
    1
    0
    1
    0
    0
    1
    1
    1
    1
    0
    0
    0
    1
    1
    1
    0
    1
    1
    1
    1
    1
    1
    Base Matrix
    0
    1
    0
    1
    0
    1
    1
    0
    1
    0
    1
    0
    0
    1
    1
    1
    1
    0
    0
    0
    1
    1
    1
    0
    1
    1
    1
    1
    1
    1
    Out Put Matrix of color red
    0
    1
    0
    1
    0
    1
    1
    0
    1
    0
    1
    0
    0
    1
    1
    1
    1
    0
    0
    0
    1
    2
    2
    0
    1
    1
    1
    2
    3
    1
    Auxiliary Matrix
    0
    1
    0
    1
    0
    1
    1
    0
    1
    0
    1
    0
    0
    1
    1
    1
    1
    0
    0
    0
    1
    2
    2
    0
    1
    1
    1
    2
    3
    1
    Auxiliary Matrix sub set select

package com.kartik.matrix;
public class MaxSquareSubMatrix { /** * @param matrixInput *            arrA * @param row *            row * @param cols *            cols */ public void displayMatrix(int[][] matrixInput, int row, int cols) { for (int i = 0; i < row; i++) { for (int j = 0; j < cols; j++) { System.out.print(matrixInput[i][j] + " "); } System.out.println(); } } /** * @purpose This is find out the Square sub matrix  * @param baseMatrixInput *            arrA * @param row *            row * @param cols *            cols */ public void subMatrix(int[][] baseMatrixInput, int row, int cols) { System.out.println("Display base of matrix"); displayMatrix(baseMatrixInput, row, cols); int maxSizeOfMatrix, maxRow, maxCol; int[][] auxiliaryMatrix = new int[row][cols]; // copy the first row for (int i = 0; i < cols; i++) { auxiliaryMatrix[0][i] = baseMatrixInput[0][i]; } // copy the first column for (int i = 0; i < row; i++) { auxiliaryMatrix[i][0] = baseMatrixInput[i][0]; } // for rest of the matrix check if arrA[i][j]==1 System.out.println("----------Start---------------"); createAuxiliaryMatrix(baseMatrixInput, row, cols, auxiliaryMatrix); System.out.println("----------End---------------"); System.out.println("Display auxiliary matrix"); displayMatrix(auxiliaryMatrix, row, cols); // find the maximum size of sub matrix maxSizeOfMatrix = auxiliaryMatrix[0][0]; maxRow = 0; maxCol = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < cols; j++) { if (auxiliaryMatrix[i][j] > maxSizeOfMatrix) { maxSizeOfMatrix = auxiliaryMatrix[i][j]; maxRow = i; maxCol = j; } } } System.out.println("Maximum size square sub-matrix with all 1s: " + maxSizeOfMatrix); System.out.println("Display of the sub matrix"); displaySubMatrix(maxSizeOfMatrix, maxRow, maxCol, auxiliaryMatrix); System.out.println("Display of the sub matrix position"); displayPositionOfSubMatrix(maxSizeOfMatrix, maxRow, maxCol); } /** * @param baseMatrixInput * @param row * @param cols * @param auxiliaryMatrix */ private void createAuxiliaryMatrix(int[][] baseMatrixInput, int row, int cols, int[][] auxiliaryMatrix) { for (int i = 1; i < row; i++) { for (int j = 1; j < cols; j++) { if (baseMatrixInput[i][j] == 1) { // +1 is for increment because new formating of matrix like // [[0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0], [0, 1, 1, 1, 1, // 0], [0, 0, 1, 2, 2, 0], [1, 1, 1, 2, 3, 1]] //create your own minimum function  or use Math.min function like this  /*auxiliaryMatrix[i][j] = Math.min( auxiliaryMatrix[i - 1][j - 1], Math.min( auxiliaryMatrix[i][j - 1], auxiliaryMatrix[i - 1][j])) + 1;*/ System.out.println(i+""+j+"-->"+(i - 1)+""+(j - 1)+" , "+i+""+(j-1)+" , "+(i-1)+""+j); auxiliaryMatrix[i][j] = min(auxiliaryMatrix[i - 1][j - 1],auxiliaryMatrix[i][j - 1],auxiliaryMatrix[i - 1][j]) + 1; } else { auxiliaryMatrix[i][j] = 0; } } } } /** * @purpose This is display of matrix data in reverser order * @param maxSizeOfMatrix * @param maxRow * @param maxCol * @param auxiliaryMatrix */ private void displaySubMatrix(int maxSizeOfMatrix, int maxRow, int maxCol, int[][] auxiliaryMatrix) { for (int i = maxRow; i > maxRow - maxSizeOfMatrix; i--) { for (int j = maxCol; j > maxCol - maxSizeOfMatrix; j--) { System.out.print(auxiliaryMatrix[i][j] + " "); } System.out.println(); } } /** * @purpose This is display of matrix position where this matrix generated *          in reverse order * @param maxSizeOfMatrix * @param maxRow * @param maxCol */ private void displayPositionOfSubMatrix(int maxSizeOfMatrix, int maxRow, int maxCol) { for (int i = maxRow; i > maxRow - maxSizeOfMatrix; i--) { for (int j = maxCol; j > maxCol - maxSizeOfMatrix; j--) { System.out.print(i + "" + j + ","); } System.out.println(); } } /** * @param a * @param b * @param c * @return */ int min(int a, int b, int c) {  int m = a;  if (m > b)     m = b;  if (m > c)     m = c;  return m; } public static void main(String[] args) { int[][] twoDimensionalMatrix = { { 0, 1, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1, 0 }, { 0, 1, 1, 1, 1, 0 }, { 0, 0, 1, 1, 1, 0 }, { 1, 1, 1, 1, 1, 1 } }; /*int[][] twoDimensionalMatrix = { {0 , 1 , 1,  0,  1  }, { 1 , 1 , 0 , 1 , 0 }, { 0,  1  ,0, 1,  0 }, { 1 , 1,  1 , 1 , 0 }, { 1 , 1 , 1,  1 , 1 },{0 , 1 , 1, 1 , 1} };*/ MaxSquareSubMatrix baseMatrix = new MaxSquareSubMatrix(); baseMatrix.subMatrix(twoDimensionalMatrix, 5, 6); } }
Out put:
Display base of matrix
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 1 1 1 0 
0 0 1 1 1 0 
1 1 1 1 1 1 
----------Start---------------
12-->01 , 11 , 02
14-->03 , 13 , 04
21-->10 , 20 , 11
22-->11 , 21 , 12
23-->12 , 22 , 13
24-->13 , 23 , 14
32-->21 , 31 , 22
33-->22 , 32 , 23
34-->23 , 33 , 24
41-->30 , 40 , 31
42-->31 , 41 , 32
43-->32 , 42 , 33
44-->33 , 43 , 34
45-->34 , 44 , 35
----------End---------------
Display auxiliary matrix
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 1 1 1 0 
0 0 1 2 2 0 
1 1 1 2 3 1 
Maximum size square sub-matrix with all 1s: 3
Display of the sub matrix
3 2 1 
2 2 1 
1 1 1 
Display of the sub matrix position
44,43,42,
34,33,32,
24,23,22,


Example:




Previous
Next Post »