schernichkin
schernichkin

Reputation: 1113

How to take a subgraph of a certain depth starting from a certain vertex in Neo4j

I need to take subgraph of given depth stating from some vertex using bfs. I've tried code similar to following

MATCH (p:Node {id: '1'}) CALL apoc.path.subgraphAll(p, {minLevel: 0, maxLevel: 1}) YIELD nodes, relationships RETURN nodes, relationships;

but it seems to be extremely slow comparing to cypher queries (neo4j 4.3.3) What is the best way to take subgraphs with bfs in neo4j?

Upvotes: 0

Views: 323

Answers (1)

Graphileon
Graphileon

Reputation: 5385

I did some tests with our own test store , and a depth of 4

using apoc

//subgraph apoc 4
MATCH (n) WHERE id(n)=30341
CALL apoc.path.subgraphAll(n, {
    minLevel: 0,
    maxLevel: 4
})
YIELD nodes, relationships
RETURN size(nodes),size(relationships)

Results with apoc

getting the same result in cypher requires this query

// subgraph cypher 4
MATCH path=(n)-[*0..4]-(m) WHERE id(n)=30341
UNWIND nodes(path) AS node
UNWIND relationships(path) AS rel
WITH COLLECT ( DISTINCT node) AS pathNodes,
     COLLECT ( DISTINCT rel ) AS pathRels
MATCH (n1)-[r]-(n2)
WHERE n1 IN pathNodes AND n2 IN pathNodes 
      AND id(n1) >= id(n2)
      AND NOT r IN pathRels
WITH pathNodes,pathRels, COLLECT(r) AS rels
RETURN size(pathNodes),size(pathRels+rels)

Results cypher

which takes a lot longer.

Upvotes: 1

Related Questions