ArticleslgStudy

computer science

Freivalds' algorithm

Freivalds' algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Freivalds' algorithm rather than just read about it. In short: 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.

Key takeaways

  • Freivalds' algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Freivalds' algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Freivalds' algorithm from memory before moving on to harder problems.

Reference excerpt

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) with high probability. In O(kn2) time the algorithm can verify a matrix product with probability of failure less than 2−k.

The algorithm

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

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

Procedure Generate an n × 1 random 0/1 vector r→. Compute P→=A×(Br→)−Cr→. 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(kn2) and error probability of ≤1/2k is achieved.

Example 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]=[−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→=[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 Let p equal the probability 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 P→=A×(Br→)−Cr→=(A×B)r→−Cr→=(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 Call D this matrix

D=A×B−C=(dij). Since A×B≠C, we have that at least one element of D is nonzero. Call i and j the indices of one such element, which is therefore dij≠0. Without loss of generality assume that there are no other nonzero elements. Call P this matrix (note that the symbols P and pi are unrelated to the similar symbol p denoting the probability)

P→=D×r→=(p1,p2,…,pn)T All the terms of the matrix multiplication are zero except for dij, so there is a constant y such that

pi=∑k=1ndikrk=di1r1+⋯+dijrj+⋯+dinrn=dijrj+y. Using Bayes' theorem, we can partition over y:

We use that:

Pr⁡[pi=0|y=0]=Pr⁡[rj=0]=12 Pr⁡[pi=0|y≠0]=Pr⁡[rj=1∧dij=−y]≤Pr⁡[rj=1]=12 Plugging these in the equation (1), we get:

Pr⁡[pi=0]≤12⋅Pr⁡[y=0]+12⋅Pr⁡[y≠0]=12⋅Pr⁡[y=0]+12⋅(1−Pr⁡[y=0])=12 Therefore,

Pr⁡[P→=0]=Pr⁡[p1=0∧⋯∧pi=0∧⋯∧pn=0]≤Pr⁡[pi=0]≤12. This completes the proof.

Ramifications 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 Schwartz–Zippel lemma

References

Worked examples

Example 1 — a first encounter with Freivalds' algorithm

Start with the simplest possible case. Write down what Freivalds' algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Freivalds' algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Freivalds' algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Freivalds' algorithm

In research
Freivalds' algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Freivalds' algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Freivalds' algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Matrix multiplication algorithms, Matrix theory, Randomized algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Freivalds' algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Freivalds' algorithm” →

How to study Freivalds' algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Freivalds' algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Freivalds' algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Freivalds' algorithm in simple terms?

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.

Why does Freivalds' algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Freivalds' algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Freivalds' algorithm.

Tags

  • Matrix multiplication algorithms
  • Matrix theory
  • Randomized algorithms

Keep exploring