Supriya Ghosh (Editor)

Freivalds' algorithm

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

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 ( n 2.3729 ) time. Freivalds' algorithm utilizes randomization in order to reduce this time bound to O ( n 2 ) with high probability. In O ( k n 2 ) time the algorithm can verify a matrix product with probability of failure less than 2 k .

Contents

Input

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

Output

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

Procedure

  1. Generate an n × 1 random 0/1 vector r .
  2. Compute P = A × ( B r ) C r .
  3. Output "Yes" if P = ( 0 , 0 , , 0 ) T ; "No," otherwise.

Error

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

Example

Suppose one wished to determine whether:

A B = [ 2 3 3 4 ] [ 1 0 1 2 ] = ? [ 6 5 8 7 ] = C .

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

A × ( B r ) C r = [ 2 3 3 4 ] ( [ 1 0 1 2 ] [ 1 1 ] ) [ 6 5 8 7 ] [ 1 1 ] = [ 2 3 3 4 ] [ 1 3 ] [ 11 15 ] = [ 11 15 ] [ 11 15 ] = [ 0 0 ] .

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

A × ( B r ) C r = [ 2 3 3 4 ] ( [ 1 0 1 2 ] [ 1 0 ] ) [ 6 5 8 7 ] [ 1 0 ] = [ 1 1 ] .

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 = [ 0 0 ] and r = [ 1 1 ] ), 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 of error. We claim that if A × B = C, then p = 0, and if A × BC, then p ≤ 1/2.

Case A × B = C

P = A × ( B r ) C r = ( A × B ) r C r = ( A × B C ) r = 0

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

Pr [ P 0 ] = 0

Case A × B ≠ C

Let

P = D × r = ( p 1 , p 2 , , p n ) T

Where

D = A × B C = ( d i j ) .

Since A × B C , we have that some element of D is nonzero. Suppose that the element d i j 0 . By the definition of matrix multiplication, we have:

p i = k = 1 n d i k r k = d i 1 r 1 + + d i j r j + + d i n r n = d i j r j + y .

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

We use that:

Pr [ p i = 0 | y = 0 ] = Pr [ r j = 0 ] = 1 2 Pr [ p i = 0 | y 0 ] = Pr [ r j = 1 d i j = y ] Pr [ r j = 1 ] = 1 2

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

Pr [ p i = 0 ] 1 2 Pr [ y = 0 ] + 1 2 Pr [ y 0 ] = 1 2 Pr [ y = 0 ] + 1 2 ( 1 Pr [ y = 0 ] ) = 1 2

Therefore,

Pr [ P = 0 ] = Pr [ p 1 = 0 p i = 0 p n = 0 ] Pr [ p i = 0 ] 1 2 .

This completes the proof.

Ramifications

Simple algorithmic analysis shows that the running time of this algorithm is O(n2), beating the classical deterministic algorithm's bound of O(n3). The error analysis also shows that if we run our algorithm k times, we can achieve an error bound of less than 1 2 k , 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. In fact, the best known deterministic matrix multiplication verification algorithm known at the current time is a variant of the Coppersmith–Winograd algorithm with an asymptotic running time of O(n2.3729).

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

References

Freivalds' algorithm Wikipedia


Similar Topics