我有一些节点和节点之间的链接(关系)。
默认情况下,dijkstra(在 neo4j 中)考虑了关系的成本属性,以找到节点之间的最短路径。
我还想考虑节点的成本属性,所以我定义了以下内容costEvaluator
:
CostEvaluator<Double> costEvaluator = new CostEvaluator<Double>() {
@Override
public Double getCost(Relationship relationship, Direction direction) {
Long costR = (Long) relationship.getProperty("cost");
Long costE = (Long) relationship.getEndNode().getProperty("cost");
Long cost = costR + costE;
return cost.doubleValue();
}
};
我在以下代码中使用了它:
PathFinder<WeightedPath> finder = GraphAlgoFactory.dijkstra(
Traversal.expanderForTypes(ROAD, Direction.BOTH),
costEvaluator);
WeightedPath path = finder.findSinglePath(node1, node2);
System.out.println("Path weight: " + path.weight());
for (Node n: path.nodes()) {
System.out.println("Node: " + n.getId() + "-" + n.getProperty("cost"));
}
for (Relationship r: path.relationships()) {
System.out.println("Relationship: " + r.getId() + "-" + r.getProperty("cost") );
}
当我执行此代码时,我有以下结果:
Path weight: 1395.0
Node: 106-100
Node: 29-30
Node: 85-30
Node: 16-40
Node: 10-100
Node: 152-60
Relationship: 344-334
Relationship: 13-398
Relationship: 406-38
Relationship: 326-146
Relationship: 500-289
为什么路径权重是 1395?它应该是 1465(在我看来)。
关系成本:334 + 398 + 38 + 146 + 289 = 1205
这些关系的结束节点的成本:30 + 30 + 40 + 100 + 60 = 260
1205+260=1465
为什么路径权重是 1395?
有人可以解释一下吗?