From 58ac54cbdbc6d8f006e4fcb753f6ac416521702b Mon Sep 17 00:00:00 2001 From: Aditya Date: Fri, 4 Jul 2025 15:46:57 +0530 Subject: [PATCH] Add Freivalds' Algorithm for randomized matrix multiplication verification (#6340) --- ...mizedMatrixMultiplicationVerification.java | 64 +++++++++++++++++++ ...dMatrixMultiplicationVerificationTest.java | 41 ++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/main/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerification.java create mode 100644 src/test/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerificationTest.java diff --git a/src/main/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerification.java b/src/main/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerification.java new file mode 100644 index 000000000..b5ac7076b --- /dev/null +++ b/src/main/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerification.java @@ -0,0 +1,64 @@ +package com.thealgorithms.randomized; + +import java.util.Random; + +public final class RandomizedMatrixMultiplicationVerification { + + private RandomizedMatrixMultiplicationVerification() { + // Prevent instantiation of utility class + } + + /** + * Verifies whether A × B == C using Freivalds' algorithm. + * @param A Left matrix + * @param B Right matrix + * @param C Product matrix to verify + * @param iterations Number of randomized checks + * @return true if likely A×B == C; false if definitely not + */ + public static boolean verify(int[][] a, int[][] b, int[][] c, int iterations) { + int n = a.length; + Random random = new Random(); + + for (int iter = 0; iter < iterations; iter++) { + // Step 1: Generate random 0/1 vector + int[] r = new int[n]; + for (int i = 0; i < n; i++) { + r[i] = random.nextInt(2); + } + + // Step 2: Compute br = b × r + int[] br = new int[n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + br[i] += b[i][j] * r[j]; + } + } + + // Step 3: Compute a(br) + int[] abr = new int[n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + abr[i] += a[i][j] * br[j]; + } + } + + // Step 4: Compute cr = c × r + int[] cr = new int[n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + cr[i] += c[i][j] * r[j]; + } + } + + // Step 5: Compare abr and cr + for (int i = 0; i < n; i++) { + if (abr[i] != cr[i]) { + return false; + } + } + } + + return true; + } +} diff --git a/src/test/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerificationTest.java b/src/test/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerificationTest.java new file mode 100644 index 000000000..330662ac2 --- /dev/null +++ b/src/test/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerificationTest.java @@ -0,0 +1,41 @@ +package com.thealgorithms.randomized; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class RandomizedMatrixMultiplicationVerificationTest { + + @Test + void testCorrectMultiplication() { + int[][] a = {{1, 2}, {3, 4}}; + int[][] b = {{5, 6}, {7, 8}}; + int[][] c = {{19, 22}, {43, 50}}; + assertTrue(RandomizedMatrixMultiplicationVerification.verify(a, b, c, 10)); + } + + @Test + void testIncorrectMultiplication() { + int[][] a = {{1, 2}, {3, 4}}; + int[][] b = {{5, 6}, {7, 8}}; + int[][] wrongC = {{20, 22}, {43, 51}}; + assertFalse(RandomizedMatrixMultiplicationVerification.verify(a, b, wrongC, 10)); + } + + @Test + void testLargeMatrix() { + int size = 100; + int[][] a = new int[size][size]; + int[][] b = new int[size][size]; + int[][] c = new int[size][size]; + + for (int i = 0; i < size; i++) { + a[i][i] = 1; + b[i][i] = 1; + c[i][i] = 1; + } + + assertTrue(RandomizedMatrixMultiplicationVerification.verify(a, b, c, 15)); + } +}