Paths and shortest-path queries
On this page
Graph algorithms
Two flavours: procedure-based (weighted, on the live graph) and
Cypher path search (unweighted, in a MATCH).
Procedures (weighted)
gdb.dijkstra, gdb.astar, gdb.pageRank, gdb.degree — full signatures in
Live graph algorithms.
CALL gdb.dijkstra(0, 42, 'ROAD', 'km', 'OUT') YIELD nodeIds, totalCost
RETURN nodeIds, totalCost;
CALL gdb.pageRank('LINKS', 30, 0.85) YIELD nodeId, score
RETURN nodeId, score ORDER BY score DESC LIMIT 10;
shortestPath / allShortestPaths (unweighted, in MATCH)
MATCH (a:Person {name: 'Ada'}), (b:Person {name: 'Zoe'})
MATCH p = shortestPath((a)-[:KNOWS*]-(b))
RETURN length(p), [n IN nodes(p) | n.name];
MATCH (a:Person {name: 'Ada'}), (b:Person {name: 'Zoe'})
MATCH p = allShortestPaths((a)-[:KNOWS*]-(b))
RETURN p;
These do a breadth-first search and return the shortest (or all equally-shortest) paths by hop count.
Data-science similarity functions
Pairwise vector similarity functions (gds.similarity.*, vector.similarity.*)
are documented in Pairwise similarity functions.
MATCH (a:Doc {id: 1}), (b:Doc)
WHERE b.id <> 1
RETURN b.id, gds.similarity.cosine(a.embedding, b.embedding) AS sim
ORDER BY sim DESC LIMIT 5;
Related articles
Indexes and constraints · Procedure directory · Transactions and concurrency