Skip to content

Commit e1af216

Browse files
committed
Formatting fixes
1 parent 808e53d commit e1af216

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

src/main/java/com/thealgorithms/randomized/RandomizedMatrixMultiplicationVerification.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import java.util.Random;
44

5-
public class RandomizedMatrixMultiplicationVerification {
5+
public final class RandomizedMatrixMultiplicationVerification {
6+
7+
private RandomizedMatrixMultiplicationVerification() {
8+
// Prevent instantiation of utility class
9+
}
610

711
/**
812
* Verifies whether A × B == C using Freivalds' algorithm.
@@ -12,8 +16,8 @@ public class RandomizedMatrixMultiplicationVerification {
1216
* @param iterations Number of randomized checks
1317
* @return true if likely A×B == C; false if definitely not
1418
*/
15-
public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) {
16-
int n = A.length;
19+
public static boolean verify(int[][] a, int[][] b, int[][] c, int iterations) {
20+
int n = a.length;
1721
Random random = new Random();
1822

1923
for (int iter = 0; iter < iterations; iter++) {
@@ -23,33 +27,33 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) {
2327
r[i] = random.nextInt(2);
2428
}
2529

26-
// Step 2: Compute Br = B × r
27-
int[] Br = new int[n];
30+
// Step 2: Compute br = b × r
31+
int[] br = new int[n];
2832
for (int i = 0; i < n; i++) {
2933
for (int j = 0; j < n; j++) {
30-
Br[i] += B[i][j] * r[j];
34+
br[i] += b[i][j] * r[j];
3135
}
3236
}
3337

34-
// Step 3: Compute A(Br)
35-
int[] ABr = new int[n];
38+
// Step 3: Compute a(br)
39+
int[] abr = new int[n];
3640
for (int i = 0; i < n; i++) {
3741
for (int j = 0; j < n; j++) {
38-
ABr[i] += A[i][j] * Br[j];
42+
abr[i] += a[i][j] * br[j];
3943
}
4044
}
4145

42-
// Step 4: Compute Cr = C × r
43-
int[] Cr = new int[n];
46+
// Step 4: Compute cr = c × r
47+
int[] cr = new int[n];
4448
for (int i = 0; i < n; i++) {
4549
for (int j = 0; j < n; j++) {
46-
Cr[i] += C[i][j] * r[j];
50+
cr[i] += c[i][j] * r[j];
4751
}
4852
}
4953

50-
// Step 5: Compare ABr and Cr
54+
// Step 5: Compare abr and cr
5155
for (int i = 0; i < n; i++) {
52-
if (ABr[i] != Cr[i]) {
56+
if (abr[i] != cr[i]) {
5357
return false;
5458
}
5559
}
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11
package com.thealgorithms.randomized;
22

3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
36
import org.junit.jupiter.api.Test;
4-
import static org.junit.jupiter.api.Assertions.*;
57

68
class RandomizedMatrixMultiplicationVerificationTest {
79

810
@Test
911
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));
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));
1416
}
1517

1618
@Test
1719
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));
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));
2224
}
2325

2426
@Test
2527
void testLargeMatrix() {
2628
int size = 100;
27-
int[][] A = new int[size][size];
28-
int[][] B = new int[size][size];
29-
int[][] C = new int[size][size];
29+
int[][] a = new int[size][size];
30+
int[][] b = new int[size][size];
31+
int[][] c = new int[size][size];
3032

3133
for (int i = 0; i < size; i++) {
32-
A[i][i] = 1;
33-
B[i][i] = 1;
34-
C[i][i] = 1;
34+
a[i][i] = 1;
35+
b[i][i] = 1;
36+
c[i][i] = 1;
3537
}
3638

37-
assertTrue(RandomizedMatrixMultiplicationVerification.verify(A, B, C, 15));
39+
assertTrue(RandomizedMatrixMultiplicationVerification.verify(a, b, c, 15));
3840
}
3941
}

0 commit comments

Comments
 (0)