Skip to content

Add RandomizedMatrixVerifier and test class #6305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

krishnamitra03
Copy link

New Feature: Randomized Matrix Equality Verifier
This pull request introduces a new utility class called RandomizedMatrixVerifier in the matrix package.
It provides an efficient method areMatricesEqual(int[][] a, int[][] b) that determines whether two matrices are equal using randomized hashing with prime coefficients.
This approach ensures performance and robustness, especially for large matrices.

Tests Added:-
A corresponding test class RandomizedMatrixVerifierTest is added under src/test/java/com/thealgorithms/matrix/.
It includes two unit tests using JUnit 5:
✅ Verifies equality for identical matrices
❌ Verifies inequality for different matrices
Both test cases pass successfully using the Maven test runner.

Build & Code Coverage:-
The project builds cleanly with Java 21 and Maven 3.9.10
Tests executed via mvn -Dtest=RandomizedMatrixVerifierTest test
Code coverage verified using JaCoCo, confirming the new code is tested

Code Quality & Structure:-
Follows the existing file structure and naming conventions of the repository
Code is clean, well-commented, and adheres to contribution guidelines
No breaking changes introduced to existing functionality

About This Contribution:-
This is my first contribution to the TheAlgorithms/Java repository
I’m currently learning open-source contribution and Java best practices.

Feedback and suggestions are warmly welcome!!!

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.

Project coverage is 74.18%. Comparing base (ae71861) to head (a9fdfe5).

Files with missing lines Patch % Lines
...thealgorithms/matrix/RandomizedMatrixVerifier.java 95.65% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6305      +/-   ##
============================================
+ Coverage     74.16%   74.18%   +0.02%     
- Complexity     5362     5372      +10     
============================================
  Files           678      679       +1     
  Lines         18686    18709      +23     
  Branches       3625     3634       +9     
============================================
+ Hits          13858    13880      +22     
- Misses         4273     4274       +1     
  Partials        555      555              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
int[] r = new int[n];

// Generate random vector r
for (int i = 0; i < n; i++) {
Copy link
Collaborator

@DenizAltunkapan DenizAltunkapan Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krishnamitra03 Freivalds' algorithm usually uses a random binary vector (entries 0 or 1), not values in range 0-9. Using Math.random() * 10 may lead to inefficiencies or poor randomness.

for (int i = 0; i < n; i++) {
r[i] = (int) (Math.random() * 10); // keep it simple
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider checking if matrices A, B, and C are all square and of the same size.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix the failing build pipeline (checkstyle)

}
}
assertFalse(result, "Verification should return false for incorrect C");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for mismatched sizes to check dimension preconditions (if implemented)

@DenizAltunkapan
Copy link
Collaborator

@TheAlgorithms/java_maintainers Would you prefer placing randomized algorithms like RandomizedMatrixVerifier or RandomizedQuickSort in their respective functional packages (e.g., matrix, sorting) to keep related algorithms together, or should we collect all randomized algorithms in a dedicated randomized package for conceptual clarity?

@siriak
Copy link
Member

siriak commented Jun 25, 2025

I'd say store them in functional packages because then it's organized based on problem and not approach used to solve it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants