gpt4 book ai didi

java - Neo4j 3.1遍历API,如何找到两个节点之间的最短路径?

转载 作者:行者123 更新时间:2023-12-01 09:30:53 25 4
gpt4 key购买 nike

我正在使用 Traversal API,但似乎 Traversal.expanderForTypes() 已弃用,我不知道如何找到两个节点之间的最短路径。

我的方法

 public Iterable<Path> shortestPath(long firstNodeId, long secondNodeId) {
Node firstNode = null;
Node secondNode = null;
try (Transaction tx = mainServiceBean.getDatabaseService().beginTx()) {
firstNode = mainServiceBean.getDatabaseService().getNodeById(firstNodeId);
secondNode = mainServiceBean.getDatabaseService().getNodeById(secondNodeId);
PathExpander expander = Traversal.expanderForTypes();//?
PathFinder<Path> shortestPath = GraphAlgoFactory.shortestPath(expander, 4, 4);
tx.success();
return shortestPath.findAllPaths(firstNode, secondNode);
}
}

我的节点是城市以及之间的关系是这样的

 Node nodeACity = mainServiceBean.getDatabaseService().createNode(ProjectLabels.City);

nodeACity .setProperty(City.NAME, CityNames.ACiTY.name());

Node nodeBCity = mainServiceBean.getDatabaseService().createNode(ProjectLabels.City);
nodeBCity.setProperty(City.NAME, CityNames.BCity.name());


Relationship ab= nodeACity .createRelationshipTo(nodeBCity , NodesRelationship.DISTANCE_TO);
ab.setProperty("distance", 124.31);

Relationship ba= nodeBCity .createRelationshipTo(nodeACity , NodesRelationship.DISTANCE_TO);
ba.setProperty("distance", 124.31);

因此关系具有属性距离和值。

如何使用 neo4j 3 的遍历 API?看起来变化很大。

最佳答案

您将在 PathExpanders 中找到几个预定义的 PathExpander 实现。 .

对于给定节点 a 和 b,您对 DISTANCE_TO 建模两次是否有特定原因?在大多数情况下,最好只具有一种关系并在遍历过程中忽略方向。在这种情况下,您可以使用

PathExpander expander = PathExpanders.forType(NodesRelationship.DISTANCE_TO);

关于java - Neo4j 3.1遍历API,如何找到两个节点之间的最短路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39413132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com