I'm trying to find the lightest path from node a
to e
in the following graph:
The result should be 13:
a -> b: 1
b -> c: 3
c -> d: 4
d -> e: 5 (take lighter edge)
----------
13
I tried several examples, (e.g. https://neo4j.com/docs/graph-algorithms/current/algorithms/shortest-path/) but can't find the right query.
MATCH (start:LocationNode{name:'a'}), (end:LocationNode{name:'e'})
CALL algo.shortestPath(start, end, 'weight',{write:true,writeProperty:'sssp'})
YIELD writeMillis,loadMillis,nodeCount, totalCost
RETURN writeMillis,loadMillis,nodeCount,totalCost
results in
╒═════════════╤════════════╤═══════════╤═══════════╕
│"writeMillis"│"loadMillis"│"nodeCount"│"totalCost"│
╞═════════════╪════════════╪═══════════╪═══════════╡
│3 │3 │5 │12.0 │
└─────────────┴────────────┴───────────┴───────────┘
and
MATCH (start:LocationNode{name:'a'}), (end:LocationNode{name:'e'})
CALL algo.shortestPath(start, end, 'weight',{
nodeQuery:'MATCH(n:LocationNode) RETURN id(n) as id',
relationshipQuery:'MATCH(n:LocationNode)-[r:CONNECTED_TO]->(m:LocationNode) RETURN id(n) as source, id(m) as target, r.weight as weight',
graph:'cypher'})
YIELD writeMillis,loadMillis,nodeCount, totalCost
RETURN writeMillis,loadMillis,nodeCount,totalCost
results in
╒═════════════╤════════════╤═══════════╤═══════════╕
│"writeMillis"│"loadMillis"│"nodeCount"│"totalCost"│
╞═════════════╪════════════╪═══════════╪═══════════╡
│3 │19 │4 │14.0 │
└─────────────┴────────────┴───────────┴───────────┘
Other queries like the following don't even return anything:
MATCH p=(LocationNode{name:'a'})-[:CONNECTED_TO*]->(LocationNode{name:'e'})
RETURN p as shortestPath,
REDUCE(weight=0, r in relationships(p) | weight+r.weight) AS totalDistance
I would like to see a query that returns '13' as solution and ideally displays the chosen path like this:
How can I achieve this?
Thank you very much.