Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program To Determine If a Given Matrix is a Sparse Matrix
What is a Sparse Matrix?
A matrix is said to be a sparse matrix if most of the elements of that matrix are 0. It means that the number of non-zero elements has a lesser count than the number of zero elements.
Let's say we have a matrix of size 3*3, which means it has 9 elements. If 5 or more of those elements are 0, then we can say that the matrix is sparse.
A = [ 0 2 0 ]
[ 0 5 4 ]
[ 0 0 0 ]
In this case, we have 6 elements that are 0, hence it is a sparse matrix.
Algorithm
To determine if a given matrix is a sparse matrix or not, we can follow these steps:
- Declare an integer matrix, namely input_matrix
- Define the values.
- Iterate over each element of the matrix using two for-loops, and count the number of elements that have the value 0.
- If the zero elements is greater than half the total elements, it's a sparse matrix, else it's not.
Java Program to Determine whether a Matrix is Sparse
Here, is the code to determine if a given matrix is a sparse matrix or not. We are taking more than half of the elements as 0 to make it a sparse matrix.
public class SparseMatrixOrNot{
public static void main(String[] args){
int[][] arr = {
{9,0,0},
{0,0,5},
{0,0,0}
};
int cnt0 = 0;
int totalEl = arr.length * arr[0].length;
System.out.println("The matrix is defined as:");
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + " ");
if(arr[i][j] == 0){
cnt0++;
}
}
System.out.println();
}
if(cnt0 > totalEl / 2){
System.out.println("\nYes, the matrix is a sparse matrix");
} else {
System.out.println("\nNo, the matrix is not a sparse matrix");
}
}
}
Output
Following is the output of the above code:
The matrix is defined as: 4 0 6 0 0 9 6 0 0 Yes, the matrix is a sparse matrix
Example 2
Now, take a matrix with more non-zero elements than zero elements and check if it is a sparse matrix or not.
public class SparseMatrixOrNot{
public static void main(String[] args){
int[][] arr = {
{1,0,3},
{0,5,0},
{7,8,9}
};
int cnt0 = 0;
int totalEl = arr.length * arr[0].length;
System.out.println("The matrix is defined as:");
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + " ");
if(arr[i][j] == 0){
cnt0++;
}
}
System.out.println();
}
if(cnt0 > totalEl / 2){
System.out.println("\nYes, the matrix is a sparse matrix");
} else{
System.out.println("\nNo, the matrix is not a sparse matrix, contains more non-zero elements than zero elements.");
}
}
}
Output
Following is the output of the above code:
The matrix is defined as: 1 0 3 0 5 0 7 8 9 No, the matrix is not a sparse matrix, it contains more non-zero elements than zero elements.