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

99 Views Asked by At

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).

2

There are 2 best solutions below

2
buda On BEST ANSWER

@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.

0
cybersam On

Memgraph's Breadth-first search is probably the most similar to shortestpath.

Here is an example from their documentation:

The following query will show the shortest path between nodes n and m as a graph result.

MATCH path=(n {id: 0})-[*BFS]->(m {id: 8})
RETURN path;