简体   繁体   English

neo4j节点之间的最短路径

[英]neo4j shortest path in between node

I have to find the shortest path between two nodes that include a specific type of node in the path. 我必须在两个节点之间找到包含路径中特定类型节点的最短路径。

have the following cypher: 有以下密码:

Match p = shortestpath((E1:Entity{seq:"123"}) –[*]-(E2:Entity{seq:"456"})))
Where any(x in nodes(path) where x:T)
Return path

T: label, can be millions of nodes dbgraph size: 4gb T:标签,dbgraph大小可以是数百万个节点:4gb

The problem is that it only works when the hops are limited up to 5, and that is not enough. 问题是它只在跳数限制为5时才有效,这还不够。

Any idea on how to rewrite this for optimization? 有关如何重写此优化的任何想法? When 6 or more hops it crashes. 当6次或更多次跳跃时它会崩溃。

The main problem with this kind of query is that there's no way to truncate paths during traversal...you'll only know that the path isn't valid when it finally ends at E2 , since that's the only point at which you can determine that none of the nodes in an path is a :T node. 这种查询的主要问题是在遍历期间没有办法截断路径...你只会知道路径最终在E2结束时无效,因为这是你可以确定的唯一点路径中没有节点是:T节点。

The only way I know that might optimize a bit is to ensure that all expansions stop when E2 is reached, since the query as it is now will find all paths, even those that expand past E2 , as there could be a path that goes through E2 , hits a :T node, then eventually returns. 我知道可能优化一点的唯一方法是确保所有扩展在到达E2时停止,因为现在的查询将找到所有路径,甚至是那些扩展到E2的路径,因为可能存在通过的路径E2 ,命中一个:T节点,然后最终返回。

Unfortunately that optimization can't be done with Cypher. 不幸的是,Cypher无法完成优化。 The latest versions of APOC Procedures (APOC 3.3.0.2 for Neo4j 3.3.x or APOC 3.2.3.6 for Neo4j 3.2.x) have enhanced the path expander procedures to work with end nodes, and we can configure the expansion to terminate when an end node is reached, so we can stop needless expansion (this assumes that paths that pass through E2 and then return are not valid). APOC程序的最新版本(Neo4j 3.3.x的APOC 3.3.0.2或Neo4j 3.2.x的APOC 3.2.3.6)增强了与终端节点一起使用的路径扩展程序,我们可以将扩展配置为在结束时终止到达节点,所以我们可以停止不必要的扩展(这假设通过E2然后返回的路径无效)。

MATCH (E1:Entity{seq:"123"}), (E2:Entity{seq:"456"})
CALL apoc.path.expandConfig(E1, {terminatorNodes:[E2]}) YIELD path
WITH path
WHERE any(x in nodes(path) where x:T)
RETURN path
ORDER BY length(path) ASC
LIMIT 1    

While this may help somewhat (at least where the E2 node blocks off expansion to a larger portion of the graph), an unbounded variable-length traversal without any restrictions on the type and with the any() predicate is not going to perform well in most cases for the reasons cited above, especially in the cases where no such path exists. 虽然这可能有所帮助(至少在E2节点阻止扩展到图形的较大部分的情况下),无限制的可变长度遍历而没有对类型和any()谓词的任何限制,但在大多数情况出于上述原因,特别是在没有这种路径的情况下。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM