3
votes

Suppose I want to calculate the shortest path of a graph with Neo4j's built-in shortestPath algorithm. Is there a way to use the algorithm where the distance between nodes is encoded in a property of the relationship?

What I mean, in case it is not clear is the following: my graph G consists of 3 vertices A, B, C and 3 edges:

  • E1: (A)-->(B)
  • E2: (A)-->(C)
  • E3: (C)-->(B)

If I ask for the shortest path between A and B in this graph I get E1. However, if the "distance" properties of the edges are as follows, then I would want to get E2-E3 as the shortest path:

  • E1: distance = 10
  • E2: distance = 4
  • E3: distance = 3

Is this possible and if so how?

1

1 Answers

2
votes

Use dijkstra instead of shortestPath, it allows you to specify a cost_property. More info can be found here