在图中查找最长路径 [英] Find Longest Path in Graph

查看:149
本文介绍了在图中查找最长路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力寻找复杂网络中的最长路径.我在StackOverflow和Internet中遇到了很多问题,但没有一个可以帮助我.我将CQL写为

I have been trying hard to find out longest path in a complex network. I have been through many questions in StackOverflow and Internet, but none could help me. I have written a CQL as

start n=node(*)
match p = (n)-[:LinkTo*1..]->(m)
with n,MAX(length(p)) as L
match p = (n)-[:LinkTo*1..]->(m)
where length(p) = L
return p,L

我没有任何解决办法. Neo4J将继续运行寻找答案,我也尝试在Neo4J Cloud Hosting中执行它.我什至没有任何解决方案,但是出现了一个错误 错误undefined-undefined" 我迫切需要一个解决方案.这个答案的结果将帮助我完成我的项目.因此,任何人都可以帮助我纠正查询.

I don't get any solution. Neo4J would keep running for the answer, and I also tried executing it in Neo4J Cloud Hosting. I didn't any solution even there, but got an error "Error undefined-undefined" I am in dire need of a solution. The result for this answer will help me complete my project. So, anyone please help me in correcting the query.

推荐答案

好吧,您只需执行一次就可以执行两次非常昂贵的操作.

Well for one you're doing a highly expensive operation twice when you only have to do it once.

此外,您至少要在数据库中的每个节点上返回一个路径(因为该节点可能有多个路径,而该路径是该节点可用的最长路径).但是从您的问题看来,您似乎想要图形中的最大路径,而不是每个节点都一个.

Additionally, you are returning one path per every single node in your database, at least (as there may be multiple paths for a node that are the longest paths available for that node). But from your question it sounds like you want the single largest path in the graph, not one each for every single node.

我们还可以通过仅在路径开头而不是中间位置的节点上执行最长路径匹配来改善您的匹配.

We can also improve your match by only performing the longest-path match on nodes that are at the head of the path, and not somewhere in the middle.

也许试试这个?

match (n)
where (n)-[:LinkTo]->() and not ()-[:LinkTo]->(n)
match p = (n)-[:LinkTo*1..]->(m)
return p, length(p) as L
order by L desc
limit 1

这篇关于在图中查找最长路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