14

What is the best way to detect for cycles in a graph of a considerable size using cypher.

I have a graph which has about 250000 nodes and about 270000 relationship and I would like to detect cycles in sub graph of about 10k nodes and involving 100k relationships. The cypher I have written is like

start 
      n = node:node_auto_index(some lucene query that returns about 10k nodes)
match
    p =  n-[:r1|r2|r3*]->n
return p

However this is not turning out to be very efficient.

Can somebody suggest a better way to do this.

Charchit Kapoor
  • 8,934
  • 2
  • 8
  • 24
Amit
  • 435
  • 3
  • 13

2 Answers2

0

1) Count un-flagged nodes
2) Flag nodes with no outgoing relations (leaves)
3) Flag nodes with no incoming relations (roots)
4) If any nodes were flagged in 2 or 3 return to step 1

5) If un-flagged nodes remain you have at least 1 cycle

the cycle(s) will be in the set of un-flagged nodes
looking at nodes with fewest [in|out] edges may help
if there are still too many to identify cycle

tomc
  • 1,146
  • 6
  • 10
0

The query you are using at the moment will return all paths for each node. Changing it to

start 
      n = node:node_auto_index(some lucene query that returns about 10k nodes)
where (n)-[*]->(n)
return distinct p, n

will let Neo4j stop once it finds a path for a given node. It will probably still be slow, but definitely less so.

Simon Thordal
  • 893
  • 10
  • 28