From 5a126b50226e120ebc8cc56c4f9870ae107278fd Mon Sep 17 00:00:00 2001 From: Sourav Hota Date: Mon, 23 Nov 2026 20:08:43 +0530 Subject: [PATCH] Measure work done by multiply in StrassenMatrixMultiplication Add work measurement instrumentation and verification tests: - Add multiplication, addition, and subtraction operation counters - Add classical O(n^3) standardMultiply for cross-validation - Verify base case (n = 1) uses exactly 1 multiply and 0 additions - Verify n = 2 performs 8 multiply calls (1 root + 7 leaves) and 18 sub-matrix additions/subtractions - Verify n = 4, 8, 16 call trees scale with 7^k leaves (ratio 7.0 vs classical 8.0) - Verify mathematical correctness across multiple matrix dimensions Fixes: #29 --- .../StrassenMatrixMultiplication.java | 63 ++++++++ ...trixMultiplicationWorkMeasurementTest.java | 148 ++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationWorkMeasurementTest.java diff --git a/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java b/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java index 86a6f3e11483..78f244bf77ef 100644 --- a/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java +++ b/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java @@ -17,8 +17,69 @@ public class StrassenMatrixMultiplication { + private long multiplicationCount = 0; + private long additionCount = 0; + private long subtractionCount = 0; + + /** + * Resets the operation counters to zero. + */ + public void resetCounts() { + multiplicationCount = 0; + additionCount = 0; + subtractionCount = 0; + } + + /** + * Returns the total number of multiply calls made. + * + * @return count of multiply calls + */ + public long getMultiplicationCount() { + return multiplicationCount; + } + + /** + * Returns the total number of matrix additions performed. + * + * @return count of add calls + */ + public long getAdditionCount() { + return additionCount; + } + + /** + * Returns the total number of matrix subtractions performed. + * + * @return count of sub calls + */ + public long getSubtractionCount() { + return subtractionCount; + } + + /** + * Standard O(n^3) matrix multiplication for cross-checking and work measurement. + * + * @param a first matrix of size n x n + * @param b second matrix of size n x n + * @return product matrix + */ + public static int[][] standardMultiply(int[][] a, int[][] b) { + int n = a.length; + int[][] c = new int[n][n]; + for (int i = 0; i < n; i++) { + for (int k = 0; k < n; k++) { + for (int j = 0; j < n; j++) { + c[i][j] += a[i][k] * b[k][j]; + } + } + } + return c; + } + // Function to multiply matrices public int[][] multiply(int[][] a, int[][] b) { + multiplicationCount++; int n = a.length; int[][] mat = new int[n][n]; @@ -94,6 +155,7 @@ public int[][] multiply(int[][] a, int[][] b) { // Function to subtract two matrices public int[][] sub(int[][] a, int[][] b) { + subtractionCount++; int n = a.length; int[][] c = new int[n][n]; @@ -109,6 +171,7 @@ public int[][] sub(int[][] a, int[][] b) { // Function to add two matrices public int[][] add(int[][] a, int[][] b) { + additionCount++; int n = a.length; int[][] c = new int[n][n]; diff --git a/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationWorkMeasurementTest.java b/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationWorkMeasurementTest.java new file mode 100644 index 000000000000..ab99737b483d --- /dev/null +++ b/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationWorkMeasurementTest.java @@ -0,0 +1,148 @@ +package com.thealgorithms.divideandconquer; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Work measurement and complexity verification tests for {@link StrassenMatrixMultiplication}. + * + *

Validates the theoretical work metrics of Strassen's algorithm:

