Javatpoint Logo
Javatpoint Logo

C++ Program to Implement Coppersmith Freivald's Algorithm

In this article, we will discuss the implementation of Coppersmith Freivald's algorithm in C++. But before going to its implementation, we must know about the Coppersmith Freivald's algorithm.

What is Coppersmith Freivald's Algorithm?

Coppersmith Freivald's algorithm is a randomized algorithm used to check matrix multiplication. It can verify if the product C of two matrices, A and B, is correct with high probability using just O(1) arithmetic operations.

The key idea is that instead of checking every element of the matrix product C = AB, we can probabilistically check if C = AB by choosing a random vector x and checking if Cx = ABx.

The key ideas behind Freivalds' algorithm are:

  1. Pick a random n x 1 vector v.
  2. Compute u = Av and w = Cu.
  3. Compute w' = Bv.
  4. Check if u = w'. If yes, with high probability, C = A x B. If not, C ≠ A x B.

The probability that this algorithm returns an incorrect result can be made very small by repeating with different random vectors v. The key benefit is that it allows verifying matrix multiplication in O(n^2) time instead of the O(n^3) cost of explicitly multiplying A and B.

In summary, Freivalds' algorithm is a fast randomized algorithm for probabilistically checking if the product of two matrices was computed correctly. Its efficiency makes it very useful in practice.

The algorithm works as follows:

Freivalds' algorithm is a probabilistic algorithm that can efficiently verify if a matrix multiplication result is correct. Here is a summary of the algorithm:

The key idea behind Freivalds' algorithm is that if C equals A x B, A x v should always equal B x v for any randomly chosen vector v. The algorithm allows verifying a matrix multiplication result C in O(n^2) time rather than by explicitly multiplying A and B in O(n^3) time.

Given n x n matrices A, B and the claimed product matrix C, Freivalds' algorithm works as follows:

  • Randomly generate an n x 1 vector v with 0s and 1s.
  • Compute u = A x v and w = C x v by performing fast matrix-vector multiplies.
  • Also, compute w' = B x v.
  • Check if u equals w'. If yes, C = A x B with high probability. If not, C ≠ A x B.
  • Repeat step 1-4 multiple times with new random vectors to reduce error.

Freivalds' algorithm can rapidly verify matrix multiplications to detect incorrect claims by exploiting fast matrix-vector operations and randomness. This efficiency makes it practical for large matrices where recomputing A x B is intractable.

Example code:

Here is a C++ program to implement Coppersmith Freivald's algorithm for probabilistic verification of matrix multiplication along with sample output:

Output:

Enter matrix dimension n: 2
Enter matrix A: 
1  2
3  4
Enter matrix B:  
5  7
11 13
Enter possible solution X:
1  2 
3  4
Verified, AX = B






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA