I have a network (e.g. a water network) and I want to find topological structures : clusters (circular paths), bridges (relationships that connect clusters) and trees (the remaining).
The Cypher statement to create the example network is here.(https://www.dropbox.com/s/e1gtqxlm9ngaau5/Cypher%20to%20create%20example%20network.cql?dl=0) The blue relationships are the clusters I am looking for, the red ones the bridges and the green ones the trees.
To find the clusters, I have two approaches, both of which return the correct results. But both are far too slow.
Approach 1: Start from the relations and look if there is a second path between the start and end node. This one takes about 10M db hits
MATCH (n:WN)-[r:PIPE]->(m:WN)
WHERE EXISTS((n)-[r]->(m)-[:PIPE*2..]-(n))
RETURN r
Approach 2: Start by looking for circular paths, ignoring directions. (about 12000) and then extract the unique relationships. This one takes about 20M db hits.
MATCH path=(n:WN)-[:PIPE*..]-(n)
RETURN
apoc.coll.subtract(
apoc.coll.flatten(COLLECT(relationships(path))
),
[]
)
AS clusterRelationships
Is there a smarter approach, returning results faster?