Reputation: 1411
How do I make a query that returns all nodes reachable from a given node along with their distances from there? I'm using Memgraph graph database.
I've tried using
MATCH (n) RETURN n
But it is not what I want.
Upvotes: 1
Views: 366
Reputation: 26
In Memgraph for an unweighted graph, where the distance is counting hops between nodes, you can use the BFS algorithm:
MATCH path=(n:City {name: 'Berlin'})-[:Road *bfs]->(m)
RETURN m, size(relationships(path));
If you have a weighted graph, use weighted shortest algorithm to calculate the total weight to another node:
MATCH (n:City {name: 'Oulu'})-[:Road *WSHORTEST(r, n | r.length) total_weight]->(m)
RETURN m, total_weight;
To learn more about the syntax and use of these algorithms, head to: https://memgraph.com/docs/memgraph/reference-guide/built-in-graph-algorithms
Upvotes: 1