cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Pathfinding query yields two correct paths, return the one with lowest weight

Hello!
I have a quite complex intertwined and directed graph network that represents a railroad. By matching a node with an ID:

MATCH (p1:Node { UniversalId: '00000000-0c00-090e-0001-060f0e030f0b'})
RETURN p1

I get two nodes back (by design). These represent a "right"-direction node and a "left"-direction node.
All the nodes in the graph are connected to another node with the same direction. (To represent "normal running direction" for the train). It could be looked as two different graphs.

However, these two graphs are in some special cases connected to each other. ( Shunting tracks) Resulting in that a "left-node" could be connected to a "right-node".

When doing a shortest path search: (Remember, Every match yields two nodes in return)
I sometimes get multiple results back, one with a lower "total weight" and ones with a much higher.

MATCH (p1:Node { UniversalId: '00000000-0c00-090e-0001-060f0e030f0b'}) 
MATCH (p2:Node { UniversalId: '00000000-0c00-090e-0001-060f0e040205'}) 
CALL apoc.algo.dijkstra(p1, p2, 'AVSTAND>', 'cost_algo') YIELD path, weight
return weight, path

Result:

...........weight.............................path...........................
1........30000.............................{"start": {"identity": 120,"labels"....
2........4000................................{"start": {"identity": 150,"labels"....
3........1235................................{"start": {"identity": 110,"labels"....

I want to return the path with the corresponding lowest weight. (Number of nodes in path doesn't matter, only the weight)

I know that I could do something like this to get the lowest weight back:

MATCH (p1:Node { UniversalId: '00000000-0c00-090e-0001-060f0e030f0b'}) 
MATCH (p2:Node { UniversalId: '00000000-0c00-090e-0001-060f0e040205'}) 
CALL apoc.algo.dijkstra(p1, p2, 'AVSTAND>', 'cost_algo') YIELD path, weight
return min(weight)

Result:

..............weight...................................
1............1235......................................

But I do also need the corresponding path of that row.
Please help 😄

Expected result:

...........weight.............................path...........................
1........1235................................{"start": {"identity": 110,"labels"....

1 ACCEPTED SOLUTION

Try this. It will return the path and weight of the path with the lowest weight.

MATCH (p1:Node { UniversalId: '00000000-0c00-090e-0001-060f0e030f0b'}) 
MATCH (p2:Node { UniversalId: '00000000-0c00-090e-0001-060f0e040205'}) 
CALL apoc.algo.dijkstra(p1, p2, 'AVSTAND>', 'cost_algo') YIELD path, weight
return path, weight
Order by weight asc
Limit 1

[/quote]

View solution in original post

2 REPLIES 2

Try this. It will return the path and weight of the path with the lowest weight.

MATCH (p1:Node { UniversalId: '00000000-0c00-090e-0001-060f0e030f0b'}) 
MATCH (p2:Node { UniversalId: '00000000-0c00-090e-0001-060f0e040205'}) 
CALL apoc.algo.dijkstra(p1, p2, 'AVSTAND>', 'cost_algo') YIELD path, weight
return path, weight
Order by weight asc
Limit 1

[/quote]

That did the job! Thank you