Tuesday, January 10, 2012

Modify Boolean MATRIX

Given a boolean matrix mat[M][N] of size M X N, modify it such that if a matrix cell mat[i][j] is 1 (or true) then make all the cells of ith row and jth column as 1.

Method 1:Using Extra Memory
1) Create two temporary arrays row[M] and col[N]. Initialize all values of row[] and col[] as 0.
2) Traverse the input matrix mat[M][N]. If you see an entry mat[i][j] as true, then mark row[i] and col[j] as true.
3) Traverse the input matrix mat[M][N] again. For each entry mat[i][j], check the values of row[i] and col[j]. If any of the two values (row[i] or col[j]) is true, then mark mat[i][j] as true.

http://www.geeksforgeeks.org/a-boolean-matrix-question/

Method 2:Using First row and First Column:
This method uses the first row and first column of the input matrix in place of the auxiliary arrays row[] and col[] of method 1. So what we do is: first take care of first row and column and store the info about these two in two flag variables rowFlag and colFlag. Once we have this info, we can use first row and first column as auxiliary arrays and apply method 1 for submatrix (matrix excluding first row and first column) of size (M-1)*(N-1).
1) Scan the first row and set a variable rowFlag to indicate whether we need to set all 1s in first row or not.
2) Scan the first column and set a variable colFlag to indicate whether we need to set all 1s in first column or not.
3) Use first row and first column as the auxiliary arrays row[] and col[] respectively, consider the matrix as submatrix starting from second row and second column and apply method 1.
4) Finally, using rowFlag and colFlag, update first row and first column if needed.

http://www.programcreek.com/2012/12/leetcode-set-matrix-zeroes-java/
http://yucoding.blogspot.in/2013/04/leetcode-question-97-set-matrix-zeros.html

Code:
void flip(int M[][COL])
{
        int i, j, r, c, flagRow, flagCol;

        for( r = 0, flagCol = 0; r < ROW; ++r )
                flagCol |= M[r][0];

        for( c = 0, flagRow = 0; c < COL; ++c )
                flagRow |= M[0][c];

        for( r = 1; r < ROW; ++r )
                for( c = 1; c < COL; ++c )
                {
                        M[0][c] |= M[r][c];
                        M[r][0] |= M[r][c];
                }

        for( r = 1; r < ROW; ++r )
                for( c = 1; c < COL; ++c )
                        if( M[0][c] || M[r][0] )
                                M[r][c] = 1;

        if( flagRow )
                for( c = 0; c < COL; ++c )
                        M[0][c] = 1;
       
        if( flagCol )
                for( r = 0; r < ROW; ++r )
                        M[r][0] = 1;
        printM(M);
}
Time Complexity: O(M*N)
Auxiliary Space: O(1)

Method 3:No extra memory and with no overhead
#include <stdio.h>
#include <limits.h>
inline void clear(const int m, const int n, int a[][n], const int row, const int col)
{
    int k;
    for(k = 0; k < m; k++)
if(a[k][col] != 0)
           a[k][col] = INT_MAX;

    for(k = 0; k < n; k++)
        if(a[row][k] != 0)
           a[row][k] = INT_MAX;

}

inline void print(const int m, const int n, int a[][n])
{
    int i, j;
    for(i = 0; i < m; i++)
    {
        for(j = 0; j < n; j++)
            printf("%d ", a[i][j]);

        printf("\n");
    }
}

void setRowCol(const int m, const int n, int a[][n])
{
    int i = 0, j = 0, k;

    for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
            if(a[i][j] == 0)
            {
                clear(m, n, a, i, j);
            }

    for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
            if(a[i][j] == INT_MAX)
            {
                a[i][j] = 0;
            }
}

int main()
{
    int a[][3] = { {2, 0, 4},
                  {1 ,2, 3},
                  {3, 5, 0},
                  {4, 5, 6}
                };
    setRowCol(4, 3, a);
    print(4, 3, a);
}

No comments:

Post a Comment