KNearest Neighbors
This section describes the KNearest Neighbors (KNN) algorithm in the Neo4j Graph Data Science library.
1. Introduction
The KNearest Neighbors algorithm computes a distance value for all node pairs in the graph and creates new relationships between each node and its k nearest neighbors. The distance is calculated based on node properties.
The input of this algorithm is a monopartite graph. The graph does not need to be connected, in fact, existing relationships between nodes will be ignored  apart from random walk sampling if that that initial sampling option is used. New relationships are created between each node and its k nearest neighbors.
The KNearest Neighbors algorithm compares given properties of each node.
The k
nodes where these properties are most similar are the knearest neighbors.
The initial set of neighbors is picked at random and verified and refined in multiple iterations.
The number of iterations is limited by the configuration parameter maxIterations
.
The algorithm may stop earlier if the neighbor lists only change by a small amount, which can be controlled by the configuration parameter deltaThreshold
.
The particular implementation is based on Efficient knearest neighbor graph construction for generic similarity measures by Wei Dong et al. Instead of comparing every node with every other node, the algorithm selects possible neighbors based on the assumption, that the neighborsofneighbors of a node are most likely already the nearest one. The algorithm scales quasilinear with respect to the node count, instead of being quadratic.
Furthermore, the algorithm only compares a sample of all possible neighbors on each iteration, assuming that eventually all possible neighbors will be seen.
This can be controlled with the configuration parameter sampleRate
:

A valid sample rate must be in between 0 (exclusive) and 1 (inclusive).

The default value is
0.5
. 
The parameter is used to control the tradeoff between accuracy and runtimeperformance.

A higher sample rate will increase the accuracy of the result.

The algorithm will also require more memory and will take longer to compute.


A lower sample rate will increase the runtimeperformance.

Some potential nodes may be missed in the comparison and may not be included in the result.

When encountered neighbors have equal similarity to the least similar already known neighbor, randomly selecting which node to keep can reduce the risk of some neighborhoods not being explored.
This behavior is controlled by the configuration parameter perturbationRate
.
The output of the algorithm are new relationships between nodes and their knearest neighbors. Similarity scores are expressed via relationship properties.
For more information on this algorithm, see:
Running this algorithm requires sufficient available memory. Before running this algorithm, we recommend that you read Memory Estimation. 
1.1. Similarity metrics
The similarity measure used in the KNN algorithm depends on the type of the configured node properties. KNN supports both scalar numeric values and lists of numbers.
1.1.1. Scalar numbers
When a property is a scalar number, the similarity is computed as follows:
This gives us a number in the range (0, 1]
.
1.1.2. List of integers
When a property is a list of integers, similarity can be measured with either the Jaccard similarity or the Overlap coefficient.
 Jaccard similarity

Figure 2. size of intersection divided by size of union
 Overlap coefficient

Figure 3. size of intersection divided by size of minimum set
Both of these metrics give a score in the range [0, 1]
and no normalization needs to be performed.
Jaccard similarity is used as the default option for comparing lists of integers when the metric is not specified.
1.1.3. List of floatingpoint numbers
When a property is a list of floatingpoint numbers, there are three alternatives for computing similarity between two nodes.
The default metric used is that of Cosine similarity.
 Cosine similarity

Figure 4. dot product of the vectors divided by the product of their lengths
Notice that the above formula gives a score in the range of [1, 1]
.
The score is normalized into the range [0, 1]
by doing score = (score + 1) / 2
.
The other two metrics include the Pearson correlation score and Normalized Euclidean similarity.
 Pearson correlation score

Figure 5. covariance divided by the product of the standard deviations
As above, the formula gives a score in the range [1, 1]
, which is normalized into the range [0, 1]
similarly.
 Euclidean similarity

