|
| 1 | +package com.thealgorithms.matrix; |
| 2 | + |
| 3 | +/** |
| 4 | + * This class provides a method to perform matrix multiplication using |
| 5 | + * Strassen's algorithm. |
| 6 | + * |
| 7 | + * <p> |
| 8 | + * Strassen's algorithm is a divide-and-conquer algorithm that is |
| 9 | + * asymptotically faster than the standard O(n^3) matrix multiplication. |
| 10 | + * It performs 7 recursive multiplications of sub-matrices of size n/2 |
| 11 | + * instead of the 8 required by the standard recursive method. |
| 12 | + * |
| 13 | + * <p> |
| 14 | + * For more details: |
| 15 | + * https://en.wikipedia.org/wiki/Strassen_algorithm |
| 16 | + * |
| 17 | + * <p> |
| 18 | + * Time Complexity: O(n^log2(7)) ≈ O(n^2.807) |
| 19 | + * |
| 20 | + * <p> |
| 21 | + * Space Complexity: O(n^2) – for storing intermediate and result matrices. |
| 22 | + * |
| 23 | + * <p> |
| 24 | + * Note: Due to the high overhead of recursion and sub-matrix creation in |
| 25 | + * Java, this algorithm is often slower than the standard O(n^3) |
| 26 | + * {@link MatrixMultiplication} for smaller matrices. A threshold is used |
| 27 | + * to switch to the standard algorithm for small matrices. |
| 28 | + * |
| 29 | + * @author @ITZ-NIHALPATEL |
| 30 | + * |
| 31 | + */ |
| 32 | +public final class StrassenMatrixMultiplication { |
| 33 | + |
| 34 | + /** |
| 35 | + * Threshold for matrix size to switch from Strassen's to standard |
| 36 | + * multiplication. Tuned by performance testing, 64 is a common value. |
| 37 | + */ |
| 38 | + private static final int THRESHOLD = 64; |
| 39 | + |
| 40 | + private StrassenMatrixMultiplication() { |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Multiplies two matrices using Strassen's algorithm. |
| 45 | + * |
| 46 | + * @param matrixA the first matrix (must be square, n x n) |
| 47 | + * @param matrixB the second matrix (must be square, n x n) |
| 48 | + * @return the product of the two matrices |
| 49 | + * @throws IllegalArgumentException if matrices are not square, not the |
| 50 | + * same size, or cannot be multiplied. |
| 51 | + */ |
| 52 | + public static double[][] multiply(double[][] matrixA, double[][] matrixB) { |
| 53 | + // --- 1. VALIDATION --- |
| 54 | + if (matrixA == null || matrixB == null) { |
| 55 | + throw new IllegalArgumentException("Input matrices cannot be null"); |
| 56 | + } |
| 57 | + if (matrixA.length == 0 || (matrixA.length > 0 && matrixA[0].length == 0)) { |
| 58 | + return new double[0][0]; // Handle empty matrix |
| 59 | + } |
| 60 | + |
| 61 | + int n = matrixA.length; |
| 62 | + if (n != matrixA[0].length || n != matrixB.length || n != matrixB[0].length) { |
| 63 | + throw new IllegalArgumentException( |
| 64 | + "Strassen's algorithm requires square matrices of the same dimension (n x n)." |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + // --- 2. PADDING --- |
| 69 | + // Find the next power of 2 |
| 70 | + int nextPowerOf2 = Integer.highestOneBit(n); |
| 71 | + if (nextPowerOf2 < n) { |
| 72 | + nextPowerOf2 <<= 1; |
| 73 | + } |
| 74 | + |
| 75 | + // Pad matrices to the next power of 2 |
| 76 | + double[][] paddedA = pad(matrixA, nextPowerOf2); |
| 77 | + double[][] paddedB = pad(matrixB, nextPowerOf2); |
| 78 | + |
| 79 | + // --- 3. RECURSION --- |
| 80 | + double[][] paddedResult = multiplyRecursive(paddedA, paddedB); |
| 81 | + |
| 82 | + // --- 4. UNPADDING --- |
| 83 | + // Extract the original n x n result from the padded result |
| 84 | + return unpad(paddedResult, n); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Recursive helper function for Strassen's algorithm. |
| 89 | + * Assumes input matrices are square and their size is a power of 2. |
| 90 | + */ |
| 91 | + private static double[][] multiplyRecursive(double[][] matrixA, double[][] matrixB) { |
| 92 | + int n = matrixA.length; |
| 93 | + |
| 94 | + // --- BASE CASE --- |
| 95 | + // If the matrix is small, switch to the standard O(n^3) algorithm |
| 96 | + if (n <= THRESHOLD) { |
| 97 | + return MatrixMultiplication.multiply(matrixA, matrixB); |
| 98 | + } |
| 99 | + |
| 100 | + // --- DIVIDE --- |
| 101 | + // Split matrices into four n/2 x n/2 sub-matrices |
| 102 | + int newSize = n / 2; |
| 103 | + double[][] a11 = split(matrixA, 0, 0, newSize); |
| 104 | + double[][] a12 = split(matrixA, 0, newSize, newSize); |
| 105 | + double[][] a21 = split(matrixA, newSize, 0, newSize); |
| 106 | + double[][] a22 = split(matrixA, newSize, newSize, newSize); |
| 107 | + |
| 108 | + double[][] b11 = split(matrixB, 0, 0, newSize); |
| 109 | + double[][] b12 = split(matrixB, 0, newSize, newSize); |
| 110 | + double[][] b21 = split(matrixB, newSize, 0, newSize); |
| 111 | + double[][] b22 = split(matrixB, newSize, newSize, newSize); |
| 112 | + |
| 113 | + // --- CONQUER (7 Recursive Calls) --- |
| 114 | + // P1 = A11 * (B12 - B22) |
| 115 | + double[][] p1 = multiplyRecursive(a11, subtract(b12, b22)); |
| 116 | + // P2 = (A11 + A12) * B22 |
| 117 | + double[][] p2 = multiplyRecursive(add(a11, a12), b22); |
| 118 | + // P3 = (A21 + A22) * B11 |
| 119 | + double[][] p3 = multiplyRecursive(add(a21, a22), b11); |
| 120 | + // P4 = A22 * (B21 - B11) |
| 121 | + double[][] p4 = multiplyRecursive(a22, subtract(b21, b11)); |
| 122 | + // P5 = (A11 + A22) * (B11 + B22) |
| 123 | + double[][] p5 = multiplyRecursive(add(a11, a22), add(b11, b22)); |
| 124 | + // P6 = (A12 - A22) * (B21 + B22) |
| 125 | + double[][] p6 = multiplyRecursive(subtract(a12, a22), add(b21, b22)); |
| 126 | + // P7 = (A11 - A21) * (B11 + B12) |
| 127 | + double[][] p7 = multiplyRecursive(subtract(a11, a21), add(b11, b12)); |
| 128 | + |
| 129 | + // --- COMBINE (Calculate Result Quadrants) --- |
| 130 | + // C11 = P5 + P4 - P2 + P6 |
| 131 | + double[][] c11 = add(subtract(add(p5, p4), p2), p6); |
| 132 | + // C12 = P1 + P2 |
| 133 | + double[][] c12 = add(p1, p2); |
| 134 | + // C21 = P3 + P4 |
| 135 | + double[][] c21 = add(p3, p4); |
| 136 | + // C22 = P5 + P1 - P3 - P7 |
| 137 | + double[][] c22 = subtract(subtract(add(p5, p1), p3), p7); |
| 138 | + |
| 139 | + // Join the four result quadrants into a single matrix |
| 140 | + return join(c11, c12, c21, c22); |
| 141 | + } |
| 142 | + |
| 143 | + // --- HELPER METHODS --- |
| 144 | + /** |
| 145 | + * Adds two matrices. |
| 146 | + */ |
| 147 | + private static double[][] add(double[][] matrixA, double[][] matrixB) { |
| 148 | + int n = matrixA.length; |
| 149 | + double[][] result = new double[n][n]; |
| 150 | + for (int i = 0; i < n; i++) { |
| 151 | + for (int j = 0; j < n; j++) { |
| 152 | + result[i][j] = matrixA[i][j] + matrixB[i][j]; |
| 153 | + } |
| 154 | + } |
| 155 | + return result; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Subtracts matrixB from matrixA. |
| 160 | + */ |
| 161 | + private static double[][] subtract(double[][] matrixA, double[][] matrixB) { |
| 162 | + int n = matrixA.length; |
| 163 | + double[][] result = new double[n][n]; |
| 164 | + for (int i = 0; i < n; i++) { |
| 165 | + for (int j = 0; j < n; j++) { |
| 166 | + result[i][j] = matrixA[i][j] - matrixB[i][j]; |
| 167 | + } |
| 168 | + } |
| 169 | + return result; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Splits a parent matrix into a new sub-matrix. |
| 174 | + */ |
| 175 | + private static double[][] split( |
| 176 | + double[][] matrix, |
| 177 | + int rowStart, |
| 178 | + int colStart, |
| 179 | + int size |
| 180 | + ) { |
| 181 | + double[][] subMatrix = new double[size][size]; |
| 182 | + for (int i = 0; i < size; i++) { |
| 183 | + System.arraycopy( |
| 184 | + matrix[i + rowStart], |
| 185 | + colStart, |
| 186 | + subMatrix[i], |
| 187 | + 0, |
| 188 | + size |
| 189 | + ); |
| 190 | + } |
| 191 | + return subMatrix; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Joins four sub-matrices into one larger matrix. |
| 196 | + */ |
| 197 | + private static double[][] join( |
| 198 | + double[][] c11, |
| 199 | + double[][] c12, |
| 200 | + double[][] c21, |
| 201 | + double[][] c22 |
| 202 | + ) { |
| 203 | + int n = c11.length; |
| 204 | + int newSize = n * 2; |
| 205 | + double[][] result = new double[newSize][newSize]; |
| 206 | + for (int i = 0; i < n; i++) { |
| 207 | + // C11 |
| 208 | + System.arraycopy(c11[i], 0, result[i], 0, n); |
| 209 | + // C12 |
| 210 | + System.arraycopy(c12[i], 0, result[i], n, n); |
| 211 | + // C21 |
| 212 | + System.arraycopy(c21[i], 0, result[i + n], 0, n); |
| 213 | + // C22 |
| 214 | + System.arraycopy(c22[i], 0, result[i + n], n, n); |
| 215 | + } |
| 216 | + return result; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Pads a matrix with zeros to a new larger size. |
| 221 | + */ |
| 222 | + private static double[][] pad(double[][] matrix, int size) { |
| 223 | + if (matrix.length == size) { |
| 224 | + return matrix; // No padding needed |
| 225 | + } |
| 226 | + int n = matrix.length; |
| 227 | + double[][] padded = new double[size][size]; |
| 228 | + for (int i = 0; i < n; i++) { |
| 229 | + System.arraycopy(matrix[i], 0, padded[i], 0, matrix[i].length); |
| 230 | + } |
| 231 | + return padded; |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Unpads a matrix to a new smaller size. |
| 236 | + */ |
| 237 | + private static double[][] unpad(double[][] matrix, int size) { |
| 238 | + if (matrix.length == size) { |
| 239 | + return matrix; // No unpadding needed |
| 240 | + } |
| 241 | + double[][] unpadded = new double[size][size]; |
| 242 | + for (int i = 0; i < size; i++) { |
| 243 | + System.arraycopy(matrix[i], 0, unpadded[i], 0, size); |
| 244 | + } |
| 245 | + return unpadded; |
| 246 | + } |
| 247 | +} |
0 commit comments