Freivalds' Algorithm
   HOME

TheInfoList



OR:

Freivalds' algorithm (named after Rūsiņš Mārtiņš Freivalds) is a probabilistic
randomized algorithm A randomized algorithm is an algorithm that employs a degree of randomness as part of its logic or procedure. The algorithm typically uses uniformly random bits as an auxiliary input to guide its behavior, in the hope of achieving good performan ...
used to verify
matrix multiplication In mathematics, specifically in linear algebra, matrix multiplication is a binary operation that produces a matrix (mathematics), matrix from two matrices. For matrix multiplication, the number of columns in the first matrix must be equal to the n ...
. Given three ''n'' × ''n''
matrices Matrix (: matrices or matrixes) or MATRIX may refer to: Science and mathematics * Matrix (mathematics), a rectangular array of numbers, symbols or expressions * Matrix (logic), part of a formula in prenex normal form * Matrix (biology), the ...
A, B, and C, a general problem is to verify whether A \times B = C. A naïve
algorithm In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
would compute the product A \times B explicitly and compare term by term whether this product equals C. However, the best known matrix multiplication algorithm runs in O(n^) time. Freivalds' algorithm utilizes
randomization Randomization is a statistical process in which a random mechanism is employed to select a sample from a population or assign subjects to different groups.Oxford English Dictionary "randomization" The process is crucial in ensuring the random alloc ...
in order to reduce this time bound to O(n^2)
with high probability In mathematics, an event that occurs with high probability (often shortened to w.h.p. or WHP) is one whose probability depends on a certain number ''n'' and goes to 1 as ''n'' goes to infinity, i.e. the probability of the event occurring can be m ...
. In O(kn^2) time the algorithm can verify a matrix product with probability of failure less than 2^.


The algorithm


Input

Three ''n'' × ''n''
matrices Matrix (: matrices or matrixes) or MATRIX may refer to: Science and mathematics * Matrix (mathematics), a rectangular array of numbers, symbols or expressions * Matrix (logic), part of a formula in prenex normal form * Matrix (biology), the ...
A, B, and C.


Output

Yes, if A \times B = C; No, otherwise.


Procedure

# Generate an ''n'' × 1
random In common usage, randomness is the apparent or actual lack of definite pattern or predictability in information. A random sequence of events, symbols or steps often has no order and does not follow an intelligible pattern or combination. ...
0/1
vector Vector most often refers to: * Euclidean vector, a quantity with a magnitude and a direction * Disease vector, an agent that carries and transmits an infectious pathogen into another living organism Vector may also refer to: Mathematics a ...
\vec. # Compute \vec = A \times (B \vec) - C\vec. # Output "Yes" if \vec = (0,0,\ldots,0)^T; "No," otherwise.


Error

If A \times B = C, then the algorithm always returns "Yes". If A \times B \neq C, then the probability that the algorithm returns "Yes" is less than or equal to one half. This is called one-sided error. By iterating the algorithm ''k'' times and returning "Yes" only if all iterations yield "Yes", a runtime of O(kn^2) and error probability of \leq 1/2^k is achieved.


Example

Suppose one wished to determine whether: : AB = \begin 2 & 3 \\ 3 & 4 \end \begin 1 & 0 \\ 1 & 2 \end \stackrel \begin 6 & 5 \\ 8 & 7 \end = C. A random two-element vector with entries equal to 0 or 1 is selected say \vec = \begin1 \\ 1\end and used to compute: : \begin A \times (B \vec) - C\vec & = \begin 2 & 3 \\ 3 & 4 \end \left( \begin 1 & 0 \\ 1 & 2 \end \begin1 \\ 1\end \right) - \begin 6 & 5 \\ 8 & 7 \end \begin1 \\ 1\end \\ & = \begin 2 & 3 \\ 3 & 4 \end \begin1 \\ 3\end - \begin11 \\ 15\end \\ & = \begin11 \\ 15\end - \begin11 \\ 15\end \\ & = \begin0 \\ 0\end. \end This yields the zero vector, suggesting the possibility that AB = C. However, if in a second trial the vector \vec = \begin1 \\ 0\end is selected, the result becomes: : A \times (B \vec) - C\vec = \begin 2 & 3 \\ 3 & 4 \end \left( \begin 1 & 0 \\ 1 & 2 \end \begin1 \\ 0\end \right) - \begin 6 & 5 \\ 8 & 7 \end \begin1 \\ 0\end = \begin-1 \\ -1\end. The result is nonzero, proving that in fact AB ≠ C. There are four two-element 0/1 vectors, and half of them give the zero vector in this case (\vec = \begin0 \\ 0\end and \vec = \begin1 \\ 1\end), so the chance of randomly selecting these in two trials (and falsely concluding that AB=C) is 1/22 or 1/4. In the general case, the proportion of ''r'' yielding the zero vector may be less than 1/2, and a larger number of trials (such as 20) would be used, rendering the probability of error very small.