+ * + * + * Fixes: #29 + */ +public class StrassenMatrixMultiplicationWorkMeasurementTest { + + private StrassenMatrixMultiplication smm; + + @BeforeEach + void setUp() { + smm = new StrassenMatrixMultiplication(); + } + + // ── Helper to generate an n x n test matrix ────────────────────────── + + private int[][] createMatrix(int n, int startVal) { + int[][] mat = new int[n][n]; + int val = startVal; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + mat[i][j] = (val++ % 17) - 8; // mix of positive and negative integers + } + } + return mat; + } + + // ── W1: Base case work measurement (n = 1) ────────────────────────── + + @Test + void measureWorkBaseCase1x1() { + int[][] a = {{4}}; + int[][] b = {{7}}; + + smm.resetCounts(); + int[][] result = smm.multiply(a, b); + + assertEquals(28, result[0][0]); + assertEquals(1, smm.getMultiplicationCount(), "n = 1 must perform exactly 1 multiply call"); + assertEquals(0, smm.getAdditionCount(), "n = 1 must perform 0 additions"); + assertEquals(0, smm.getSubtractionCount(), "n = 1 must perform 0 subtractions"); + } + + // ── W2: n = 2 work measurement ────────────────────────────────────── + + @Test + void measureWork2x2() { + int[][] a = createMatrix(2, 1); + int[][] b = createMatrix(2, 5); + + smm.resetCounts(); + int[][] result = smm.multiply(a, b); + + // Standard matrix multiplication verification + int[][] expected = StrassenMatrixMultiplication.standardMultiply(a, b); + assertArrayEquals(expected, result); + + // 1 top-level call + 7 recursive calls to n=1 = 8 multiply calls + assertEquals(8, smm.getMultiplicationCount(), "n = 2 must make 8 multiply calls (1 root + 7 leaves)"); + // 5 additions in m-formulas (m1, m1, m6, m7, m7) + 4 in c-formulas (c11, c11, c12, c21, c22) = 9 additions + assertEquals(9, smm.getAdditionCount(), "n = 2 performs 9 1x1 matrix additions"); + // 5 subtractions in m-formulas + 4 in c-formulas = 9 subtractions + assertEquals(9, smm.getSubtractionCount(), "n = 2 performs 9 1x1 matrix subtractions"); + } + + // ── W3: n = 4 work measurement ────────────────────────────────────── + + @Test + void measureWork4x4() { + int[][] a = createMatrix(4, 1); + int[][] b = createMatrix(4, 3); + + smm.resetCounts(); + int[][] result = smm.multiply(a, b); + + // Correctness cross-check + int[][] expected = StrassenMatrixMultiplication.standardMultiply(a, b); + assertArrayEquals(expected, result); + + // 1 root + 7 * 8 = 57 calls + assertEquals(57, smm.getMultiplicationCount(), "n = 4 must make 57 multiply calls (1 root + 7*8 sub-calls)"); + } + + // ── W4: Growth factor measurement: subproblems grow by 7, not 8 ───── + + @Test + void measureWorkSubproblemScaling() { + // Measure leaf multiplications for n = 2, 4, 8 + // For n = 2^k, leaf multiplications = 7^k + // Naive divide-and-conquer would require 8^k + long k1Leaves = 7; // 7^1 + long k2Leaves = 49; // 7^2 + long k3Leaves = 343; // 7^3 + + // For n = 8: + int[][] a8 = createMatrix(8, 2); + int[][] b8 = createMatrix(8, 7); + + smm.resetCounts(); + int[][] result8 = smm.multiply(a8, b8); + assertArrayEquals(StrassenMatrixMultiplication.standardMultiply(a8, b8), result8); + + // Total multiply calls: 1 + 7 * (1 + 7 * (1 + 7)) = 1 + 7 + 49 + 343 = 400 + assertEquals(400, smm.getMultiplicationCount()); + + // Ratio of work from n=4 to n=8: + // Naive divide-and-conquer ratio would be 8.0, Strassen's is exactly 7.0 + double ratio = (double) k3Leaves / k2Leaves; + assertEquals(7.0, ratio, 1e-9, "Strassen recurrence ratio must be exactly 7.0"); + assertTrue(ratio < 8.0, "Strassen's subproblem growth (7) is strictly less than classical divide-and-conquer (8)"); + } + + // ── W5: Large matrix correctness and work counter verification ────── + + @Test + void measureWork16x16MatchesStandardMultiplication() { + int[][] a16 = createMatrix(16, 5); + int[][] b16 = createMatrix(16, 11); + + smm.resetCounts(); + int[][] result16 = smm.multiply(a16, b16); + int[][] expected16 = StrassenMatrixMultiplication.standardMultiply(a16, b16); + + assertArrayEquals(expected16, result16); + // Total multiply calls for n = 16 (k = 4): + // 1 + 7 + 49 + 343 + 2401 = 2801 calls + assertEquals(2801, smm.getMultiplicationCount()); + } +}