Reputation: 68
Given the example found here: http://console.neo4j.org/?id=qzjrxu, how would I identify nodes with relationships that end back to the starting node?
IE: joe -> bill -> tom -> joe AND matt -> matt
Thanks for the help.
Upvotes: 4
Views: 2379
Reputation: 77
Depending on your problem and size of your graph you could try apoc.nodes.cycles
from the apoc extension.
Documentation of that function
With that you could run something like:
MATCH (e)
WITH collect(e) AS e_list
CALL apoc.nodes.cycles(e_list, {relTypes: ['XYZ'], maxDepth: 100})
YIELD path RETURN path
It is better to reduce the number of nodes beforehand by filtering etc., especially with a large database.
Upvotes: 0
Reputation: 30417
For small to medium graphs, this should return nodes suffering from a circular reference and the path itself:
MATCH (e)
WHERE SIZE((e)<-[:ManagedBy]-()) <> 0
AND SIZE(()<-[:ManagedBy]-(e)) <> 0
MATCH path = (e)<-[:ManagedBy*]-(e)
RETURN e, path
EDIT
I made a small change to first filter out nodes which do not have incoming and outgoing :ManagedBy relationships, those will never have a cycle.
Also it's highly recommended to use labels to reduce the nodes processed to the smallest set.
Upvotes: 4