Utility functions

This section provides explanations and examples for each of the utility functions in the Neo4j Graph Data Science library.

1. System Functions

Name Description

gds.version

Return the version of the installed Neo4j Graph Data Science library.

Usage:
RETURN gds.version() AS version
Table 1. Results
version

"1.7.2"

2. Numeric Functions

Table 2. Numeric Functions
Name Description

gds.util.NaN

Returns NaN as a Cypher value.

gds.util.infinity

Return infinity as a Cypher value.

gds.util.isFinite

Return true iff the given argument is a finite value (not ±Infinity, NaN, or null).

gds.util.isInfinite

Return true iff the given argument is not a finite value (not ±Infinity, NaN, or null).

2.1. Syntax

Name Parameter

gds.util.NaN()

-

gds.util.infinity()

-

gds.util.isFinite(value: NUMBER)

value to be checked if it is finite

gds.util.isInfinite(value: NUMBER)

value to be checked if it is infinite.

2.2. Examples

Example for gds.util.IsFinite:
UNWIND [1.0, gds.util.NaN(), gds.util.infinity()] AS value
RETURN gds.util.isFinite(value) AS isFinite
Table 3. Results
isFinite

true

false

false

Example for gds.util.isInfinite():
UNWIND [1.0, gds.util.NaN(), gds.util.infinity()] AS value
RETURN gds.util.isInfinite(value) AS isInfinite
Table 4. Results
isInfinite

false

true

true

The utility function gds.util.NaN can be used as an default value for input parameters, as shown in the examples of cosine similarity. A common usage of gds.util.IsFinite and gds.util.IsInfinite is for filtering streamed results, as for instance seen in the examples of gds.alpha.allShortestPaths.

3. Node id functions

Table 5. Node id functions
Name Description

gds.util.asNode

Return the node object for the given node id or null if none exists.

gds.util.asNodes

Return the node objects for the given node ids or an empty list if none exists.

3.1. Syntax

Name Parameters

gds.util.asNode(nodeId: NUMBER)

nodeId of a node in the neo4j-graph

gds.util.asNodes(nodeIds: List of NUMBER)

list of nodeIds of nodes in the neo4j-graph

3.2. Examples

Consider the graph created by the following Cypher statement:

Example graph:
CREATE  (nAlice:User {name: 'Alice'})
CREATE  (nBridget:User {name: 'Bridget'})
CREATE  (nCharles:User {name: 'Charles'})
CREATE  (nAlice)-[:LINK]->(nBridget)
CREATE  (nBridget)-[:LINK]->(nCharles)
Example for gds.util.asNode:
MATCH (u:User{name: 'Alice'})
WITH id(u) AS nodeId
RETURN gds.util.asNode(nodeId).name AS node
Table 6. Results
node

"Alice"

Example for gds.util.asNodes:
MATCH (u:User)
WHERE NOT u.name = 'Charles'
WITH collect(id(u)) AS nodeIds
RETURN [x in gds.util.asNodes(nodeIds)| x.name] AS nodes
Table 7. Results
nodes

[Alice, Bridget]

As many algorithms streaming mode only return the node id, gds.util.asNode and gds.util.asNodes can be used to retrieve the whole node from the neo4j database.