From 808e53d47e9da7892ff08a2d4c395210116d4d2c Mon Sep 17 00:00:00 2001 From: aditya-7562 Date: Fri, 4 Jul 2025 14:22:32 +0530 Subject: [PATCH 1/2] Added Freivalds' Algorithm for randomized matrix multiplication verification --- ...mizedMatrixMultiplicationVerification.java | 60 +++++++++++++++++++ ...dMatrixMultiplicationVerificationTest.java | 39 ++++++++++++ 2 files changed, 99 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 000000000000..639144838b77 --- /dev/null +++ b/src/main/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerification.java @@ -0,0 +1,60 @@ +package com.thealgorithms.randomized; + +import java.util.Random; + +public class RandomizedMatrixMultiplicationVerification { + + /** + * 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 000000000000..05c16ce2797d --- /dev/null +++ b/src/test/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerificationTest.java @@ -0,0 +1,39 @@ +package com.thealgorithms.randomized; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +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)); + } +} From e1af2163bafc36b3b3e3d14afd31ee58d86c2272 Mon Sep 17 00:00:00 2001 From: aditya-7562 Date: Fri, 4 Jul 2025 14:44:44 +0530 Subject: [PATCH 2/2] Formatting fixes --- ...mizedMatrixMultiplicationVerification.java | 32 +++++++++-------- ...dMatrixMultiplicationVerificationTest.java | 34 ++++++++++--------- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerification.java b/src/main/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerification.java index 639144838b77..b5ac7076bfd6 100644 --- a/src/main/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerification.java +++ b/src/main/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerification.java @@ -2,7 +2,11 @@ import java.util.Random; -public class RandomizedMatrixMultiplicationVerification { +public final class RandomizedMatrixMultiplicationVerification { + + private RandomizedMatrixMultiplicationVerification() { + // Prevent instantiation of utility class + } /** * Verifies whether A × B == C using Freivalds' algorithm. @@ -12,8 +16,8 @@ public class RandomizedMatrixMultiplicationVerification { * @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; + 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++) { @@ -23,33 +27,33 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) { r[i] = random.nextInt(2); } - // Step 2: Compute Br = B × r - int[] Br = new int[n]; + // 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]; + br[i] += b[i][j] * r[j]; } } - // Step 3: Compute A(Br) - int[] ABr = new int[n]; + // 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]; + abr[i] += a[i][j] * br[j]; } } - // Step 4: Compute Cr = C × r - int[] Cr = new int[n]; + // 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]; + cr[i] += c[i][j] * r[j]; } } - // Step 5: Compare ABr and Cr + // Step 5: Compare abr and cr for (int i = 0; i < n; i++) { - if (ABr[i] != Cr[i]) { + if (abr[i] != cr[i]) { return false; } } diff --git a/src/test/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerificationTest.java b/src/test/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerificationTest.java index 05c16ce2797d..330662ac2c52 100644 --- a/src/test/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerificationTest.java +++ b/src/test/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerificationTest.java @@ -1,39 +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; -import static org.junit.jupiter.api.Assertions.*; 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)); + 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)); + 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]; + 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; + a[i][i] = 1; + b[i][i] = 1; + c[i][i] = 1; } - assertTrue(RandomizedMatrixMultiplicationVerification.verify(A, B, C, 15)); + assertTrue(RandomizedMatrixMultiplicationVerification.verify(a, b, c, 15)); } }