John Smith
John Smith

Reputation: 1

Neo4j apoc dijkstra procedure

To find the shortest path in neo4j I am using Dijkstra's algorithm from APOC library. The issue is that request returns just 1 result. Is it possible to get 5 or 10 shortest paths? Or can I set conditions by weight of edges? For instance, total length more than 500.

MATCH (start:Point {title: 'Some Point 1'}), (end:Point {title: 'Some Point 5'}) CALL apoc.algo.dijkstra(start, end, 'distance', 'value') YIELD path, weight RETURN path, weight

Upvotes: 0

Views: 588

Answers (1)

szenyo
szenyo

Reputation: 522

If you want to have more control, I would go for a pure cypher solution instead of the apoc procedure.

Top 10:

MATCH p=(start:Point {title: 'Some Point 1'})-[rels:distance*]->(end:Point {title: 'Some Point 5'})
WITH p, REDUCE(weight=0, rel in rels | weight + rel.value) as length
RETURN p, length 
ORDER BY length ASC
LIMIT 10

Path where length is more than 500:

MATCH p=(start:Point {title: 'Some Point 1'})-[rels:distance*]->(end:Point {title: 'Some Point 5'})
WITH p, REDUCE(weight=0, rel in rels | weight + rel.value) as length
with p, length where length > 500
return p, length 
LIMIT 10

Upvotes: 1

Related Questions