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 Concurrency Interview Questions and Answers
An Intro to Spring Cloud Contract
Java Program to Implement Gauss Seidel Method
Getting Started with Forms in Spring MVC
The DAO with Spring and Hibernate
Spring @RequestParam Annotation
HandlerAdapters in Spring MVC
Java Program to Implement Variable length array
A Guide to HashSet in Java
Spring Security and OpenID Connect
Giới thiệu thư viện Apache Commons Chain
Java Program to Implement Iterative Deepening
Introduction to Spring Data JDBC
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Java – Random Long, Float, Integer and Double
Tìm hiểu về Web Service
Spring Boot - Build Systems
Java Program to Implement Stack API
Java Program to Implement Tarjan Algorithm
Java Program to Implement Cartesian Tree
Adding Parameters to HttpClient Requests
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Hướng dẫn Java Design Pattern – Intercepting Filter
Format ZonedDateTime to String
Dockerizing a Spring Boot Application
String Joiner trong Java 8
Guide to Selenium with JUnit / TestNG
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Program to Implement Skew Heap
Java Program to Implement ArrayList API
Using the Not Operator in If Conditions in Java