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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Program to check diagonal matrix and scalar matrix
- Queries for Count of divisors of product of an Array in given range | Set 2 (MO's Algorithm)
- Program to check if a matrix is Binary matrix or not
- Check if it is possible to make the given matrix increasing matrix or not
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Find the product of sum of two diagonals of a square Matrix
- Maximum product of 4 adjacent elements in matrix
- Product of middle row and column in an odd square matrix
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- Program to find the Product of diagonal elements of a 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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