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:
Từ khóa throw và throws trong Java
Java Program to Implement Ternary Heap
Guide to Dynamic Tests in Junit 5
Java Copy Constructor
A Guide to LinkedHashMap in Java
Java Program to Find Nearest Neighbor for Static Data Set
Java Program to Find Maximum Element in an Array using Binary Search
Java Program to Perform Stooge Sort
Java Program to Implement ArrayList API
Handling URL Encoded Form Data in Spring REST
Java Program to Use rand and srand Functions
Spring Boot - Interceptor
A Quick Guide to Spring Cloud Consul
Partition a List in Java
Spring Data MongoDB – Indexes, Annotations and Converters
Xây dựng ứng dụng Client-Server với Socket trong Java
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Spring Boot - Thymeleaf
Extract network card address
Default Password Encoder in Spring Security 5
Java Program to Check whether Graph is a Bipartite using DFS
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Exploring the Spring Boot TestRestTemplate
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Hướng dẫn sử dụng Java Generics
Using a List of Values in a JdbcTemplate IN Clause
Query Entities by Dates and Times with Spring Data JPA
Count Occurrences of a Char in a String
Spring Boot Security Auto-Configuration
Using the Not Operator in If Conditions in Java
Java – Delete a File
Java Program to Implement Adjacency List