Find next shortest path in neo4j - neo4j

I want to find a couple of paths between 2 nodes. I don't just want the shortest path or all paths with the shortest length (allShortestPaths). I need all the shortest paths and the next shortest paths. Since it is not possible to set allShortestPaths with minimal length different from 0/1.
"allShortestPaths(...) does not support a minimal length different from 0 or 1"
It could be solved with projections or apoc.path.expandConfig, however we have a database with 14 million nodes and 56 million relationships, so when I tried doing this, we could deliver paths with lengths of 4 in 20-30 seconds and lengths of more than 4 was not possible. Is there a way to find a subset of all paths that are shortest, but also fast to execute?
Lets say my source node is A, and target node is F, and I wanted the 3 shortest paths, the 3 paths would then be:
(A-f), (A-C-F), (A-B-F)

I figured that yen's algorithm would be very suitable after I tried using projections (which was pretty fast)
https://neo4j.com/docs/graph-data-science/current/algorithms/yens/

Related

Shortest path with stopover in circular graph

I have a directed (weighted) circular graph like this:
I would like to find the shortest path through a number of nodes with same start and end node. For example I would like to find the shortest path starting from node 0 going through node 3 and 4 and ending again at node 0. It gets a little more complicated due to constraints e.g. node 4 should always be visited after node 3. I visualized this constraint here:
Is there any algorithm to solve this problem?

How to know if a tree is colorable (RB Tree)

is there an algorithm to know if a tree is colorable or not? Because I found this phrase on Wikipedia:
The path from the root to the farthest leaf is no more than twice as long as the path from the root to the nearest leaf.
So, for example, taking in exam these two trees:
In the first one the shortest path from the root is the one on the left and it has a distance of 1 from the root. The longest one is on the right and has a distance of 3. So 3 is not <= 2*1, so the tree on the left is not colorable, right?
In the second tree the shortest path takes 2 nodes and the fastest takes 2 nodes too. 2 <= 2*2 so it is colorable I guess.
Exactly, to know if a tree is colorable or not you must count from the root (excluding it) and then go through the longest path. Then, you will have the answer

Calculating a full matrix of shortest path-lengths between all nodes

We are trying to find a way to create a full distance matrix in a neo4j database, where that distance is defined as the length of the shortest path between any two nodes. Of course, there is the shortestPath method but using a loop going through all pairs of nodes and calculating their shortestPaths get very slow. We are explicitely not talking about allShortestPaths, because that returns all shortest paths between 2 specific nodes.
Is there a specific method or approach that is fast for a large number of nodes (>30k)?
Thank you!
j.
There is no easier method; the full distance matrix will take a long time to build.
As you've described it, the full distance matrix must contain the shortest path between any two nodes, which means you will have to get that information at some point. Iterating over each pair of nodes and running a shortest-path algorithm is the only way to do this, and the complexity will be O(n) multiplied by the complexity of the algorithm.
But you can cut down on the runtime with a dynamic programming solution.
You could certainly leverage some dynamic programming methods to cut down on the calculation time. For instance, if you are trying to find the shortest path between (A) and (C), and have already calculated the shortest from (B) to (C), then if you happen to encounter (B) while pathfinding from (A), you do not need to recalculate the rest of the cost of that path; it is known.
However, creating a dynamic programming solution of any reasonable complexity will almost certainly be best done in a separate module for Neo4J that is thrown in into a plugin. If what you are doing is a one-time operation or an operation that won't be run frequently, it might be easier to just do the naive solution of calling shortestPath between each pair, but if you plan to be running it fairly frequently on dynamic data, it might be worth authoring a custom plugin. It totally depends on your needs.
No matter what, though, it will take some time to calculate. The dynamic programming solution will cut down on the time greatly (especially in a densely-connected graph), but it will still not be very fast.
What is the end game? Is this a one-time query that resets some property or creates new edges. Or a recurring frequent effort. If it's one-time, you might create edges between the two nodes at each step creating a transitive closure environment. The edge would point between the two nodes and have, as a property, the distance.
Thus, if the path is a>b>c>d, you would create the edges
a>b 1
a>c 2
a>d 3
b>c 1
b>d 2
c>d 1
The edges could be named distinctively to distinguish them from the original path edges. This could create circular paths, which may neither negate this strategy or need a constraint. if you are dealing with directed acyclic graphs it would work well.

Efficiently Finding all paths between 2 nodes in a directed graph - RGL Gem

I am struggling to find 1 efficient algorithm which will give me all possible paths between 2 nodes in a directed graph.
I found RGL gem, fastest so far in terms of calculations. I am able to find the shortest path using the Dijkstras Shortest Path Algorithm from the gem.
I googled, inspite of getting many solutions (ruby/non-ruby), either couldn't convert the code or the code is taking forever to calculate (inefficient).
I am here primarily if someone can suggest to find all paths using/tweaking various algorithms from RGL gem itself (if possible) or some other efficient way.
Input of directed graph can be an array of arrays..
[[1,2], [2,3], ..]
P.S. : Just to avoid negative votes/comments, unfortunately I don't have inefficient code snippet to show as I discarded it days ago and didn't save it anywhere for the record or reproduce here.
The main problem is that the number of paths between two nodes grows exponentially in the number of overall nodes. Thus any algorithm finding all paths between two nodes, will be very slow on larger graphs.
Example:
As an example imagine a grid of n x n nodes each connected to their 4 neighbors. Now you want to find all paths from the bottom left node to the top right node. Even when you only allow for moves to the right (r) and moves up (u) your resulting paths can be described by any string of length 2n with equal number of (r)'s and (u)'s. This will give you "2n choose n" number of possible paths (ignoring other moves and cycles)

Stop path processing

I am new in neo4j and trying to understand how can I optimize routing queries.
I am working with OSM db.
and I am trying to calclulate the distance from one point to another.
START a=node(760119)
MATCH path=(a)-[:NEXT|NODE*1..30]-(c)
WHERE HAS(c.node_osm_id) AND c.node_osm_id=283103898
RETURN DISTINCT reduce(
distance = 0, n in filter(
x in path where has(x.length)
) | distance + n.length
) AS distance order by distance
My query returns a set of distances.
319.5609607071325
320.0901127819706
321.64043860878735
332.13372820085
334.21320610250484
How can i rewrite the query, to stop looking for new paths if the distance is longer than the shortest.
Thanks in advance.
Cypher doesn't have support for shortest path with cost evaluation yet (as of 2.0-RC1). If you need to use a more efficient shortest path algorithm, you'll need to implement an unmanaged extension.
However, I do see where you might be able to improve your performance... have you tried adding C as a start point query?

Resources