'How to find shortest paths to group of nodes

So let's say that I have a graph with two node types: Source and Destination and relation FEEDS_INTO. Where Source nodes can have realtion between each other and to Destination nodes, but Destination nodes can only have relations from Source nodes and other Destination nodes are not connected. For Example: graph

My question is, how can I find all paths to Destination nodes that are shorter then the longest path to given Destination node ? In this example I want to return paths E->F->G->J and H->I->J and do this for every Destination node in graph. How can I do it ? Any help would be greatly appreciated.



Solution 1:[1]

You could find the longest path in a subquery first and then find the paths that are shorter than the longest path.

CALL
    {MATCH p=(startNode)-[:FEEDS_INTO*]->(J:Destination)
    WHERE J.name='some filter' //Optional
    RETURN max(length(p)) as `longestPath`,J}
MATCH p=(startNode)-[:FEEDS_INTO*]->(J)
    WHERE length(p) < `longestPath`
RETURN p

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thennan