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 the Bin Packing Algorithm
Tránh lỗi NullPointerException trong Java như thế nào?
Setting the Java Version in Maven
Understanding Memory Leaks in Java
Guide to Java Instrumentation
Count Occurrences of a Char in a String
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
ExecutorService – Waiting for Threads to Finish
Java Program to Implement Sparse Array
Registration – Password Strength and Rules
Java Program to Find Nearest Neighbor for Static Data Set
Concurrent Test Execution in Spring 5
Java – Convert File to InputStream
Java Program to Implement Jarvis Algorithm
A Guide To UDP In Java
Spring Boot - Google Cloud Platform
Guide to Spring 5 WebFlux
Spring Web Annotations
Java Program to Implement Floyd Cycle Algorithm
Java Program to find the number of occurrences of a given number using Binary Search approach
Chuyển đổi giữa các kiểu dữ liệu trong Java
Hướng dẫn Java Design Pattern – Flyweight
Java Program to Generate Randomized Sequence of Given Range of Numbers
Java Program to Implement Binary Heap
Java Program to Implement Counting Sort
Introduction to Spring Data REST
Java Program to Perform Addition Operation Using Bitwise Operators
Java Stream Filter with Lambda Expression
Spring Boot - Cloud Configuration Server
Spring Boot - Scheduling
Java Program to Implement Floyd-Warshall Algorithm