What is the time and space complexity of an algorithm, which calculates the dot product between two vectors with the length n?
相关问题
- Finding k smallest elements in a min heap - worst-
- binary search tree path list
- High cost encryption but less cost decryption
- Set *both* elements and initial capacity of std::v
- How to get a fixed number of evenly spaced points
相关文章
- What is the complexity of bisect algorithm?
- What are the problems associated to Best First Sea
- Coin change DP solution to keep track of coins
- Algorithm for partially filling a polygonal mesh
- How do I get characters common to two vectors in C
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- How can I unpack (destructure) elements from a vec
If the 2 vectors are
a = [a1, a2, ... , an]
andb = [b1, b2, ... , bn]
, thenThe 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)
.