簡體   English   中英

Neo4j 3.1遍歷API,如何找到兩個節點之間的最短路徑?

[英]Neo4j 3.1 traversal API, how to find shortest Path between two nodes?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM