Multiple relationships match in circular path

Hi,

i need to find circular paths.

I've started with this query thats gives me the 1st circular path and is working ok.

MATCH (g1:Perception_Group)-[s1:SEQUENCE]-(g2:Perception_Group)-[s2:SEQUENCE]-(g3:Perception_Group)-[s3:SEQUENCE]-(g4:Perception_Group)
WHERE  g1=g4
RETURN g1,g2,g3,g4,s1,s2,s3 LIMIT 1

But since i need to match paths with more than 3 relationships i came up with the following query where i can adjust *3 for longer paths. This one is not working. Can anyone tell me why ?

MATCH (g1:Perception_Group)-[s:SEQUENCE*3]-(g2:Perception_Group)
WHERE  g1=g2
RETURN g1,g2,s LIMIT 1

I am also trying the following one but i don't know how to match 1st node to last node, and also to separate results since the apoc function return results as a chunk.


MATCH (pg:Perception_Group)
CALL apoc.path.subgraphAll(pg, {
    relationshipFilter: "SEQUENCE",
    minLevel: 0,
    maxLevel: 3
})
YIELD nodes, relationships
RETURN nodes, relationships LIMIT 1;

Hey @a10554, try to use a directional relationship. Maybe because of the bidirectional relationship you are not getting the desired answer.

Hi rushikesh,

unfortunately i can't guarantee directional relationships since some of them must loosely connect nodes because of the context of the application.

Found an older post (https://stackoverflow.com/questions/42260067/cypher-query-to-find-circular-reference) with a solution like the following that looks promising. After getting the cycles, for each of the them i need to create a node that connects all the nodes of the circular path making a star.

MATCH (pg:Perception_Group)
WHERE SIZE((pg)<-[:SEQUENCE]-()) <> 0 
AND SIZE(()<-[:SEQUENCE]-(pg)) <> 0
MATCH path = (pg)<-[:SEQUENCE*]-(pg) 
RETURN pg, path LIMIT 1
1 Like

I'm still fighting it. Got this one to create the new node and relationships but i'm still missing something, because it is not creating only one "Function" node per circular path.


MATCH (pg:Perception_Group)
WHERE SIZE((pg)<-[:SEQUENCE]-()) <> 0
AND SIZE(()<-[:SEQUENCE]-(pg)) <> 0
MATCH path = (pg)<-[:SEQUENCE*]-(pg)
WITH collect(path) as paths
FOREACH(path in paths |
  FOREACH(pg in nodes(path) |
    MERGE (pg)-[s:STEP]-(f:Function)))

This following cypher is working better, but still has a problem, because it is returning all the different permutations of a circular path.


MATCH (pg:Perception_Group)
WHERE SIZE((pg)<-[:SEQUENCE]-()) <> 0
AND SIZE(()<-[:SEQUENCE]-(pg)) <> 0
MATCH path = (pg)<-[:SEQUENCE*]-(pg)
WITH collect(path) as paths
FOREACH (path in paths |
  CREATE (f:Function)
  FOREACH(pg in nodes(path) |
    MERGE (pg)-[s:STEP]-(f)))