求指教,shortestpath问题
发布于 5 年前 作者 prostect1996 1829 次浏览 来自 问答

在找两个结点间的最短路径的时候,如何在确保是快速算法的前提下,给路径中结点加约束条件。 举个例子:找两个有关系的人的最短路径,路径中的所有人的"国籍"属性一致。(此例不考虑权重)

match p = shortestpath((a:Person{Name:""})-[*]-(b:Person{Name:"")) where all(x in nodes( p ) where x.nationality=" china ") return p

这种情况,neo4j就提示会执行 exhaustive search。 参照官方的这句话 Neo4j will use a fast bidirectional breadth-first search algorithm if the predicates can be evaluated whilst searching for the path。对结点属性判断不需要先获取整个结果集,应该还是快速算法才对,但就是不行。 尝试后发现,where约束中使用nodes( p )对路径上结点做判断就会执行exhaustive search,使用relationships( p )对路径上关系做判断就不会执行exhaustive search,但是我需要对节点属性过滤而不是关系属性。

with 没法起作用。因为用with的话是先找到最短路径再做判断,判断不满足就返回空值了。 algo.shortestpath()及apoc path finding中的apoc.algo.dijkstra()也只有标签和关系过滤。

求指导,万分感谢!

1 回复

同求高效搜索算法

回到顶部