I have n vectors, each with m elements (real number). I want to find the pair where there cosine similarity is maximum among all pairs.
The straightforward solution
Cosine similarity sim(a,b)
is related to Euclidean distance |a - b|
by
|a - b|² = 2(1 - sim(a,b))
for unit vectors a
and b
.
That means cosine similarity is greatest when Euclidean distance is smallest after normalizing by the L2 norm, and the problem reduces to the closest pair of points problem, which can be solved in O(n lg n) time.