算法导论 分治 Streassen矩阵乘法

Streassen矩阵算法     时间复杂度:O(n) = 7T(n / 2) + O(n^{2})

O(n) = O(n^{log2^{7}})\approx O(n^{2.81})

用七次乘法七次乘法运算:

M_{1} = A_{11}(B_{12} - B_{22})

M_{2} = (A_{11} + A_{12})B_{22}

M_{3} = (A_{21} + A_{22})B_{11}

M_{4} = A_{22}(B_{21} - B_{11})

M_{5} = (A_{12} + A_{22})(B_{11} + B_{22})

M_{6} = (A_{12} - A_{22})(B_{21} + B_{22})

M_{7} = (A_{11} - A_{21})(B_{11} + B_{12})

增加若干次加法运算:

C_{11} = M_{5} + M_{4} - M_{2} + M_{6}

C_{12} = M_{1} + M_{2}

C_{21} = M_{3} + M_{4}

C_{22} = M_{5} + M_{1} - M_{3} - M_{7}

 
/**************************************************************
    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]));
            }
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值