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 Find the Longest Path in a DAG
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Spring Boot - Application Properties
Guide to Java Instrumentation
Java Program to Implement PrinterStateReasons API
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Guide to Guava Multimap
Guide to java.util.concurrent.Future
SOAP Web service: Authentication trong JAX-WS
Java Program to Evaluate an Expression using Stacks
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Setting Up Swagger 2 with a Spring REST API
Programmatic Transaction Management in Spring
Getting Started with Custom Deserialization in Jackson
Java Stream Filter with Lambda Expression
Spring Boot - Creating Docker Image
Java Program to Implement Max Heap
Java Program to Perform Insertion in a BST
Giới thiệu java.io.tmpdir
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
New Features in Java 11
Validations for Enum Types
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Guide to PriorityBlockingQueue in Java
Java Program to Implement Bloom Filter
Properties with Spring and Spring Boot
Java Program to Implement Ternary Search Tree
Java Program to Implement Queue using Two Stacks
Netflix Archaius with Various Database Configurations
Spring Boot - Rest Controller Unit Test
Mệnh đề if-else trong java