Figure 6. the root of the sum of the square difference between each pair of elements
The result from this formula is a nonnegative value, but is not necessarily bounded into the [0, 1]
range.
Τo bound the number into this range and obtain a similarity score, we return score = 1 / (1 + distance)
, i.e., we perform the same normalization as in the case of scalar values.
1.1.4. Multiple properties
Finally, when multiple properties are specified, the similarity of the two neighbors is the mean of the similarities of the individual properties, i.e. the simple mean of the numbers, each of which is in the range [0, 1]
, giving a total score also in the [0, 1]
range.
The validity of this mean is highly context dependent, so take care when applying it to your data domain. 
1.1.5. Node properties and metrics configuration
The node properties and metrics to use are specified with the nodeProperties
configuration parameter.
At least one node property must be specified.
This parameter accepts one of:
a single property name 

a Map of property keys to metrics 
nodeProperties: { embedding: 'COSINE', age: 'DEFAULT', lotteryNumbers: 'OVERLAP' } 
list of Strings and/or Maps 
nodeProperties: [ {embedding: 'COSINE'}, 'age', {lotteryNumbers: 'OVERLAP'} ] 
The available metrics by type are:
type  metric 

List of Integer 

List of Float 

For any property type, DEFAULT
can also be specified to use the default metric.
For scalar numbers, there is only the default metric.
1.2. Initial neighbor sampling
The algorithm starts off by picking k
random neighbors for each node.
There are two options for how this random sampling can be done.
 Uniform

The first
k
neighbors for each node are chosen uniformly at random from all other nodes in the graph. This is the classic way of doing the initial sampling. It is also the algorithm’s default. Note that this method does not actually use the topology of the input graph.  Random Walk

From each node we take a depth biased random walk and choose the first
k
unique nodes we visit on that walk as our initial random neighbors. If after some internally definedO(k)
number of steps a random walk,k
unique neighbors have not been visited, we will fill in the remaining neighbors using the uniform method described above. The random walk method makes use of the input graph’s topology and may be suitable if it is more likely to find good similarity scores between topologically close nodes.
The random walk used is biased towards depth in the sense that it will more likely choose to go further away from its previously visited node, rather that go back to it or to a node equidistant to it. The intuition of this bias is that subsequent iterations of comparing neighborofneighbors will likely cover the extended (topological) neighborhood of each node. 
2. Syntax
This section covers the syntax used to execute the KNearest Neighbors algorithm in each of its execution modes. We are describing the named graph variant of the syntax. To learn more about general syntax variants, see Syntax overview.
CALL gds.knn.stream(
graphName: String,
configuration: Map
) YIELD
node1: Integer,
node2: Integer,
similarity: Float
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

yes 
Filter the named graph using the given node labels. 

List of String 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 
Name  Type  Default  Optional  Description 

nodeProperties 
String or Map or List of Strings / Maps 

no 
The node properties to use for similarity computation along with their selected similarity metrics. Accepts a single property key, a Map of property keys to metrics, or a List of property keys and/or Maps, as above. See Node properties and metrics configuration for details. 
topK 
Integer 

yes 
The number of neighbors to find for each node. The Knearest neighbors are returned. This value cannot be lower than 1. 
sampleRate 
Float 

yes 
Sample rate to limit the number of comparisons per node. Value must be between 0 (exclusive) and 1 (inclusive). 
deltaThreshold 
Float 

yes 
Value as a percentage to determine when to stop early. If fewer updates than the configured value happen, the algorithm stops. Value must be between 0 (exclusive) and 1 (inclusive). 
Integer 

yes 
Hard limit to stop the algorithm after that many iterations. 

randomJoins 
Integer 

yes 
Between every iteration, how many attempts are being made to connect new node neighbors based on random selection. 
String 

yes 
The method used to sample the first 

randomSeed 
Integer 

yes 
The seed value to control the randomness of the algorithm.
Note that 
similarityCutoff 
Float 

yes 
Filter out from the list of Knearest neighbors nodes with similarity below this threshold. 
perturbationRate 
Float 

yes 
The probability of replacing the least similar known neighbor with an encountered neighbor of equal similarity. 
Name  Type  Description 


