Search code examples
memgraphdbopencypher

Memgraph shortest path between two nodes without weights or using *WSHORTEST


How do you return the shortest path between two nodes in Memgraph. I found the algorithm for the weighted shortest path in their documentation [*WSHORTEST (r, n | r.weight)], but this is not what I am looking for. I need to find the shortest path between two specific nodes without using weights.

Neo4j has a way of doing this with Cypher's shortestPath((startNode) - [*] - (endNode) function, but as far as I know this is not supported in Memgraph which uses openCypher.

So the question is how to compute the shortest path in Memgraph; is there an algorithm for it or can I somehow use WSHORTEST without weights (I tried but could not made it work).


Solution

  • @cybersam is right! The variable length bfs is most likely what you want.

    wshortest can also be used by specifying 1 in the expression part, so:

    MATCH path=(n {id: 0})-[R:Edge *WSHORTEST (r, n | 1) total_weight]-(m {id: 9})
    RETURN nodes(path), total_weight;
    

    but it's too heavy for the simplest shortest path because it runs the full Dijkstra algorithm, which is not required if the weights are constants.