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.

//This is a sample program to check whether the matrix is sparse matrix or not
//The complexity of the code is O(n^2)
import java.util.Scanner;
 
public class Sparsity_Matrix 
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the dimensions of the matrix: ");
        int m = sc.nextInt();
        int n = sc.nextInt();
        double[][] mat = new double[m][n];
        int zeros = 0;
        System.out.println("Enter the elements of the matrix: ");
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                mat[i][j] = sc.nextDouble();
                if(mat[i][j] == 0)
                {
                    zeros++;
                }
            }
        }
 
        if(zeros > (m*n)/2)
        {
            System.out.println("The matrix is a sparse matrix");
        }
        else
        {
            System.out.println("The matrix is not a sparse matrix");
        }
 
        sc.close();
    }
}

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

Related posts:

Function trong Java 8
JUnit5 @RunWith
Biểu thức Lambda trong Java 8 – Lambda Expressions
Spring Security Logout
Using Spring @ResponseStatus to Set HTTP Status Code
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Java Program to Implement Selection Sort
Stack Memory and Heap Space in Java
Spring Boot with Multiple SQL Import Files
TreeSet và sử dụng Comparable, Comparator trong java
Spring Boot - Unit Test Cases
Một số nguyên tắc, định luật trong lập trình
How to Read a Large File Efficiently with Java
Guide to @ConfigurationProperties in Spring Boot
Guide to Dynamic Tests in Junit 5
Java Program to Implement Min Heap
Toán tử trong java
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Tính kế thừa (Inheritance) trong java
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
ClassNotFoundException vs NoClassDefFoundError
Java – String to Reader
Reading an HTTP Response Body as a String in Java
Java Program to Represent Graph Using 2D Arrays
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Spring Security with Maven
Java Program to Find All Pairs Shortest Path
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
A Guide to the finalize Method in Java
The Difference Between Collection.stream().forEach() and Collection.forEach()
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm