CS 3343/3341
  Introduction to Algorithms  
Ordinary Matrix Multiplication
Using Divide and Conquer

On the left is the normal 4-by-4 matrix multiplication. On the right is the 4-by-4 case done using divide and conquer, the 4-by-4 divided into 2-by-2s of 2-by-2s. In this second case, all the multiplication is done using 2-by-2 multiplication and addition.

See instrumented programs for programs that count the exact number of multiplications and additions.

4-by-4 Matrices 2-by-2 of 2-by-2 Matrices
// Matr.java: multiply matrices, with returns
public class Matr {

   // C = A*B, mult of 4-by-4 matrices
   int[][] mult(int A[][], int B[][]){
      int[][] C = new int[4][4];
      for (int i = 0; i < 4; i++) { //rows
         for (int j = 0; j < 4; j++) { //cols
            C[i][j] = 0;
            for (int k = 0; k < 4; k++)
               C[i][j] += A[i][k] * B[k][j];
         }
      }
      return C;
   }

   void printMat(int A[][]){
      for (int i = 0; i < 4; i++){
         System.out.print("|");
         for (int j = 0; j < 4; j++)
           System.out.print(" "+A[i][j]+" ");
         System.out.print("|\n");
      }
      System.out.print("\n");
   }

   public static void main(String[] argv) {
      Matr matr = new Matr();
      int [][] A = {{2, 3, 1, 6},
                    {4, 0, 0, 2},
                    {4, 2, 0, 1},
                    {0, 3, 5, 2}};
      int [][] B = {{3, 0, 4, 3},
                    {1, 2, 0, 2},
                    {0, 3, 1, 4},
                    {5, 1, 3, 2}};
      int [][] C = matr.mult(A, B);
      matr.printMat(A);
      matr.printMat(B);
      matr.printMat(C);
   }
}
// Matr2.java: multiply matrices, with returns
public class Matr2 {

   // C = A*B, mult of 2-by-2 matrices
   int[][] mult(int A[][], int B[][]){
      int[][] C = new int[2][2];
      for (int i = 0; i < 2; i++) { //rows
         for (int j = 0; j < 2; j++) { //cols
            C[i][j] = 0;
            for (int k = 0; k < 2; k++)
               C[i][j] += A[i][k] * B[k][j];
         }
      }
      return C;
   }

   // C = A + B, addition of 2-by-2 matrices
   int[][] add(int A[][], int B[][]){
      int[][] C = new int[2][2];
      for (int i = 0; i < 2; i++) { //rows
         for (int j = 0; j < 2; j++) { //cols
            C[i][j] = A[i][j] + B[i][j];
         }
      }
      return C;
   }

   void printMat(int A[][]){
      for (int i = 0; i < 2; i++){
         System.out.print("|");
         for (int j = 0; j < 2; j++)
           System.out.print(" "+A[i][j]+" ");
         System.out.print("|\n");
      }
      System.out.print("\n");
   }

   public static void main(String[] argv) {
      Matr2 matr = new Matr2();
      int [][] A11 = {{2, 3},
                      {4, 0}};
      int [][] A12 = {{1, 6},
                      {0, 2}};
      int [][] A21 = {{4, 2},
                      {0, 3}};
      int [][] A22 = {{0, 1},
                      {5, 2}};
      int [][] B11 = {{3, 0},
                      {1, 2}};
      int [][] B12 = {{4, 3},
                      {0, 2}};
      int [][] B21 = {{0, 3},
                      {5, 1}};
      int [][] B22 = {{1, 4},
                      {3, 2}};
      int [][] C11 = 
          matr.add(matr.mult(A11, B11),
                   matr.mult(A12, B21));
      matr.printMat(A11);
      matr.printMat(B11);
      matr.printMat(A12);
      matr.printMat(B21);
      matr.printMat(C11);
      System.out.println("-----------");
      int [][] C12 =
          matr.add(matr.mult(A11, B12),
                   matr.mult(A12, B22));
      matr.printMat(A11);
      matr.printMat(B12);
      matr.printMat(A12);
      matr.printMat(B22);
      matr.printMat(C12);
      System.out.println("-----------");
      int [][] C21 =
          matr.add(matr.mult(A21, B11),
                   matr.mult(A22, B21));
      matr.printMat(A21);
      matr.printMat(B11);
      matr.printMat(A22);
      matr.printMat(B21);
      matr.printMat(C21);
      System.out.println("-----------");
      int [][] C22 =
          matr.add(matr.mult(A21, B12),
                   matr.mult(A22, B22));
      matr.printMat(A21);
      matr.printMat(B12);
      matr.printMat(A22);
      matr.printMat(B22);
      matr.printMat(C22);
      System.out.println("-----------");
   }
}
| 2  3  1  6 |        
| 4  0  0  2 |        | A11  A12 |
| 4  2  0  1 | = A =  | A21  A22 |
| 0  3  5  2 |

| 3  0  4  3 |
| 1  2  0  2 |        | B11  B12 |
| 0  3  1  4 | = B =  | B21  B22 |
| 5  1  3  2 |

| 39  15  27  28 |
| 22   2  22  16 |            | C11  C12 |
| 19   5  19  18 | = A * B =  | C21  C22 |
| 13  23  11  30 |


A11 * B11 + A12 * B21 = C11 | 2 3 | | 3 0 | | 1 6 | | 0 3 | | 39 15 | | 4 0 | * | 1 2 | + | 0 2 | * | 5 1 | = | 22 2 |
A11 * B12 + A12 * B22 = C12 | 2 3 | | 4 3 | | 1 6 | | 1 4 | | 27 28 | | 4 0 | * | 0 2 | + | 0 2 | * | 3 2 | = | 22 16 |
A21 * B11 + A22 * B21 = C21 | 4 2 | | 3 0 | | 0 1 | | 0 3 | | 19 5 | | 0 3 | * | 1 2 | + | 5 2 | * | 5 1 | = | 13 23 |
A21 * B12 + A22 * B22 = C22 | 4 2 | | 4 3 | | 0 1 | | 1 4 | | 19 18 | | 0 3 | * | 0 2 | + | 5 2 | * | 3 2 | = | 11 30 |


Revision date: 2012-09-26. (Please use ISO 8601, the International Standard Date and Time Notation.)