0
votes

I'm struggling for days to find a way for finding all paths (to a maximum length) between two nodes while controlling the path exploration by Neo4j by sorting the relationships that are going to be explored (by one of their properties).

So to be clear, lets say I want to find K best paths between two nodes until a maximum length M. The query will be like:

match (source{name:"source"}), (target{name:"target"}),
p = (source)-[*..M]->(target)
return p order by length(p) limit K;

So far so good. But lets say the relationships of the path have a property called "priority". What I want is to write a query that tells Neo4j on each step of path exploration which relationships should be explored first.

I know that can be possible when I use the java libraries and an embedded database (By implementing PathExpander interface and giving it as input to the GraphAlgoFactory.allSimplePaths() function in Java). But now I'm trying to find a way doing this in a server mode database access using Bolt or REST api.

Is there any way to do this in the server mode? Or maybe using Java libraries functions while accessing the graph in server mode?

2
What about APOC? github.com/neo4j-contrib/…stdob--
I have checked it, it does'nt do what I want, but thanks anyway!Gongotar

2 Answers

1
votes
  1. use labels and an index to find your two start-nodes
  2. perhaps consider allShortestPaths to make it faster

try this:

match (source{name:"source"}), (target{name:"target"}),
p = (source)-[rels:*..20]->(target)
return p, reduce(prio=0, r IN rels | prio + r.priority) as priority 
order by priority ASC, length(p) 
limit 100;
0
votes

I had a very similar problem. I was trying to find the shortest path from one node to all other nodes. I had written a query similar to the one in the answer above (https://stackoverflow.com/a/38030536/783836) and couldn't get it to perform in any reasonable time.

Asking Can Graph DBs perform well with unspecified end nodes? pointed me to the solution: the Single Shortest Path algorithm.

In Neo4j you need to install the Graph Data Science Library and make use of this function: gds.alpha.shortestPath.deltaStepping.stream