1

I'm trying to "translate" this Cypher query that uses NetworkX into pure Cypher. Can this even be achieved?

MATCH (n)-[e]->()
WITH collect(n) AS nodes, collect(e) AS edges
CALL wcc.get_components(nodes, edges) YIELD *
RETURN n_components, components;
MasaZo
  • 43
  • 5

1 Answers1

0

When using a neo4j DB, you can use the Graph Data Science Library, which can generate weakly connected components. The library plugin needs to be installed before you can use it.

The GDS library does all its processing in memory, for speed. So you first have to load the desired nodes/resources into memory before performing the wcc processing. For example, to load all the nodes and relationships into memory (only advisable for relatively small datasets):

CALL gds.graph.project.cypher(
  'allData',
  'MATCH (n) RETURN ID(n) AS id',
  'MATCH (n)-->(m) RETURN ID(n) AS source, ID(m) AS target')

Then, to get a wcc result similar to your query's:

CALL gds.wcc.stream('allData') YIELD nodeId, componentId
WITH componentId, COLLECT(nodeId) AS components
RETURN SIZE(components) AS n_components, components
cybersam
  • 63,203
  • 6
  • 53
  • 76