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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the product of sum of two diagonals of a square Matrix
- Check if a pair with given product exists in a Matrix
- 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
- 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
- Queries for Count of divisors of product of an Array in given range | Set 2 (MO's Algorithm)
- Check if two elements of a matrix are on the same diagonal or not
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- Rabin-Karp algorithm for Pattern Searching in Matrix
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Maximum product of 4 adjacent elements in matrix
- Product of middle row and column in an odd square matrix
- Program to find the Product of diagonal elements of a matrix
- Count of ways to generate a Matrix with product of each row and column as 1 or -1
- Kth Smallest Element of a Matrix of given dimensions filled with product of indices
- Finding shortest path between any two nodes using Floyd Warshall Algorithm
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