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.
Input: Enter the dimensions of the matrices: 2 Enter the 1st matrix: -2 1 0 4 Enter the 2st matrix: 6 5 -7 1 Enter the result matrix: -19 9 -28 4 Output: Yes, The matrix multiplication is correct.
Take the size of the matrix as input from the user.
Goal: According to the equation we need to verify matrix A * matrix B = matrix C.
Take inputs of matrix A(n*n) matrix B(n*n) and the resultant matrix C(n*n) as input.
1) Take array r[n] randomly which consists of elements of 0/1 only.
2) Compute matrix B*r, matrix C*r and then matrix A*(matrix B*r) for evaluating the expression matrix A*(matrix B * r) – (matrix C*r)
3) Check if the equation matrix A*(matrix B * r) – (matrix C*r)=0 or not.
4)If it is zero then print “Yes” else print “No”.
Implementation: Input should be taken in the order shown above, else it will lead to wrong results. Below is the example for consideration
Output: Custom input for 2 random matrices of order 2
Time Complexity : O(kN^2) where N is the size of the matrix.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.