Graph Data Science algorithms

Neo4j · Expert · 15 min

Centrality, communities and similarity power recommendations and fraud detection, and plain Cypher cannot compute them efficiently.

The Graph Data Science (GDS) library is a Neo4j plugin (included in Aura Graph Analytics and Desktop) providing parallel implementations of more than 60 algorithms. Its workflow differs from normal Cypher:

  • project a graph into memory: gds.graph.project('name', nodeLabels, relTypes, config) builds a compact in-memory copy of just the nodes and relationships you need, optionally with properties as weights
  • run an algorithm in one of four modes: stream (return results as rows), stats (summary only), mutate (write results into the in-memory graph for use by other algorithms), write (persist as properties back into the database)
  • use estimate first on large graphs to check memory, and gds.graph.drop afterwards

Popular algorithm families:

  • centrality: PageRank, betweenness, degree, to find influential or bridging nodes
  • community detection: Louvain, Leiden, label propagation, weakly connected components for clusters and fraud rings
  • similarity: node similarity and k-nearest neighbours for "people like you" recommendations
  • path finding: Dijkstra, A* and Yen's k-shortest for weighted routes
  • embeddings (FastRP, GraphSAGE) and link prediction pipelines for machine learning

Results are node ids; gds.util.asNode(nodeId) maps them back to database nodes. GDS has a Python client (graphdatascience) that wraps the same procedures.

Example

// 1. project Person nodes and FOLLOWS relationships into memory
CALL gds.graph.project('social', 'Person', 'FOLLOWS');

// 2. stream PageRank scores: most influential people first
CALL gds.pageRank.stream('social')
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, round(score, 3) AS score
ORDER BY score DESC LIMIT 5;

// 3. find communities and store them on the nodes
CALL gds.louvain.write('social', {writeProperty: 'community'})
YIELD communityCount, modularity;

// 4. free the memory
CALL gds.graph.drop('social');

Remember

  • GDS projects a graph into memory, then runs parallel algorithms on it
  • Modes: stream, stats, mutate, write; estimate memory first
  • Centrality, communities, similarity, weighted paths and embeddings
  • Map node ids back with gds.util.asNode

Watch out for

  • Forgetting to drop projected graphs, leaving gigabytes of heap occupied.

Check yourself

  1. What does the 'write' execution mode of a GDS algorithm do?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: neo4j.com/docs/graph-data-science/current/