Integer 
Node ID of the first node. 

Integer 
Node ID of the second node. 

Float 
Similarity score for the two nodes. 
CALL gds.knn.stats(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
nodesCompared: Integer,
ranIterations: Integer,
didConverge: Boolean,
nodePairsConsidered: Integer,
similarityPairs: Integer,
similarityDistribution: Map,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

yes 
Filter the named graph using the given node labels. 

List of String 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 
Name  Type  Default  Optional  Description 

nodeProperties 
String or Map or List of Strings / Maps 

no 
The node properties to use for similarity computation along with their selected similarity metrics. Accepts a single property key, a Map of property keys to metrics, or a List of property keys and/or Maps, as above. See Node properties and metrics configuration for details. 
topK 
Integer 

yes 
The number of neighbors to find for each node. The Knearest neighbors are returned. This value cannot be lower than 1. 
sampleRate 
Float 

yes 
Sample rate to limit the number of comparisons per node. Value must be between 0 (exclusive) and 1 (inclusive). 
deltaThreshold 
Float 

yes 
Value as a percentage to determine when to stop early. If fewer updates than the configured value happen, the algorithm stops. Value must be between 0 (exclusive) and 1 (inclusive). 
Integer 

yes 
Hard limit to stop the algorithm after that many iterations. 

randomJoins 
Integer 

yes 
Between every iteration, how many attempts are being made to connect new node neighbors based on random selection. 
String 

yes 
The method used to sample the first 

randomSeed 
Integer 

yes 
The seed value to control the randomness of the algorithm.
Note that 
similarityCutoff 
Float 

yes 
Filter out from the list of Knearest neighbors nodes with similarity below this threshold. 
perturbationRate 
Float 

yes 
The probability of replacing the least similar known neighbor with an encountered neighbor of equal similarity. 
Name  Type  Description 

ranIterations 
Integer 
Number of iterations run. 
didConverge 
Boolean 
Indicates if the algorithm converged. 
nodePairsConsidered 
Integer 
The number of similarity computations. 
preProcessingMillis 
Integer 
Milliseconds for preprocessing the data. 
computeMillis 
Integer 
Milliseconds for running the algorithm. 
postProcessingMillis 
Integer 
Milliseconds for computing similarity value distribution statistics. 
nodesCompared 
Integer 
The number of nodes for which similarity was computed. 
similarityPairs 
Integer 
The number of similarities in the result. 
similarityDistribution 
Map 
Map containing min, max, mean as well as p50, p75, p90, p95, p99 and p999 percentile values of the computed similarity results. 
configuration 
Map 
The configuration used for running the algorithm. 
CALL gds.knn.mutate(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
mutateMillis: Integer,
postProcessingMillis: Integer,
relationshipsWritten: Integer,
nodesCompared: Integer,
ranIterations: Integer,
didConverge: Boolean,
nodePairsConsidered: Integer,
similarityDistribution: Map,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

yes 
Filter the named graph using the given node labels. 

List of String 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

mutateRelationshipType 
String 

no 
The relationship type used for the new relationships written to the projected graph. 
mutateProperty 
String 

no 
The relationship property in the GDS graph to which the similarity score is written. 
Name  Type  Default  Optional  Description 

nodeProperties 
String or Map or List of Strings / Maps 

no 
The node properties to use for similarity computation along with their selected similarity metrics. Accepts a single property key, a Map of property keys to metrics, or a List of property keys and/or Maps, as above. See Node properties and metrics configuration for details. 
topK 
Integer 

yes 
The number of neighbors to find for each node. The Knearest neighbors are returned. This value cannot be lower than 1. 
sampleRate 
Float 

yes 
Sample rate to limit the number of comparisons per node. Value must be between 0 (exclusive) and 1 (inclusive). 
deltaThreshold 
Float 

yes 
Value as a percentage to determine when to stop early. If fewer updates than the configured value happen, the algorithm stops. Value must be between 0 (exclusive) and 1 (inclusive). 
Integer 

yes 
Hard limit to stop the algorithm after that many iterations. 

randomJoins 
Integer 

yes 
Between every iteration, how many attempts are being made to connect new node neighbors based on random selection. 
String 

yes 
The method used to sample the first 

randomSeed 
Integer 

yes 
The seed value to control the randomness of the algorithm.
Note that 
similarityCutoff 
Float 

yes 
Filter out from the list of Knearest neighbors nodes with similarity below this threshold. 
perturbationRate 
Float 

yes 
The probability of replacing the least similar known neighbor with an encountered neighbor of equal similarity. 
Name  Type  Description 

ranIterations 
Integer 
Number of iterations run. 
didConverge 
Boolean 
Indicates if the algorithm converged. 
nodePairsConsidered 
Integer 
The number of similarity computations. 
preProcessingMillis 
Integer 
Milliseconds for preprocessing the data. 
computeMillis 
Integer 
Milliseconds for running the algorithm. 
mutateMillis 
Integer 
Milliseconds for adding properties to the projected graph. 
postProcessingMillis 
Integer 
Milliseconds for computing similarity value distribution statistics. 
nodesCompared 
Integer 
The number of nodes for which similarity was computed. 
relationshipsWritten 
Integer 
The number of relationships created. 
similarityDistribution 
Map 
Map containing min, max, mean, stdDev and p1, p5, p10, p25, p75, p90, p95, p99, p100 percentile values of the computed similarity results. 
configuration 
Map 
The configuration used for running the algorithm. 
CALL gds.knn.write(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
writeMillis: Integer,
postProcessingMillis: Integer,
nodesCompared: Integer,
ranIterations: Integer,
didConverge: Boolean,
nodePairsConsidered: Integer,
relationshipsWritten: Integer,
similarityDistribution: Map,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

yes 
Filter the named graph using the given node labels. 

List of String 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. Also provides the default value for 'writeConcurrency'. 

Integer 

yes 
The number of concurrent threads used for writing the result to Neo4j. 

writeRelationshipType 
String 

no 
The relationship type used to persist the computed relationships in the Neo4j database. 
String 

no 
The relationship property in the Neo4j database to which the similarity score is written. 
Name  Type  Default  Optional  Description 

nodeProperties 
String or Map or List of Strings / Maps 

no 
The node properties to use for similarity computation along with their selected similarity metrics. Accepts a single property key, a Map of property keys to metrics, or a List of property keys and/or Maps, as above. See Node properties and metrics configuration for details. 
topK 
Integer 

yes 
The number of neighbors to find for each node. The Knearest neighbors are returned. This value cannot be lower than 1. 
sampleRate 
Float 

yes 
Sample rate to limit the number of comparisons per node. Value must be between 0 (exclusive) and 1 (inclusive). 
deltaThreshold 
Float 

yes 
Value as a percentage to determine when to stop early. If fewer updates than the configured value happen, the algorithm stops. Value must be between 0 (exclusive) and 1 (inclusive). 
Integer 

yes 
Hard limit to stop the algorithm after that many iterations. 

randomJoins 
Integer 

yes 
Between every iteration, how many attempts are being made to connect new node neighbors based on random selection. 
String 

yes 
The method used to sample the first 

randomSeed 
Integer 

yes 
The seed value to control the randomness of the algorithm.
Note that 
similarityCutoff 
Float 

yes 
Filter out from the list of Knearest neighbors nodes with similarity below this threshold. 
perturbationRate 
Float 

yes 
The probability of replacing the least similar known neighbor with an encountered neighbor of equal similarity. 
Name  Type  Description 

ranIterations 
Integer 
Number of iterations run. 
didConverge 
Boolean 
Indicates if the algorithm converged. 
nodePairsConsidered 
Integer 
The number of similarity computations. 
preProcessingMillis 
Integer 
Milliseconds for preprocessing the data. 
computeMillis 
Integer 
Milliseconds for running the algorithm. 
writeMillis 
Integer 
Milliseconds for writing result data back to Neo4j. 
postProcessingMillis 
Integer 
Milliseconds for computing similarity value distribution statistics. 
nodesCompared 
Integer 
The number of nodes for which similarity was computed. 
relationshipsWritten 
Integer 
The number of relationships created. 
similarityDistribution 
Map 
Map containing min, max, mean, stdDev and p1, p5, p10, p25, p75, p90, p95, p99, p100 percentile values of the computed similarity results. 
configuration 
Map 
The configuration used for running the algorithm. 
The KNN algorithm does not read any relationships, but the values for 
The results are the same as running write mode on a named graph, see write mode syntax above.
To get a deterministic result when running the algorithm:

3. Examples
Consider the graph created by the following Cypher statement:
CREATE (alice:Person {name: 'Alice', age: 24, lotteryNumbers: [1, 3], embedding: [1.0, 3.0]})
CREATE (bob:Person {name: 'Bob', age: 73, lotteryNumbers: [1, 2, 3], embedding: [2.1, 1.6]})
CREATE (carol:Person {name: 'Carol', age: 24, lotteryNumbers: [3], embedding: [1.5, 3.1]})
CREATE (dave:Person {name: 'Dave', age: 48, lotteryNumbers: [2, 4], embedding: [0.6, 0.2]})
CREATE (eve:Person {name: 'Eve', age: 67, lotteryNumbers: [1, 5], embedding: [1.8, 2.7]});
In the example, we want to use the KNearest Neighbors algorithm to compare people based on either their age or a combination on all provided properties.
In the examples below we will use named graphs and native projections as the norm. However, Cypher projections can also be used. 
CALL gds.graph.project(
'myGraph',
{
Person: {
label: 'Person',
properties: ['age','lotteryNumbers','embedding']
}
},
'*'
);
3.1. Memory Estimation
First off, we will estimate the cost of running the algorithm using the estimate
procedure.
This can be done with any execution mode.
We will use the write
mode in this example.
Estimating the algorithm is useful to understand the memory impact that running the algorithm on your graph will have.
When you later actually run the algorithm in one of the execution modes the system will perform an estimation.
If the estimation shows that there is a very high probability of the execution going over its memory limitations, the execution is prohibited.
To read more about this, see Automatic estimation and execution blocking.
For more details on estimate
in general, see Memory Estimation.
CALL gds.knn.write.estimate('myGraph', {
nodeProperties: ['age'],
writeRelationshipType: 'SIMILAR',
writeProperty: 'score',
topK: 1
})
YIELD nodeCount, bytesMin, bytesMax, requiredMemory
nodeCount  bytesMin  bytesMax  requiredMemory 

5 
2040 
3096 
"[2040 Bytes ... 3096 Bytes]" 
3.2. Stream
In the stream
execution mode, the algorithm returns the similarity score for each relationship.
This allows us to inspect the results directly or postprocess them in Cypher without any side effects.
For more details on the stream
mode in general, see Stream.
CALL gds.knn.stream('myGraph', {
topK: 1,
nodeProperties: ['age'],
// The following parameters are set to produce a deterministic result
randomSeed: 1337,
concurrency: 1,
sampleRate: 1.0,
deltaThreshold: 0.0
})
YIELD node1, node2, similarity
RETURN gds.util.asNode(node1).name AS Person1, gds.util.asNode(node2).name AS Person2, similarity
ORDER BY similarity DESCENDING, Person1, Person2
Person1  Person2  similarity 

"Alice" 
"Carol" 
1.0 
"Carol" 
"Alice" 
1.0 
"Bob" 
"Eve" 
0.14285714285714285 
"Eve" 
"Bob" 
0.14285714285714285 
"Dave" 
"Eve" 
0.05 
We use default values for the procedure configuration parameter for most parameters.
The randomSeed
and concurrency
is set to produce the same result on every invocation.
The topK
parameter is set to 1 to only return the single nearest neighbor for every node.
Notice that the similarity between Dave and Eve is very low.
Setting the similarityCutoff
parameter to 0.10 will filter the relationship between them, removing it from the result.
3.3. Stats
In the stats
execution mode, the algorithm returns a single row containing a summary of the algorithm result.
This execution mode does not have any side effects.
It can be useful for evaluating algorithm performance by inspecting the computeMillis
return item.
In the examples below we will omit returning the timings.
The full signature of the procedure can be found in the syntax section.
For more details on the stats
mode in general, see Stats.
CALL gds.knn.stats('myGraph', {topK: 1, concurrency: 1, randomSeed: 42, nodeProperties: ['age']})
YIELD nodesCompared, similarityPairs
nodesCompared  similarityPairs 

5 
5 
3.4. Mutate
The mutate
execution mode extends the stats
mode with an important side effect: updating the named graph with a new relationship property containing the similarity score for that relationship.
The name of the new property is specified using the mandatory configuration parameter mutateProperty
.
The result is a single summary row, similar to stats
, but with some additional metrics.
The mutate
mode is especially useful when multiple algorithms are used in conjunction.
For more details on the mutate
mode in general, see Mutate.
CALL gds.knn.mutate('myGraph', {
mutateRelationshipType: 'SIMILAR',
mutateProperty: 'score',
topK: 1,
randomSeed: 42,
concurrency: 1,
nodeProperties: ['age']
})
YIELD nodesCompared, relationshipsWritten
nodesCompared  relationshipsWritten 

5 
5 
As we can see from the results, the number of created relationships is equal to the number of rows in the streaming example.
The relationships that are produced by the mutation are always directed, even if the input graph is undirected.
If for example 
3.5. Write
The write
execution mode extends the stats
mode with an important side effect: for each pair of nodes we create a relationship with the similarity score as a property to the Neo4j database.
The type of the new relationship is specified using the mandatory configuration parameter writeRelationshipType
.
Each new relationship stores the similarity score between the two nodes it represents.
The relationship property key is set using the mandatory configuration parameter writeProperty
.
The result is a single summary row, similar to stats
, but with some additional metrics.
For more details on the write
mode in general, see Write.
CALL gds.knn.write('myGraph', {
writeRelationshipType: 'SIMILAR',
writeProperty: 'score',
topK: 1,
randomSeed: 42,
concurrency: 1,
nodeProperties: ['age']
})
YIELD nodesCompared, relationshipsWritten
nodesCompared  relationshipsWritten 

5 
5 
As we can see from the results, the number of created relationships is equal to the number of rows in the streaming example.
The relationships that are written are always directed, even if the input graph is undirected.
If for example 
3.6. Calculation with multiple properties
If we want to calculate similarity based on multiple metrics, we can calculate the similarity for each property individually and take their mean. As an example, we can use the Normalized Euclidean similarity metric for the embedding property and the Overlap metric for the lottery numbers property in addition to the age property.
CALL gds.knn.stream('myGraph', {
topK: 1,
nodeProperties: [
{embedding: "EUCLIDEAN"},
'age',
{lotteryNumbers: "OVERLAP"}
],
// The following parameters are set to produce a deterministic result
randomSeed: 1337,
concurrency: 1,
sampleRate: 1.0,
deltaThreshold: 0.0
})
YIELD node1, node2, similarity
RETURN gds.util.asNode(node1).name AS Person1, gds.util.asNode(node2).name AS Person2, similarity
ORDER BY similarity DESCENDING, Person1, Person2
Person1  Person2  similarity 

"Alice" 
"Carol" 
0.931216931216931 
"Carol" 
"Alice" 
0.931216931216931 
"Bob" 
"Carol" 
0.432336103416436 
"Eve" 
"Alice" 
0.366920651602733 
"Dave" 
"Bob" 
0.243466706038683 
Note that the two distinct maps in the query could be merged to a single one.
Was this page helpful?