forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRandomizedMatrixMultiplicationVerification.java
More file actions
64 lines (54 loc) · 1.84 KB
/
RandomizedMatrixMultiplicationVerification.java
File metadata and controls
64 lines (54 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
}
}