分治法 java_Java实现用传统分治法解决矩阵相乘问题
时间: 2024-02-13 20:06:20 浏览: 30
好的,下面是用传统分治法解决矩阵相乘问题的Java实现:
```java
public class MatrixMultiplication {
public static int[][] multiply(int[][] A, int[][] B) {
int n = A.length;
int[][] C = new int[n][n];
if (n == 1) {
C[0][0] = A[0][0] * B[0][0];
} else {
int[][] A11 = new int[n/2][n/2];
int[][] A12 = new int[n/2][n/2];
int[][] A21 = new int[n/2][n/2];
int[][] A22 = new int[n/2][n/2];
int[][] B11 = new int[n/2][n/2];
int[][] B12 = new int[n/2][n/2];
int[][] B21 = new int[n/2][n/2];
int[][] B22 = new int[n/2][n/2];
// Divide A matrix into 4 sub-matrices
divideMatrix(A, A11, 0 , 0);
divideMatrix(A, A12, 0 , n/2);
divideMatrix(A, A21, n/2, 0);
divideMatrix(A, A22, n/2, n/2);
// Divide B matrix into 4 sub-matrices
divideMatrix(B, B11, 0 , 0);
divideMatrix(B, B12, 0 , n/2);
divideMatrix(B, B21, n/2, 0);
divideMatrix(B, B22, n/2, n/2);
// Recursively multiply sub-matrices
int[][] C11 = addMatrix(multiply(A11,B11), multiply(A12,B21));
int[][] C12 = addMatrix(multiply(A11,B12), multiply(A12,B22));
int[][] C21 = addMatrix(multiply(A21,B11), multiply(A22,B21));
int[][] C22 = addMatrix(multiply(A21,B12), multiply(A22,B22));
// Combine sub-matrices into the result matrix
combineMatrix(C11, C, 0 , 0);
combineMatrix(C12, C, 0 , n/2);
combineMatrix(C21, C, n/2, 0);
combineMatrix(C22, C, n/2, n/2);
}
return C;
}
// Divide matrix into sub-matrices
public static void divideMatrix(int[][] P, int[][] C, int iB, int jB) {
for(int i1 = 0, i2 = iB; i1 < C.length; i1++, i2++) {
for(int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++) {
C[i1][j1] = P[i2][j2];
}
}
}
// Combine sub-matrices into the result matrix
public static void combineMatrix(int[][] C, int[][] P, int iB, int jB) {
for(int i1 = 0, i2 = iB; i1 < C.length; i1++, i2++) {
for(int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++) {
P[i2][j2] = C[i1][j1];
}
}
}
// Add two matrices
public static int[][] addMatrix(int[][] A, int[][] B) {
int n = A.length;
int[][] C = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
return C;
}
public static void main(String[] args) {
int[][] A = {{1, 2}, {3, 4}};
int[][] B = {{5, 6}, {7, 8}};
int[][] C = multiply(A, B);
for (int i = 0; i < C.length; i++) {
for (int j = 0; j < C.length; j++) {
System.out.print(C[i][j] + " ");
}
System.out.println();
}
}
}
```
这个程序将两个n×n的矩阵相乘,其中n是2的幂。如果n等于1,则直接计算乘积。否则,将每个矩阵分成四个n/2×n/2的子矩阵,并递归地计算子矩阵的乘积。最后,将四个子矩阵的乘积组合成结果矩阵。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)