Euclidean Distance

Euclidean distance measures the straight line distance between two points in n-dimensional space. The algorithm takes two vectors denoted by ListAccum and return the Euclidean distance between them.

This algorithm is implemented as a user-defined function. You need to follow the steps in Add a User-Defined Function to add the function to GSQL. After adding the function, you can call it in any GSQL query in the same way as a built-in GSQL function.

Specifications

tg_euclidean_distance_accum(A, B)

Time complexity

The algorithm has a complexity of \$O(n)\$, where \$n\$ is the number of dimensions of the vectors.

Parameters

Name Description Data type

A

An n-dimensional vector denoted by a ListAccum of length n

ListAccum<INT/UINT/FLOAT/DOUBLE>

B

An n-dimensional vector denoted by a ListAccum of length n

ListAccum<INT/UINT/FLOAT/DOUBLE>

Return value

The Euclidean distance between the two vectors.

Example

  • Query

  • Result

CREATE QUERY euclidean_example() FOR GRAPH social {
  ListAccum<INT> @@a = [1, 2, 3];
  ListAccum<INT> @@b = [4, 5, 6];
  double distance = tg_euclidean_distance_accum(@@a, @@b);
  PRINT distance;
}
{
    "distance": 5.19615
}