forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparcity.java
More file actions
47 lines (44 loc) · 1.52 KB
/
Sparcity.java
File metadata and controls
47 lines (44 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package Misc;
import java.util.*;
/*
*A matrix is sparse if many of its coefficients are zero (In general if 2/3rd of matrix elements are 0, it is considered as sparse).
*The interest in sparsity arises because its exploitation can lead to enormous computational savings and because many large matrix problems that occur in practice are sparse.
*
* @author Ojasva Jain
*/
class Sparcity{
/*
* @return Sparcity of matrix
*
* where sparcity = number of zeroes/total elements in matrix
*
*/
static double sparcity(double [][] mat){
int zero =0;
//Traversing the matrix to count number of zeroes
for(int i=0;i<mat.length;i++){
for(int j=0;j<mat[i].length;j++){
if(mat[i][j]==0)
zero++;
}
}
//return sparcity
return ((double)zero/(mat.length*mat[1].length));
}
//Driver method
public static void main(String [] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter number of rows in matrix: ");
int n = in.nextInt();
System.out.println("Enter number of Columns in matrix: ");
int m = in.nextInt();
System.out.println("Enter Matrix elements: ");
double [][] mat = new double[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
mat[i][j] = in.nextDouble();
}
}
System.out.println("Sparcity of matrix is: "+ sparcity(mat));
}
}