From a9fdfe56b597b5cafe64491d9dd7ed26ba796c04 Mon Sep 17 00:00:00 2001 From: Aarya Bamne Date: Fri, 20 Jun 2025 20:35:52 +0530 Subject: [PATCH] Add RandomizedMatrixVerifier and test class --- .../matrix/RandomizedMatrixVerifier.java | 54 +++++++++++++++++ .../matrix/RandomizedMatrixVerifierTest.java | 59 +++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/main/java/com/thealgorithms/matrix/RandomizedMatrixVerifier.java create mode 100644 src/test/java/com/thealgorithms/matrix/RandomizedMatrixVerifierTest.java diff --git a/src/main/java/com/thealgorithms/matrix/RandomizedMatrixVerifier.java b/src/main/java/com/thealgorithms/matrix/RandomizedMatrixVerifier.java new file mode 100644 index 000000000000..7f7f921775a7 --- /dev/null +++ b/src/main/java/com/thealgorithms/matrix/RandomizedMatrixVerifier.java @@ -0,0 +1,54 @@ +/** + * Randomized algorithm to verify if A * B = C without computing full multiplication. + * Returns true if probably correct, false if definitely incorrect. + * Uses Freivalds' algorithm. + */ + +package com.thealgorithms.matrix; + +public class RandomizedMatrixVerifier { + + public static boolean verify(int[][] A, int[][] B, int[][] C) { + int n = A.length; + int[] r = new int[n]; + + // Generate random vector r + for (int i = 0; i < n; i++) { + r[i] = (int) (Math.random() * 10); // keep it simple + } + + // Compute B * r + int[] Br = new int[n]; + for (int i = 0; i < n; i++) { + Br[i] = 0; + for (int j = 0; j < n; j++) { + Br[i] += B[i][j] * r[j]; + } + } + + // Compute A * (B * r) + int[] ABr = new int[n]; + for (int i = 0; i < n; i++) { + ABr[i] = 0; + for (int j = 0; j < n; j++) { + ABr[i] += A[i][j] * Br[j]; + } + } + + // Compute C * r + int[] Cr = new int[n]; + for (int i = 0; i < n; i++) { + Cr[i] = 0; + for (int j = 0; j < n; j++) { + Cr[i] += C[i][j] * r[j]; + } + } + + // Compare ABr and Cr + for (int i = 0; i < n; i++) { + if (ABr[i] != Cr[i]) return false; + } + + return true; // probably correct + } +} diff --git a/src/test/java/com/thealgorithms/matrix/RandomizedMatrixVerifierTest.java b/src/test/java/com/thealgorithms/matrix/RandomizedMatrixVerifierTest.java new file mode 100644 index 000000000000..b2b65bed61f2 --- /dev/null +++ b/src/test/java/com/thealgorithms/matrix/RandomizedMatrixVerifierTest.java @@ -0,0 +1,59 @@ +package com.thealgorithms.matrix; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +public class RandomizedMatrixVerifierTest { + + @Test + public void testCorrectMultiplication() { + int[][] A = { + {1, 2}, + {3, 4} + }; + int[][] B = { + {5, 6}, + {7, 8} + }; + int[][] C = { + {19, 22}, + {43, 50} + }; + + // Run multiple times to reduce chance of false positive + boolean result = true; + for (int i = 0; i < 5; i++) { + if (!RandomizedMatrixVerifier.verify(A, B, C)) { + result = false; + break; + } + } + assertTrue(result, "Verification should return true for correct C = A * B"); + } + + @Test + public void testIncorrectMultiplication() { + int[][] A = { + {1, 2}, + {3, 4} + }; + int[][] B = { + {5, 6}, + {7, 8} + }; + int[][] wrongC = { + {19, 22}, + {43, 51} // incorrect value + }; + + // Even with randomness, wrong matrix should fail at least once in 5 tries + boolean result = true; + for (int i = 0; i < 5; i++) { + if (!RandomizedMatrixVerifier.verify(A, B, wrongC)) { + result = false; + break; + } + } + assertFalse(result, "Verification should return false for incorrect C"); + } +}