开发者

Time and space complexity of vector dot-product computation

What is the time and space complexity of an algorithm, wh开发者_JAVA技巧ich calculates the dot product between two vectors with the length n?


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