Utility functions

Numeric Functions

Since Neo4j 5, this functionality can also be expressed directly in Cypher using the Inf, Infinity, and NaN literals and the isNaN() function.
Name Parameter Description

gds.util.NaN()

-

Returns NaN as a Cypher value.

gds.util.infinity()

-

Returns Infinity as a Cypher value.

gds.util.isFinite(value: Number)

value to be checked if it is finite.

Return false if the given argument is ±Infinity, NaN, or null.

gds.util.isInfinite(value: Number)

value to be checked if it is infinite.

Returns true if the given argument is ±Infinity, NaN, or null.

Examples

gds.util.isFinite

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

true

false

false

gds.util.isInfinite

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

false

true

true

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.allShortestPaths.

Node id functions

Results in GDS often contain node IDs. You can use the following functions to connect IDs to nodes in the graph.

Name Parameters Description

gds.util.asNode(nodeId: Number)

nodeId of a node in the neo4j-graph

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

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

list of nodeIds of nodes in the neo4j-graph

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

Examples

All the examples below should be run in an empty database.

The examples use Cypher projections as the norm. Native projections will be deprecated in a future release.

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)
Project the graph into GDS:
MATCH (n:User)-[r:LINK]->(m:User)
RETURN gds.graph.project('socialGraph', n, m)

AsNode

Example for gds.util.asNode using Degree Centrality:
CALL gds.degree.stream('socialGraph')
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS node, score
ORDER BY score DESC
Table 3. Results
node score

"Alice"

1.0

"Bridget"

1.0

"Charles"

0.0

AsNodes

Example for gds.util.asNodes using Breadth-First-Search (BFS):
MATCH (alice {name: 'Alice'})
CALL gds.bfs.stream('socialGraph', {sourceNode: alice, maxDepth: 1})
YIELD sourceNode, nodeIds
RETURN [x in gds.util.asNodes(nodeIds)| x.name] AS nodes
Table 4. Results
nodes

["Alice", "Bridget"]