gpt4 book ai didi

java - Neo4j双向遍历api

转载 作者:太空宇宙 更新时间:2023-11-04 14:39:21 24 4
gpt4 key购买 nike

我正在使用 Neo4j,到目前为止我有一个地理图,其中 AIRPORT连接到 CITYCITYCOUNTRYCOUNTRYCONTINENT ,如图enter image description here

箭头上的标签翻译为 org.neo4j.graphdb.RelationshipType进入我的代码。到目前为止,我可以构建起始节点 MXP 之间的路径。到结束节点LTN使用以下单向遍历。

Traverser traverse = database.traversalDescription().depthFirst()
.relationships(CITY, BOTH)
.relationships(CONTINENT, BOTH)
.relationships(COUNTRY, BOTH)
.relationships(REGION, BOTH)
.evaluator(Evaluators.includeWhereEndNodeIs(endNode)).traverse(startNode);

这样,我得到了一条路径 MXP -> Milan -> Italy -> Europe <- England <- London <- LTN ,考虑到图形描述、遍历描述以及我对此类描述的理解,这是正确的。

我正在尝试更改此代码以执行双向遍历,这意味着我想从 MXP 开始和LTN并停在碰撞点。我尝试使用以下代码片段,其中评论表示我的理解,因此可能更容易指出问题。

TraversalDescription startSide = database.traversalDescription().depthFirst() //Depth first algorithm
.relationships(CITY, OUTGOING) //consider CITY relationship, only outgoing
.relationships(REGION, OUTGOING) //consider REGION relationship, only outgoing
.relationships(COUNTRY, OUTGOING) //consider COUNTRY relationship, only outgoing
.relationships(CONTINENT, OUTGOING) //consider CONTINENT relationship, only outgoing
.evaluator(Evaluators.excludeStartPosition()); //do not consider the starting point.
//Here I tried also with all, with the same result
//with includeWhereEndNodeIs(endNode), again with same result
//and combining includeWhereEndNodeIs and excludeStartPosition, once more with same result.
//All tries I mirrored for the endSide description, changing endNode to startNode where I feel it was needed

TraversalDescription endSide = database.traversalDescription().depthFirst()
.relationships(CITY, OUTGOING)
.relationships(REGION, OUTGOING)
.relationships(COUNTRY, OUTGOING)
.relationships(CONTINENT, OUTGOING)
.evaluator(Evaluators.excludeStartPosition());

List<Node> asList = Arrays.asList(startNode, endNode);
Traverser traverse = database.bidirectionalTraversalDescription().endSide(endSide).startSide(startSide).traverse(asList, asList);

在这里,我得到的不是单向遍历尝试得到的路径,而是两条路径,其中一条只有 MXP和一个只有 LTN .

在这一点上,我真的相信我完全误解了双向遍历,甚至可能误解了它的目的。我的错误在哪里?为什么我没有得到相同的输出?

最佳答案

我终于找到了一个可行的解决方案。我的代码中的问题与 uniqueness 的概念有关。 。我的问题的有趣之处是

Sets the rules for how positions can be revisited during a traversal as stated in Uniqueness. Default if not set is NODE_GLOBAL.

NODE_GLOBAL uniqueness: No node in the entire graph may be visited more than once. This could potentially consume a lot of memory since it requires keeping an in-memory data structure remembering all the visited nodes.

NODE_PATH uniqueness: A node may not occur previously in the path reaching up to it.

这些描述与official API有些不同。所以我尝试了不同的组合,最终得到了以下代码:

TraversalDescription bothSide = database.traversalDescription().depthFirst()
.relationships(CITY, OUTGOING)
.relationships(REGION, OUTGOING)
.relationships(COUNTRY, OUTGOING)
.relationships(CONTINENT, OUTGOING)
.uniqueness(NODE_PATH);

Traverser traverser = database
.bidirectionalTraversalDescription()
.startSide(bothSide)
.endSide(bothSide)
.traverse(node, endNode);

基本上,我定义了一个常见的 TraversalDescription对于结束侧和开始侧,我只想关注 OUTGOING关系,我只想考虑节点在路径本身内唯一的路径。

然后,我定义了一个bidirectional traverser它简单地设置结束和开始边,并从起始节点 node 遍历图形。到结束节点endNode (好吧,实际上它同时从开始到结束和从结束到开始遍历,并在两次遍历碰撞时停止,将生成的路径合并为从 startend 的单个路径)。

注意:我不完全确定 NODE_GLOBAL 的含义,因为在我的数据库中每个节点代表一个地理实体,所以路径 MXP -> Milan -> Italy -> Europe <- England <- London <- LTN 中的每个节点应该只被访问一次,因此 NODE_GLOBAL 之间应该没有区别和NODE_PATH在此背景下。

关于java - Neo4j双向遍历api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25137068/

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