简体   繁体   English

如何找到给定两个节点之间的路径?

[英]How can I find a path between given two nodes?

How can I find a path between given two nodes in neo4j Java API, the product of all weights of the path is maximum in all paths between two nodes, what can I do? 如何在neo4j Java API中找到给定两个节点之间的路径,该路径的所有权重的乘积在两个节点之间的所有路径中最大,该怎么办? In my graph database, there are two elements: one is node, another is relationship, and all have a name property, but the relationship have an extra property: weight (double type, and values in (0,1]). My code as follows: How do I modify? 在我的图形数据库中,有两个元素:一个是节点,另一个是关系,所有元素都具有name属性,但是该关系具有一个额外的属性:weight(双精度类型,并且值在(0,1]中)。如下:如何修改?

public static ArrayList<Path> getAllOptPaths(Long startNodeId, Long endNodeId, GraphDatabaseService db){
    ArrayList<Path> optPathsBetweenTwoNodes = new ArrayList<Path>();
    try (Transaction tx = db.beginTx()){
        Node node1 = db.getNodeById(startNodeId);
        Node node2 = db.getNodeById(endNodeId);

        PathExpander<Object> pathExpander = PathExpanders.allTypesAndDirections();
        CostEvaluator<Double> costEvaluator = CommonEvaluators.doubleCostEvaluator("Cost");

        // find all paths between given two nodes
        PathFinder<WeightedPath> dijkstraPathsFinder = GraphAlgoFactory.dijkstra(pathExpander, costEvaluator);
        WeightedPath path = dijkstraPathsFinder.findSinglePath(node1, node2);

        optPathsBetweenTwoNodes.add(path);

        tx.success();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return optPathsBetweenTwoNodes;       
}

Exploring paths is also possible in a cypher query. 在密码查询中也可以探索路径。 This can deliver you the result of a weighted path calculation (and thus the path with the minimum weight on it) as a result. 因此,可以为您提供加权路径计算的结果(以及加权最小的路径)。

This webpage contains an example of a weighted path query. 该网页包含加权路径查询的示例。 Also, the Neo4j reduce function will help you to specify how to calculate (and weigh) your paths. 另外, Neo4j reduce功能将帮助您指定如何计算(和称重)路径。

This problem has been resolved by myself, and the method is modify the source of neo4j core JAVA API.In the process of modifying source involved some specific documents as follows:Dijkstra.java, BestFirstSelectoryFactory.java, GraphAlgoFactory.java, Path.java, WeightedPathImpl.java, WeightedPathIterator.java. 这个问题已由我自己解决,方法是修改neo4j核心JAVA API的源代码。在修改源代码的过程中,涉及一些特定的文档,如下所示:Dijkstra.java,BestFirstSelectoryFactory.java,GraphAlgoFactory.java,Path.java WeightedPathImpl.java,WeightedPathIterator.java。 If anyone is interested in the detail of modify, you can send mail to me. 如果有人对修改的细节感兴趣,可以发送邮件给我。

Changing the codebase is not a good idea as I think. 我认为更改代码库不是一个好主意。 You can set the weight of the edges with key "Cost" when you set the property of the relationship (weight of the edge for example) using function setProperty(). 使用功能setProperty()设置关系的属性(例如,边缘的权重)时,可以使用键“成本”来设置边缘的权重。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将巨大的图加载到内存中以反复查找两个给定节点之间的最短路径? - How to load a huge graph into memory to repeatedly find shortest path between two given nodes? 找到任意两个节点之间的最长路径 - Find the longest path between any two nodes 如何查找两个给定顶点之间是否存在路径JAVA - How to find if a path exists between two given vertex JAVA 如何找到树中两个节点之间的路径? - How to find the path between 2 nodes in a tree? 给定一个有向图,找出两个节点之间是否存在路由 - Given a directed graph, find out whether there is a route between two nodes Dijkstra算法在大图中找到两个节点之间的最短路径? - Dijkstra algorithm to find shortest path between two nodes in big graph? Neo4j 3.1遍历API,如何找到两个节点之间的最短路径? - Neo4j 3.1 traversal API, how to find shortest Path between two nodes? 如何计算沿路径法线的两个点,仅给出该路径的两个点? - How can I calculate two points along the normal of a path given only two points from that path? 如何在处理中计算两个给定点之间的点? - How can I calculate a point between two given points in processing? 如何使用广度优先搜索获得两个节点之间的最短路径? - How to get shortest path between two nodes with Breadth First Search?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM