Given three matrices A, B and C, find if C is a product of A and B.
Input : A = 1 1 1 1 B = 1 1 1 1 C = 2 2 2 2 Output : Yes C = A x B Input : A = 1 1 1 1 1 1 1 1 1 B = 1 1 1 1 1 1 1 1 1 C = 3 3 3 3 1 2 3 3 3 Output : No
A simple solution is to find product of A and B and then check if product is equal to C or not. A possible time complexity of this method is O(n2.8874) using Stression’s matrix multiplication.
Freivalds’ algorithm is a probabilistic randomized algorithm that works in time O(n2) with high probability. In O(kn2) time the algorithm can verify a matrix product with probability of failure less than 2-k. Since the output is not always correct, it is a Monte Carlo randomized algorithm.
- Generate an n × 1 random 0/1 vector r⃗.
- Compute P⃗ = A × (Br)⃗ – Cr⃗.
- Return true if P⃗ = ( 0, 0, …, 0 )T, return false otherwise.
The idea is based on the fact that if C is actually a product, then value of A × (Br)⃗ – Cr⃗ will always be 0. If the value is non-zero, then C can not be a product. The error condition is that the value may be 0 even when C is not a product.
- Check if a pair with given product exists in a Matrix
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Program to check diagonal matrix and scalar matrix
- Check if it is possible to make the given matrix increasing matrix or not
- Program to check if a matrix is Binary matrix or not
- Maximum product of 4 adjacent elements in matrix
- Product of middle row and column in an odd square matrix
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Check if a given matrix is Hankel or not
- Check if a Matrix is Invertible
- Check if a given matrix is sparse or not
- Check for possible path in 2D matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : manishshaw1