Java Program to Check if it is a Sparse Matrix

This is the java program to find out a given matrix is sparse matrix or not. Sparse matrix contains zero elements above a certain threshold. This threshold is given by (n*m)/2, where n and m are the rows and columns in matrix. Hence, if a matrix contains more than nm/2 mumber of zeros it is sparse matrix otherwise not.

Here is the source code of the Java Program to Check if it is a Sparse Matrix. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a sample program to check whether the matrix is sparse matrix or not
  2. //The complexity of the code is O(n^2)
  3. import java.util.Scanner;
  4.  
  5. public class Sparsity_Matrix 
  6. {
  7.     public static void main(String args[])
  8.     {
  9.         Scanner sc = new Scanner(System.in);
  10.         System.out.println("Enter the dimensions of the matrix: ");
  11.         int m = sc.nextInt();
  12.         int n = sc.nextInt();
  13.         double[][] mat = new double[m][n];
  14.         int zeros = 0;
  15.         System.out.println("Enter the elements of the matrix: ");
  16.         for(int i=0; i<m; i++)
  17.         {
  18.             for(int j=0; j<n; j++)
  19.             {
  20.                 mat[i][j] = sc.nextDouble();
  21.                 if(mat[i][j] == 0)
  22.                 {
  23.                     zeros++;
  24.                 }
  25.             }
  26.         }
  27.  
  28.         if(zeros > (m*n)/2)
  29.         {
  30.             System.out.println("The matrix is a sparse matrix");
  31.         }
  32.         else
  33.         {
  34.             System.out.println("The matrix is not a sparse matrix");
  35.         }
  36.  
  37.         sc.close();
  38.     }
  39. }

Output:

$ javac Sparsity_matrix.java
$ java Sparsity_matrix
Enter the dimensions of the matrix: 
2 3
Enter the elements of the matrix: 
1 0 0
2 1 1
The matrix is not a sparse matrix
 
$ javac Sparsity_matrix.java
$ java Sparsity_matrix
Enter the dimensions of the matrix: 
3 4
Enter the elements of the matrix: 
1 0 0 0 
0 1 0 0
0 0 1 1
The matrix is a sparse matrix

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.