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:

Java Program to Implement Park-Miller Random Number Generation Algorithm
Introduction to Spliterator in Java
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Spring Autowiring of Generic Types
Converting String to Stream of chars
How to Change the Default Port in Spring Boot
Java Program to Implement AVL Tree
Convert Character Array to String in Java
Hướng dẫn Java Design Pattern – DAO
How to Find an Element in a List with Java
Java Program to Implement Variable length array
Handling URL Encoded Form Data in Spring REST
Java Program to Implement Ford–Fulkerson Algorithm
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Java Program to Represent Graph Using Incidence Matrix
Java Program to Implement Strassen Algorithm
Java Program to Implement LinkedHashMap API
Java Deep Learning Essentials - Yusuke Sugomori
Configuring a DataSource Programmatically in Spring Boot
Java Program to Implement Unrolled Linked List
Java Program to Implement ArrayBlockingQueue API
Java Program to Implement HashMap API
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Extract network card address
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Spring Cloud – Securing Services
Servlet 3 Async Support with Spring MVC and Spring Security
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Java Program to Check whether Graph is a Bipartite using BFS
Java Program to implement Bit Set
Guide to System.gc()
Hướng dẫn Java Design Pattern – Facade