cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Find loop path

Hi! I want to find all closed circles in a directed connection diagram. That is, a node has a path back to itself through any node. Thank you very much!

8 REPLIES 8

Hi,

You may try using the following code snippet to find the loops in the path.

MATCH p=((n)-[*..10]-(m)) WHERE ID(n)=Id(m) RETURN p

This code uses ID to find the same starting and ending node upto 10 hops. The maximum number of hops can be defined using param tag if required.

Best
Ishwarya

Thank you very much!

Bennu
Graph Fellow

Hi @ruanlovelin !

This is probably one of the most expected functionalities on APOC. You should also consider cycles that are not closing on your starting node tho. I'm sending you an example of a working cypher on a small dummy db.

MATCH (p:NODE {ID: 4})
CALL apoc.path.expandConfig(p, {
	relationshipFilter: "CON>",
    minLevel: 1
})
YIELD path
with relationships(path) as rel, path
UNWIND rel as r
WITH path, rel, endNode(r) as node, count(endNode(r)) as  c
where c> 1
RETURN path
UNION
MATCH (p:NODE {ID: 4})
CALL apoc.path.expandConfig(p, {
	relationshipFilter: "CON>",
    minLevel: 1,
    terminatorNodes: [p]
})
YIELD path
RETURN path

H

I haven't understood it in a short time. I'll ask you if I have any questions .Thank you very much!

Hi!

First part of the union should calculate cycles that doesn't close on initial node. Second part does it.

H

Thank you for your reply, I really appreciate it. I want to query whether a node is in a closed circle,Or query all such circles. Could you tell me the second part solve it or not?

Hi,

Second part will give you cycles that start and close on the node p you define.

H

Thank you very much!