esjmb
esjmb

Reputation: 137

cypher query for shortest paths between set of nodes

I am trying to work out a cypher query to determine the shortest paths between a set of nodes, such as the set identified as follows,

MATCH (u:User) WITH u ORDER by u.score DESC LIMIT 10 RETURN u

assuming a graph of the form

(:User)-[:Owns]-(:Box)

The first restriction is that I only want to return paths between users matching the first query.

The second restriction is that I do not want to include Box elements where there is a link to only one user in the user set. I am only interested in Box elements where there is more that one matching User who Owns the Box. There may be other Users not selected linked to the Box, but I have no interest in them.

In a sense, it seems to me that I am seeking to extract a subnetwork of all nodes/paths linking the matching user set, but I am new to Cypher/Neo4j and can not work this out.

Any pointers gratefully received.

Upvotes: 3

Views: 776

Answers (1)

Brian Underwood
Brian Underwood

Reputation: 10856

MATCH (u:User)
WITH u ORDER by u.score DESC LIMIT 10
WITH collect(ID(u)) AS user_ids
MATCH (user1:User), (user2:User)
MATCH path=allShortestPaths((user1)-[*0..3]-(user2))
WHERE ID(user1) IN user_ids AND ID(user2) IN user_ids AND user1 <> user2
RETURN path

You can increase the variable path length (3 in this case) but performance may degrade rapidly depending on your network.

The above query doesn't filter out paths which have Box elements which only match greater than one user. It's much easier if you just want the direct links through one box, though I'm not sure if that's what you want. If it is you could just do:

MATCH (u:User)
WITH u ORDER by u.score DESC LIMIT 10
WITH collect(ID(u)) AS user_ids
MATCH path=(user1:User)-[:Owns]-(box:Box)-[:Owns]-(user2:User)
WHERE ID(user1) IN user_ids AND ID(user2) IN user_ids AND user1 <> user2
RETURN path, user1, box, user2

Honestly, maybe that is what you want. You might need to process the results after the query is returned. It depends on what you're doing with them, I suppose ;)

Upvotes: 2

Related Questions