Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time and space complexity of vector dot-product computation

What is the time and space complexity of an algorithm, which calculates the dot product between two vectors with the length n?

like image 516
Gæst Avatar asked Sep 19 '10 00:09

Gæst


People also ask

How do you calculate the dot product?

bn> we can find the dot product by multiplying the corresponding values in each vector and adding them together, or (a1 * b1) + (a2 * b2) + (a3 * b3) .... + (an * bn). We can calculate the dot product for any number of vectors, however all vectors must contain an equal number of terms.

How do you find the time complexity of a matrix multiplication?

The standard way of multiplying an m-by-n matrix by an n-by-p matrix has complexity O(mnp). If all of those are "n" to you, it's O(n^3), not O(n^2).

How do you find the dot product of two vectors?

First find the magnitude of the two vectors a and b, i.e., |→a| | a → | and |→b| | b → | . Secondly, find the cosine of the angle θ between the two vectors. Finally take a product of the magnitude of the two vectors and the and cosine of the angle between the two vectors, to obtain the dot product of the two vectors.


1 Answers

If the 2 vectors are a = [a1, a2, ... , an] and b = [b1, b2, ... , bn], then

The dot-product is given by a.b = a1 * b1 + a2 * b2 + ... + an * bn

To compute this, we must perform n multiplications and (n-1) additions. (I assume that this is the dot-product algorithm you are referring to).

Assuming that multiplication and addition are constant-time operations, the time-complexity is therefore O(n) + O(n) = O(n).

The only auxiliary space we require during the computation is to hold the 'partial dot-product so far' and the last product computed, i.e. ai * bi.

Assuming we can hold both values in constant-space, the space complexity is therefore O(1) + O(1) = O(1).

like image 156
Ani Avatar answered Sep 23 '22 15:09

Ani