Search code examples
graphneo4jcypherpy2neo

Cypher allShortestPaths just return one path?


Background statement:

  • I have a graph like bellow: enter image description here

  • I want to find all the path between Node A and Node F (something like how many ways I can reach F from A), then my Cypher like this bellow:

MATCH (start:kg:test), (end:kg:test),  p = allShortestPaths((start)-[*..8]-(end))
    where start.value = 'A' and end.value = 'F'
    RETURN start, end, p
  • As I expected, this query will return the whole graph, but it just returns A->F (return the same thing with using the shortestPath function), like bellow: enter image description here

Problems

  • Why that query won't return all the different paths in the graph?
  • Do I misuse the allShortestPaths function?
  • How can I get all the path from Node A to Node F?

thanks


Solution

  • shortestPath() returns the single shortest path between the nodes (and if there are multiple of the same size it just returns the first that it finds).

    If there are multiple paths that could have been returned by shortestPath() (they will all have the same size), then allShortesPaths() will return them.

    If you just want to find all possible paths between two nodes (the length of the path doesn't matter, and you don't care about shortest paths at all), then you don't need to use either of these functions.

    MATCH p=(start:kg:test)-[*..8]-(end:kg:test)
        where start.value = 'A' and end.value = 'F'
        RETURN start, end, p