首页 > 其他 > 详细

All shortest paths between a set of nodes

时间:2019-10-11 10:51:36      阅读:64      评论:0      收藏:0      [点我收藏+]

Consider a number of arbitrary nodes, A,B,C,D,E,F,…?..

I wish to return all of the shortest paths between these nodes. The nodes may have many edges between them, but anticipate a maximum of 4. The graph is complex and non hierarchical (if this makes sense – any node may point to any other node). A typical node has the form: match (n:Entity { name: ‘xyz‘ })

How would I write the match expression to return the shortest paths between the above nodes, in no specific order?

Solution

  1. Find the set of nodes using an indexed lookup operation
  2. Collect them into a list
  3. Unwind the list twice, once for every side of the path
  4. Remove inverse pairs by id comparison
  5. match and return the paths
MATCH (n:Entity) where n.name IN [names]
WITH collect(n) as nodes
UNWIND nodes as n
UNWIND nodes as m
WITH * WHERE id(n) < id(m)
MATCH path = allShortestPaths( (n)-[*..4]-(m) )
RETURN path

原文地址:https://neo4j.com/developer/kb/all-shortest-paths-between-set-of-nodes/

All shortest paths between a set of nodes

原文:https://www.cnblogs.com/jpfss/p/11652218.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!