cancel
Showing results forΒ 
Search instead forΒ 
Did you mean:Β 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

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

johan_nilsson12
Node Link

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

glilienfield
Ninja
Ninja

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

glilienfield
Ninja
Ninja

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