作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我是一名优秀的程序员,十分优秀!