Skip to content

Commit 808e53d

Browse files
committed
Added Freivalds' Algorithm for randomized matrix multiplication verification
1 parent 712ada5 commit 808e53d

File tree

2 files changed

+99
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)