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:
An Intro to Spring Cloud Task
Convert Time to Milliseconds in Java
Quick Guide on Loading Initial Data with Spring Boot
Java Byte Array to InputStream
Guide to @ConfigurationProperties in Spring Boot
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
So sánh HashMap và HashSet trong Java
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Java Program to Implement HashSet API
Introduction to Spring Cloud OpenFeign
Introduction to Using Thymeleaf in Spring
Mảng (Array) trong Java
Compare Two JSON Objects with Jackson
Chương trình Java đầu tiên
Guide to the ConcurrentSkipListMap
Merging Streams in Java
Spring Webflux with Kotlin
Implementing a Runnable vs Extending a Thread
Java Program to Implement Queue using Two Stacks
Java Program to Implement CountMinSketch
Java Program to Perform Searching Using Self-Organizing Lists
Create a Custom Auto-Configuration with Spring Boot
Java Program to implement Array Deque
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Checked and Unchecked Exceptions in Java
How to Break from Java Stream forEach
Logging a Reactive Sequence
Reactive WebSockets with Spring 5
Jackson – JsonMappingException (No serializer found for class)
Java Program to Implement Karatsuba Multiplication Algorithm
Java Program to implement Priority Queue