cancel
Showing results for 
Search instead for 
Did you mean: 

Cypher querry to detect redundant paths

hvozdovic_patri
Node Link

Hello,
I am not able to implement a simple query, so I am looking for help here.

How can I get all paths from a graph, where is more than one path from the start node to the end node.
So, I am basically looking for all the duplicities in paths. For example, all possible railway lines from town A to town B, no matter how long they are, how many other cities do we visit. (railway lines are one way in this case).

1 ACCEPTED SOLUTION

jackson1
Node Link

If you're only looking at nodes and relationships with the structure of
(User)->[IS_MEMBER_OF]->(Role)->[HAS]->(AccessRight)
you can try the following query

MATCH path=(u:User)->[:IS_MEMBER_OF]->(:Role)->[:HAS]->(ar:AccessRight)
WITH u, ar, collect(path) as paths
WHERE size(paths) > 1
RETURN u, ar, size(paths) AS numPaths

kevintim's query would be useful for cases where the node/relationship labels along each path could vary, as opposed to having a consistent schema such as (User)->[IS_MEMBER_OF]->(Role)->[HAS]->(AccessRight)

View solution in original post

4 REPLIES 4

Hi,
this is related to variable path length. A query would look somewhat like this:
MATCH paths = (a:Town {name: "Town A"})-[*]->(b:Town {name: "Town B"})
RETURN paths
Also see Patterns - Neo4j Cypher Manual

Thanks for your response.
More specifically, I need to do that on the whole graph with a single query.

I am basically making a risk assessment on an identity management system, which has data stored in the Neo4j database. The simplified schema looks like this:
(User)->[IS_MEMBER_OF]->(Role)->[HAS]->(AccessRight)

My task is to implement a query, that will report all pairs (User, AccessRight), that have more than 1 path between them. Do you have any suggestions for that?

jackson1
Node Link

If you're only looking at nodes and relationships with the structure of
(User)->[IS_MEMBER_OF]->(Role)->[HAS]->(AccessRight)
you can try the following query

MATCH path=(u:User)->[:IS_MEMBER_OF]->(:Role)->[:HAS]->(ar:AccessRight)
WITH u, ar, collect(path) as paths
WHERE size(paths) > 1
RETURN u, ar, size(paths) AS numPaths

kevintim's query would be useful for cases where the node/relationship labels along each path could vary, as opposed to having a consistent schema such as (User)->[IS_MEMBER_OF]->(Role)->[HAS]->(AccessRight)

Thank you.
It is working like expected, but I discovered that in the graph are some direct AccessRights from the User. Therefore I did a small change in the first line of the query.

(u:User)-[*]->(ar:AccessRight)
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.