Error analysis

Let ''p'' equal the
probability Probability is a branch of mathematics and statistics concerning events and numerical descriptions of how likely they are to occur. The probability of an event is a number between 0 and 1; the larger the probability, the more likely an e ...
of error. We claim that if ''A'' × ''B'' = ''C'', then ''p'' = 0, and if ''A'' × ''B'' ≠ ''C'', then ''p'' ≤ 1/2.


Case ''A'' × ''B'' = ''C''

: \begin \vec &= A \times (B \vec) - C \vec\\ &= (A \times B)\vec - C\vec\\ &= (A \times B - C)\vec\\ &= \vec \end This is regardless of the value of \vec, since it uses only that A \times B - C = 0. Hence the probability for error in this case is: :\Pr vec \neq 0= 0


Case ''A'' × ''B'' ≠ ''C''

Let D such that :\vec = D \times \vec = (p_1, p_2, \dots, p_n)^T Where :D = A \times B - C = (d_). Since A \times B \neq C, we have that some element of D is nonzero. Suppose that the element d_ \neq 0. By the definition of
matrix multiplication In mathematics, specifically in linear algebra, matrix multiplication is a binary operation that produces a matrix (mathematics), matrix from two matrices. For matrix multiplication, the number of columns in the first matrix must be equal to the n ...
, we have: :p_i = \sum_^n d_r_k = d_r_1 + \cdots + d_r_j + \cdots + d_r_n = d_r_j + y. For some constant y. Using
Bayes' theorem Bayes' theorem (alternatively Bayes' law or Bayes' rule, after Thomas Bayes) gives a mathematical rule for inverting Conditional probability, conditional probabilities, allowing one to find the probability of a cause given its effect. For exampl ...
, we can partition over y: We use that: :\Pr y = 0= \Pr _j = 0= \frac :\Pr y \neq 0= \Pr _j = 1 \land d_=-y\leq \Pr _j = 1= \frac Plugging these in the equation (), we get: : \begin \Pr _i = 0&\leq \frac\cdot \Pr = 0+ \frac\cdot \Pr \neq 0\ &= \frac\cdot \Pr = 0+ \frac\cdot (1 - \Pr = 0\\ &= \frac \end Therefore, :\Pr vec = 0= \Pr _1 = 0 \land \dots \land p_i = 0 \land \dots \land p_n = 0\leq \Pr _i = 0\leq \frac. This completes the proof.


Ramifications

Simple algorithmic analysis shows that the running time of this
algorithm In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
is O(n^2) (in
big O notation Big ''O'' notation is a mathematical notation that describes the asymptotic analysis, limiting behavior of a function (mathematics), function when the Argument of a function, argument tends towards a particular value or infinity. Big O is a memb ...
). This beats the classical deterministic algorithm's runtime of O(n^3) (or O(n^) if using fast matrix multiplication). The error analysis also shows that if the
algorithm In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
is run k times, an
error bound The approximation error in a given data value represents the significant discrepancy that arises when an exact, true value is compared against some approximation derived for it. This inherent error in approximation can be quantified and express ...
of less than 1/2^k can be achieved, an exponentially small quantity. The algorithm is also fast in practice due to wide availability of fast implementations for matrix-vector products. Therefore, utilization of
randomized algorithms A randomized algorithm is an algorithm that employs a degree of randomness as part of its logic or procedure. The algorithm typically uses Uniform distribution (discrete), uniformly random bits as an auxiliary input to guide its behavior, in the ...
can speed up a very slow
deterministic algorithm In computer science, a deterministic algorithm is an algorithm that, given a particular input, will always produce the same output, with the underlying machine always passing through the same sequence of states. Deterministic algorithms are by fa ...
. Freivalds' algorithm frequently arises in introductions to
probabilistic algorithm A randomized algorithm is an algorithm that employs a degree of randomness as part of its logic or procedure. The algorithm typically uses uniformly random bits as an auxiliary input to guide its behavior, in the hope of achieving good performan ...
s because of its simplicity and how it illustrates the superiority of probabilistic algorithms in practice for some problems.


See also

* Schwartz–Zippel lemma


References

* * {{DEFAULTSORT:Freivald's Algorithm Articles containing proofs Matrix theory Randomized algorithms Matrix multiplication algorithms