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

标签 java neo4j traversal spring-data-neo4j

我正在使用 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/

相关文章:

java - 使用 SMSLib 运行 Java 应用程序时出现 NoSuchPortException

java - 如何在 Map 中控制 JPA 列名

json - 我想得到一个 json 格式的 Neo4j 结果集?

java - 关于遍历数组时for循环中的 "else"语句

javascript - 在 React 中使用 map() 遍历嵌套的 Prop

java - @PostConstruct 和 @PreDestroy 注解的实时应用程序使用

java - 单个模型元素在 UI 中多次表示...如何在 GEF 中实现?

neo4j - 按路径顺序返回节点的密码查询

neo4j - 在 cypher 语句中复制 Neo4j 浏览器自动完成功能

Javascript:使用遍历从 JSON 对象获取引用