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 Check Whether an Undirected Graph Contains a Eulerian Path
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Represent Graph Using Adjacency List
Java Program to Implement SimpeBindings API
Spring Boot - Eureka Server
Java Program to Implement Floyd Cycle Algorithm
Java Program to Implement Circular Doubly Linked List
Guide to the ConcurrentSkipListMap
Java Program for Douglas-Peucker Algorithm Implementation
Java – Write an InputStream to a File
Introduction to PCollections
Java Program to Implement Regular Falsi Algorithm
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Consumer trong Java 8
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Jackson – Unmarshall to Collection/Array
Logout in an OAuth Secured Application
Checking for Empty or Blank Strings in Java
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Count Occurrences of a Char in a String
An Intro to Spring Cloud Vault
The Java 8 Stream API Tutorial
A Guide to Queries in Spring Data MongoDB
Simplify the DAO with Spring and Java Generics
Java Program to Implement JobStateReasons API
Introduction to Java Serialization
Spring Cloud AWS – EC2
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists