Java Program to determine whether a given matrix is a sparse matrix

In this program, we need to check whether the given matrix is a sparse matrix.

A matrix is said to be sparse matrix if most of the elements of that matrix are 0. It implies that it contains very less non-zero elements.

To check whether the given matrix is the sparse matrix or not, we first count the number of zero elements present in the matrix. Then calculate the size of the matrix. For the matrix to be sparse, count of zero elements present in an array must be greater than size/2.

Java Program to determine whether a given matrix is a sparse matrix

Number of zeroes present in above matrix is 6 and size of the matrix is 3 * 3 = 9. Since, 6 > 4.5 that means, most elements of given array are zero. Hence, the above matrix is a sparse matrix.

Algorithm

  • STEP 1: START
  • STEP 2: DEFINE rows, cols, size
  • STEP 3: SET count = 0
  • STEP 4: INITIALIZE first matrix a[][] ={{4,0,0}, {0,5,0}, {0,0,6}}
  • STEP 5: rows = a.length
  • STEP 6: cols = a[0].length
  • STEP 7: size = rows*cols
  • STEP 8: REPEAT STEP 9 to STEP 10 UNTIL i<rows
            //for(i=0;i<rows; i++)
  • STEP 9: REPEAT STEP 10 UNTIL j<cols
            //for(j=0;j<cols; j++)
  • STEP 10: if(a[i][j]==0) then count++
  • STEP 11: if(count>size/2) then PRINT “Yes” else PRINT “No”
  • STEP 12: END

Program

  1. public class SparseMatrix    
  2. {    
  3.     public static void main(String[] args) {    
  4.         int rows, cols, size, count = 0;    
  5.             
  6.         //Initialize matrix a    
  7.         int a[][] = {       
  8.                         {4, 0, 0},    
  9.                         {0, 5, 0},    
  10.                         {0, 0, 6}    
  11.                     };    
  12.               
  13.           //Calculates number of rows and columns present in given matrix    
  14.           rows = a.length;    
  15.         cols = a[0].length;    
  16.             
  17.         //Calculates the size of array    
  18.         size = rows * cols;    
  19.             
  20.         //Count all zero element present in matrix    
  21.         for(int i = 0; i < rows; i++){    
  22.             for(int j = 0; j < cols; j++){    
  23.                 if(a[i][j] == 0)    
  24.                     count++;    
  25.                 }    
  26.             }    
  27.                 
  28.         if(count > (size/2))    
  29.             System.out.println(“Given matrix is a sparse matrix”);    
  30.         else    
  31.             System.out.println(“Given matrix is not a sparse matrix”);    
  32.     }    
  33. }     

Output:

Given matrix is a sparse matrix

Leave a Comment