Streassen矩阵算法 时间复杂度:
七次乘法运算:
增加若干次加法运算:
/**************************************************************
Author: Alive
date:2019/4/2
Language: Java
Time:105 ms
Memory:12416 kb
****************************************************************/
/*用Java创建动态数组会方便一些*/
import java.util.Scanner;
public class Main {
static void MartixSub(int arr1[][], int arr2[][], int ans[][]) {//矩阵减法
for (int i = 0; i < arr1.length; ++i) {
for (int j = 0; j < arr1.length; ++j) {
ans[i][j] = arr1[i][j] - arr2[i][j];
}
}
}
static void MartixAdd(int arr1[][], int arr2[][], int ans[][]) {//矩阵加法
for (int i = 0; i < arr1.length; ++i) {
for (int j = 0; j < arr1.length; ++j) {
ans[i][j] = arr1[i][j] + arr2[i][j];
}
}
}
static void Strassen(int arr1[][], int arr2[][], int res[][], int size) {
if (size == 1) {
res[0][0] = arr1[0][0] * arr2[0][0];
}
else {
int bs = (size >> 1);
int a11[][] = new int[bs][bs];
int a12[][] = new int[bs][bs];
int a21[][] = new int[bs][bs];
int a22[][] = new int[bs][bs];
int b11[][] = new int[bs][bs];
int b12[][] = new int[bs][bs];
int b21[][] = new int[bs][bs];
int b22[][] = new int[bs][bs];
for (int i = 0; i < bs; ++i) {
for (int j = 0; j < bs; ++j) {
a11[i][j] = arr1[i][j];
a12[i][j] = arr1[i][j + bs];
a21[i][j] = arr1[i + bs][j];
a22[i][j] = arr1[i + bs][j + bs];
b11[i][j] = arr2[i][j];
b12[i][j] = arr2[i][j + bs];
b21[i][j] = arr2[i + bs][j];
b22[i][j] = arr2[i + bs][j + bs];
}
}
int M1[][] = new int[bs][bs];
int M2[][] = new int[bs][bs];
int M3[][] = new int[bs][bs];
int M4[][] = new int[bs][bs];
int M5[][] = new int[bs][bs];
int M6[][] = new int[bs][bs];
int M7[][] = new int[bs][bs];
int temp1[][] = new int[bs][bs];
int temp2[][] = new int[bs][bs];
MartixSub(b12, b22, temp1);
Strassen(a11, temp1, M1, bs);
MartixAdd(a11, a12, temp1);
Strassen(temp1, b22, M2, bs);
MartixAdd(a21, a22, temp1);
Strassen(temp1, b11, M3, bs);
MartixSub(b21, b11, temp1);
Strassen(a22, temp1, M4, bs);
MartixAdd(a11, a22, temp1);
MartixAdd(b11, b22, temp2);
Strassen(temp1, temp2, M5, bs);
MartixSub(a12, a22, temp1);
MartixAdd(b21, b22, temp2);
Strassen(temp1, temp2, M6, bs);
MartixSub(a11, a21, temp1);
MartixAdd(b11, b12, temp2);
Strassen(temp1, temp2, M7, bs);
int C11[][] = new int[bs][bs];
int C12[][] = new int[bs][bs];
int C21[][] = new int[bs][bs];
int C22[][] = new int[bs][bs];
MartixAdd(M5, M4, C11);
MartixSub(C11, M2, C11);
MartixAdd(C11, M6, C11);
MartixAdd(M1, M2, C12);
MartixAdd(M3, M4, C21);
MartixAdd(M5, M1, C22);
MartixSub(C22, M3, C22);
MartixSub(C22, M7, C22);
for (int i = 0; i < bs; ++i) {
for (int j = 0; j < bs; ++j) {
res[i][j] = C11[i][j];
res[i][j + bs] = C12[i][j];
res[i + bs][j] = C21[i][j];
res[i + bs][j + bs] = C22[i][j];
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int n = sc.nextInt();
int martix1[][] = new int[n][n];
int martix2[][] = new int[n][n];
int res[][] = new int[n][n];
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
martix1[i][j] = sc.nextInt();
}
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
martix2[i][j] = sc.nextInt();
}
}
Strassen(martix1, martix2, res, n);
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n - 1; ++j) {
System.out.print(String.format("%2d", res[i][j]) + " ");
}
System.out.println(String.format("%2d", res[i][n - 1]));
}
}
}
}