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:
A Guide to JUnit 5 Extensions
Giới thiệu Google Guice – Injection, Scope
Java Program to Implement Merge Sort Algorithm on Linked List
Java Program to Implement Bubble Sort
Inheritance with Jackson
Lớp TreeMap trong Java
HttpAsyncClient Tutorial
Java Optional as Return Type
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Introduction to Spring Security Expressions
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
HTTP Authentification and CGI/Servlet
How to Get the Last Element of a Stream in Java?
A Guide to the ViewResolver in Spring MVC
Java Program to Implement Best-First Search
Redirect to Different Pages after Login with Spring Security
Cài đặt và sử dụng Swagger UI
Sử dụng CountDownLatch trong Java
Practical Java Examples of the Big O Notation
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Java Program to Check whether Directed Graph is Connected using DFS
Case-Insensitive String Matching in Java
Returning Image/Media Data with Spring MVC
Java Program to Implement Stack using Two Queues
Spring WebClient vs. RestTemplate
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Java Program to Implement Disjoint Sets
Functional Interface trong Java 8
Java Program to Generate Random Numbers Using Probability Distribution Function
Spring Boot - Exception Handling
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Implement Ternary Tree