Skip to content

Commit 58ac54c

Browse files
authored
Add Freivalds' Algorithm for randomized matrix multiplication verification (#6340)
1 parent 712ada5 commit 58ac54c

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.thealgorithms.randomized;
2+
3+
import java.util.Random;
4+
5+
public final class RandomizedMatrixMultiplicationVerification {
6+
7+
private RandomizedMatrixMultiplicationVerification() {
8+
// Prevent instantiation of utility class
9+
}
10+
11+
/**
12+
* Verifies whether A × B == C using Freivalds' algorithm.
13+
* @param A Left matrix
14+
* @param B Right matrix
15+
* @param C Product matrix to verify
16+
* @param iterations Number of randomized checks
17+
* @return true if likely A×B == C; false if definitely not
18+
*/
19+
public static boolean verify(int[][] a, int[][] b, int[][] c, int iterations) {
20+
int n = a.length;
21+
Random random = new Random();
22+
23+
for (int iter = 0; iter < iterations; iter++) {
24+
// Step 1: Generate random 0/1 vector
25+
int[] r = new int[n];
26+
for (int i = 0; i < n; i++) {
27+
r[i] = random.nextInt(2);
28+
}
29+
30+
// Step 2: Compute br = b × r
31+
int[] br = new int[n];
32+
for (int i = 0; i < n; i++) {
33+
for (int j = 0; j < n; j++) {
34+
br[i] += b[i][j] * r[j];
35+
}
36+
}
37+
38+
// Step 3: Compute a(br)
39+
int[] abr = new int[n];
40+
for (int i = 0; i < n; i++) {
41+
for (int j = 0; j < n; j++) {
42+
abr[i] += a[i][j] * br[j];
43+
}
44+
}
45+
46+
// Step 4: Compute cr = c × r
47+
int[] cr = new int[n];
48+
for (int i = 0; i < n; i++) {
49+
for (int j = 0; j < n; j++) {
50+
cr[i] += c[i][j] * r[j];
51+
}
52+
}
53+
54+
// Step 5: Compare abr and cr
55+
for (int i = 0; i < n; i++) {
56+
if (abr[i] != cr[i]) {
57+
return false;
58+
}
59+
}
60+
}
61+
62+
return true;
63+
}
64+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.thealgorithms.randomized;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class RandomizedMatrixMultiplicationVerificationTest {
9+
10+
@Test
11+
void testCorrectMultiplication() {
12+
int[][] a = {{1, 2}, {3, 4}};
13+
int[][] b = {{5, 6}, {7, 8}};
14+
int[][] c = {{19, 22}, {43, 50}};
15+
assertTrue(RandomizedMatrixMultiplicationVerification.verify(a, b, c, 10));
16+
}
17+
18+
@Test
19+
void testIncorrectMultiplication() {
20+
int[][] a = {{1, 2}, {3, 4}};
21+
int[][] b = {{5, 6}, {7, 8}};
22+
int[][] wrongC = {{20, 22}, {43, 51}};
23+
assertFalse(RandomizedMatrixMultiplicationVerification.verify(a, b, wrongC, 10));
24+
}
25+
26+
@Test
27+
void testLargeMatrix() {
28+
int size = 100;
29+
int[][] a = new int[size][size];
30+
int[][] b = new int[size][size];
31+
int[][] c = new int[size][size];
32+
33+
for (int i = 0; i < size; i++) {
34+
a[i][i] = 1;
35+
b[i][i] = 1;
36+
c[i][i] = 1;
37+
}
38+
39+
assertTrue(RandomizedMatrixMultiplicationVerification.verify(a, b, c, 15));
40+
}
41+
}

0 commit comments

Comments
 (0)