Freivalds' algorithm

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Freivalds' algorithm (named after Rūsiņš Mārtiņš Freivalds) is a probabilistic randomized algorithm used to verify matrix multiplication. Given three n × n matrices A, B, and C, a general problem is to verify whether A×B=C. A naïve algorithm would compute the product A×B explicitly and compare term by term whether this product equals C. However, the best known matrix multiplication algorithm runs in O(n2.372) time. Freivalds' algorithm utilizes randomization in order to reduce this time bound to O(n2)[1] with high probability. In O(kn2) time the algorithm can verify a matrix product with probability of failure less than 2k.

The algorithm

[edit | edit source]

Input

[edit | edit source]

Three n × n matrices A, B, and C.

Output

[edit | edit source]

Yes, if A×B=C; No, otherwise.

Procedure

[edit | edit source]
  1. Generate an n × 1 random 0/1 vector r.
  2. Compute P=A×(Br)Cr.
  3. Output "Yes" if P=(0,0,,0)T; "No," otherwise.

Error

[edit | edit source]

If A×B=C, then the algorithm always returns "Yes". If A×BC, 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(kn2) and error probability of 1/2k is achieved.

Example

[edit | edit source]

Suppose one wished to determine whether:

AB=[2334][1012]=?[6587]=C.

A random two-element vector with entries equal to 0 or 1 is selected – say r=[11] – and used to compute:

A×(Br)Cr=[2334]([1012][11])[6587][11]=[2334][13][1115]=[1115][1115]=[00].

This yields the zero vector, suggesting the possibility that AB = C. However, if in a second trial the vector r=[10] is selected, the result becomes:

A×(Br)Cr=[2334]([1012][10])[6587][10]=[11].

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 (r=[00] and r=[11]), 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

[edit | edit source]

Let p equal the probability of error. We claim that if A × B = C, then p = 0, and if A × BC, then p ≤ 1/2.

Case A × B = C

[edit | edit source]
P=A×(Br)Cr=(A×B)rCr=(A×BC)r=0

This is regardless of the value of r, since it uses only that A×BC=0. Hence the probability for error in this case is:

Pr[P0]=0

Case A × BC

[edit | edit source]

Let D such that

P=D×r=(p1,p2,,pn)T

Where

D=A×BC=(dij).

Since A×BC, we have that some element of D is nonzero. Suppose that the element dij0. By the definition of matrix multiplication, we have:

pi=k=1ndikrk=di1r1++dijrj++dinrn=dijrj+y.

For some constant y. Using Bayes' theorem, we can partition over y:

We use that:

Pr[pi=0|y=0]=Pr[rj=0]=12
Pr[pi=0|y0]=Pr[rj=1dij=y]Pr[rj=1]=12

Plugging these in the equation (1), we get:

Pr[pi=0]12Pr[y=0]+12Pr[y0]=12Pr[y=0]+12(1Pr[y=0])=12

Therefore,

Pr[P=0]=Pr[p1=0pi=0pn=0]Pr[pi=0]12.

This completes the proof.

Ramifications

[edit | edit source]

Simple algorithmic analysis shows that the running time of this algorithm is O(n2) (in big O notation). This beats the classical deterministic algorithm's runtime of O(n3) (or O(n2.372) if using fast matrix multiplication). The error analysis also shows that if the algorithm is run k times, an error bound of less than 1/2k 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 can speed up a very slow deterministic algorithm.

Freivalds' algorithm frequently arises in introductions to probabilistic algorithms because of its simplicity and how it illustrates the superiority of probabilistic algorithms in practice for some problems.

See also

[edit | edit source]

References

[edit | edit source]
  1. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
  • Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
  • Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).