0

When doing a Cypher query to retrieve a specific subgraph with automorphisms, let's say

MATCH (a)-[:X]-(b)-[:X]-(c),
RETURN a, b, c

It seems that the default behaviour is to return every retrieved subgraph and all their automorphisms. In that exemple, if (u)-[:X]-(v)-[:X]-(w) is a graph matching the pattern, the output will be u,v,w but also w,v,u, which consist in the same graph.

Is there a way to retrieve each subgraph only once ?

EDIT: It would be great if Cypher have a feature to do that in the search, using some kind of symmetry breaking condition as it would reduce the computing time. If that is not the case, how would you post-process to find the desired output ?

Alexis Pister
  • 449
  • 3
  • 13

2 Answers2

1

In the query you are making, (a)-[r:X]-(b) and (a)-[t:X]-(c) refer to a similar pattern. Since (b) and (c) can be interchanged. What is the need to repeat matching twice? MATCH (a)-[r:X]-(b) RETURN a, r, b returns all the subgraphs you are looking for.

EDIT

You can do something as follows to find the nodes, which are having two relations of type X.

MATCH (a)-[r:X]-(b) WHERE size((a)-[:X]-()) = 2 RETURN a, r, b
m0hithreddy
  • 1,752
  • 1
  • 10
  • 17
0

For these kind of mirrored patterns, we can add a restriction on the internal graph ids so only one of the two paths is kept:

MATCH (a)-[:X]-(b)-[:X]-(c)
WHERE id(a) < id(c)
RETURN a, b, c

This will also prevent the case where a = c.

InverseFalcon
  • 29,576
  • 4
  • 38
  • 51