Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
* [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
* [SparsityTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/SparsityTest.java)
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
* others
* [ArrayLeftRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java)
Expand Down
27 changes: 5 additions & 22 deletions src/main/java/com/thealgorithms/misc/Sparsity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.thealgorithms.misc;

import java.util.Scanner;

/*
*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
Expand All @@ -22,6 +20,10 @@ private Sparsity() {
*
*/
static double sparsity(double[][] mat) {
if (mat == null || mat.length == 0) {
throw new IllegalArgumentException("Matrix cannot be null or empty");
}

int zero = 0;
// Traversing the matrix to count number of zeroes
for (int i = 0; i < mat.length; i++) {
Expand All @@ -32,25 +34,6 @@ static double sparsity(double[][] mat) {
}
}
// return sparsity
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("Sparsity of matrix is: " + sparsity(mat));
in.close();
return ((double) zero / (mat.length * mat[0].length));
}
}
66 changes: 66 additions & 0 deletions src/test/java/com/thealgorithms/misc/SparsityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.thealgorithms.misc;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

public class SparsityTest {

private static final double DELTA = 1e-9;

@Test
public void testAllZeroElements() {
double[][] mat = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
double expectedSparsity = 1.0;
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with all zero elements should be 1.0");
}

@Test
public void testNoZeroElements() {
double[][] mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
double expectedSparsity = 0.0;
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with no zero elements should be 0.0");
}

@Test
public void testMixedElements() {
double[][] mat = {{0, 2, 0}, {4, 0, 6}, {0, 8, 0}};
double expectedSparsity = 5.0 / 9.0;
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of the matrix should be 5/9");
}

@Test
public void testSingleRowMatrix() {
double[][] mat = {{0, 1, 0, 2, 0}};
double expectedSparsity = 3.0 / 5.0;
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of the single-row matrix should be 3/5");
}

@Test
public void testSingleColumnMatrix() {
double[][] mat = {{1}, {0}, {0}, {2}};
double expectedSparsity = 2.0 / 4.0;
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of the single-column matrix should be 2/4");
}

@Test
public void testEmptyMatrix() {
double[][] mat = {};
assertThrows(IllegalArgumentException.class, () -> Sparsity.sparsity(mat), "Sparsity of an empty matrix should throw an IllegalArgumentException");
}

@Test
public void testMatrixWithSingleElementZero() {
double[][] mat = {{0}};
double expectedSparsity = 1.0;
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with a single zero element should be 1.0");
}

@Test
public void testMatrixWithSingleElementNonZero() {
double[][] mat = {{5}};
double expectedSparsity = 0.0;
assertEquals(expectedSparsity, Sparsity.sparsity(mat), DELTA, "Sparsity of a matrix with a single non-zero element should be 0.0");
}
}