The Wayback Machine - https://web.archive.org/web/20240610163559/https://www.geeksforgeeks.org/implementing-coppersmith-winograd-algorithm-in-java/
Open In App

Implementing Coppersmith Winograd Algorithm in Java

Last Updated : 15 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Coppersmith Winograd Algorithm is asymptotically the fastest known algorithm to date for the matrix multiplication algorithm. Algorithms with a preferred asymptotic running time over the Strassen calculation are rarely utilized practically, on the grounds that the huge steady factors in their running occasions make them inappropriate. 

It can multiply two n × n matrices in O(n^{2.375477}) time. It is used to check matrix multiplication.

It decides if the matrices are equivalent to a chosen value of k with an expectation value of failure under 2^-k in O(kn^2).

Example

Input:M1={{1,2},
      {3,4}}
      M2={{3,2},
          {5,1}}
      Result={{13,4},
              {29,10}}
Output:Resultant matrix is matching

Algorithm

// Task is to verify matrix multiplication as M1*M2=M3 or not.
1. Start
2. Take Matrices M1, M2, M3 as an input of (n*n).
3. Choose matrix a[n][1] randomly to which component will be 0 or 1.
4. Calculate M2 * a, M3 * a and then M1 * (M2 * a) for computing the expression,
   M1 * (M2 * a) - M3 * a.
5. Verify if M1 * (M2 * a) - M3 * a = 0 or not.
6. If it is zero or false, then matrix multiplication is correct otherwise not.
7. End

Below is the implementation of the above approach.

Java




// Implementing Coppersmith Winograd Algorithm in Java
import java.io.*;
import java.util.Random;
 
class GFG {
 
    public static boolean coppersmithWinograd(double[][] M1,
                                       double[][] M2,
                                       double[][] M3, int n)
    {
        double[][] a = new double[n][1];
        Random rand = new Random();
        for (int i = 0; i < n; i++) {
            a[i][0] = rand.nextInt() % 2;
        }
 
        double[][] M2a = new double[n][1];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 1; j++) {
                for (int k = 0; k < n; k++) {
                    M2a[i][j]
                        = M2a[i][j] + M2[i][k] * a[k][j];
                }
            }
        }
 
        double[][] M3a = new double[n][1];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 1; j++) {
                for (int k = 0; k < n; k++) {
                    M3a[i][j]
                        = M3a[i][j] + M3[i][k] * a[k][j];
                }
            }
        }
 
        double[][] M12a = new double[n][1];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 1; j++) {
                for (int k = 0; k < n; k++) {
                    M12a[i][j]
                        = M12a[i][j] + M1[i][k] * M2a[k][j];
                }
            }
        }
        for (int i = 0; i < n; i++) {
            M12a[i][0] -= M3a[i][0];
        }
        boolean sameResultantMatrix = true;
        for (int i = 0; i < n; i++) {
            if (M12a[i][0] == 0)
                continue;
            else
                sameResultantMatrix = false;
        }
        return sameResultantMatrix;
    }
 
    // Driver's Function
    public static void main(String[] args)
    {
 
        /// "Input the dimension of the matrices: "
        int n;
        n = 2;
        // "Input the 1st or M1 matrix: "
        double[][] M1 = { { 1, 2 }, { 3, 4 } };
        // "Input the 2nd or M2 matrix: "
 
        double[][] M2 = { { 2, 0 }, { 1, 2 } };
 
        // "Input the result or M3 matrix: "
        double[][] M3 = { { 4, 4 }, { 10, 8 } };
 
        if (coppersmithWinograd(M1, M2, M3, n))
            System.out.println("Resultant matrix is Matching");
        else
            System.out.println("Resultant matrix is not Matching");
    }
}


Output

Resultant matrix is Matching

Time Complexity: O(n^{2.375477})



Similar Reads

Implementing Coppersmith Freivald’s Algorithm in Java
Concept: Coppersmith Freivald's Algorithm is to check whether the matrix A multiplied by matrix B equals the given matrix C. It is used to verify matrix multiplication. It is verified with the help of an equation which stands A*(B*r)-(C*r)=0, where r is a random column vector consisting of 0/1 only. Illustration: Input: Enter the dimensions of the
4 min read
Java Applet | Implementing Flood Fill algorithm
Flood Fill Algorithm is to replace a certain closed or a similarly colored field with a specified color. The use of the FloodFill algorithm can be seen in paints and other games such as minesweeper.In this article, FloodFill is used for a connected area by a specified color, in Java Applet by using the FloodFill algorithm.There are two approaches t
5 min read
Implementing Borwein Algorithm in Java
Borwein's algorithm is a calculation contrived by Jonathan and Peter Borwein to compute the estimation of 1/π. They conceived a few different algorithms. The following implementation of Borwein's algorithm with quartic assembly in Java in fact ascertains Pi, however, converges excessively fast. In principle, a merges quartic ally to 1/π. On every e
3 min read
Implementing Strassen’s Algorithm in Java
Strassen's algorithm is used for the multiplication of Square Matrices that is the order of matrices should be (N x N). Strassen's Algorithm is based on the divide and conquer technique. In simpler terms, it is used for matrix multiplication. Strassen's method of matrix multiplication is a typical divide and conquer algorithm. However, let’s get ag
8 min read
Implementing Rabin Karp Algorithm Using Rolling Hash in Java
There are so many pattern searching algorithms for the string. KMP algorithm, Z algorithm Rabin Karp algorithm, etc these algorithms are the optimization of Naive Pattern searching Algorithm. Naive Pattern Searching Algorithm: Input : "AABACACAACAC" Pattern : "CAC" Output : [4,9] AABACACAACAC Implementation: Java Code // Java Program to Search for
5 min read
Implementing the Flip Algorithm For Non-Delaunay Triangles in Java
In computational geometry, the Delaunay triangulation is a fundamental concept used to partition a set of points into a mesh of non-overlapping triangles with the property that no point lies inside the circumcircle of any triangle. However, in certain cases, we may encounter non-Delaunay triangles where this property is violated. In this article, w
6 min read
Java | Implementing Iterator and Iterable Interface
Iterators are used in Collection framework in Java to retrieve elements one by one. For more details and introduction related to this, see this link. Why it is needed to implement Iterable interface? Every class that implements Iterable interface appropriately, can be used in the enhanced For loop (for-each loop). The need to implement the Iterator
4 min read
Implementing Checksum Using Java
The checksum is an error-detecting technique that can be applied to message of any length. It is used mostly at the network and transport layers of the TCP/IP protocol suite. Here, we have considered decimal data that is being sent by the sender to the receiver using socket programming. The number of segments that the data is divided into here depe
6 min read
Implementing Byte Stuffing using Java
Need for Byte-Stuffing In variable-size framing at the data link layer, we need to define a way to separate one frame from the next. Byte stuffing is employed to accomplish the task. In byte stuffing an 8-bit flag ('F') is added at the beginning and at the end of the frame, thereby distinguishing one frame from the next. Therefore, every time a fla
6 min read
Implementing next_permutation() in Java with Examples
Given an array or string, the task is to find the next lexicographically greater permutation of it in Java.Examples: Input: string = "gfg" Output: ggf Input: arr[] = {1, 2, 3} Output: {1, 3, 2} In C++, there is a specific function that saves us from a lot of code. It’s in the header file #include&lt;algorithm&gt;. The function is next_permutation(a
5 min read