Neo4j最短路径问题

1.指定某一结点

  • 无向边:
MATCH (p1:Person {name:"aaaaaaa"}),(p2:Person{name:"bbbbbb"}),
p=shortestpath((p1)-[*..10]-(p2))
RETURN p
  • 有向边:
MATCH (p1:Person {name:"aaaaaaa"}),(p2:Person{name:"bbbbbb"}),
p=shortestpath((p1)-[*..10]->(p2))
RETURN p

注:[*…10]表示查询路径长度10以内的关系

  • 同时返回最短路径长度:
MATCH (p1:Person {name:"aaaaaaa"}),(p2:Person{name:"bbbbbb"}),
p=shortestpath((p1)-[*..10]->(p2))
RETURN p,length(p)
  • 添加限制条件
  • 1)只经过标签为“rrrr”的边:
MATCH (p1:Person {name:"aaaaaaa"}),(p2:Person{name:"bbbbbb"}),
p=shortestpath((p1)-[r:rrrr*..10]->(p2))
RETURN p
  • 2)不经过属性值idp为"xxxx"的结点:
MATCH (p1:Person {name:"aaaaaaa"}),(p2:Person{name:"bbbbbb"}),
p=shortestpath((p1)-[*..10]->(p2))
where all(x in nodes(p) where x.idp<>"xxxx")
RETURN p
  • 2)不经过属性值idr为"yyyy"的边:
MATCH (p1:Person {name:"aaaaaaa"}),(p2:Person{name:"bbbbbb"}),
p=shortestpath((p1)-[r*..10]->(p2))
where all(x in r where x.idr<>"yyyy")
RETURN p

2.指定某一类结点

(边的有向无向、限制条件同上,此处不再分别叙述)

match (p:Person) with collect(p) as nodes
unwind nodes as source
unwind nodes as target
with source,target where id(source)<>id(target)
match paths = shortestPath((source)-[*..10]->(target))
with paths limit 25
return path
  • 返回所有最短路径
match (p:Person) with collect(p) as nodes
unwind nodes as source
unwind nodes as target
with source,target where id(source)<>id(target)
match paths = allShortestPaths((source)-[*..10]->(target))
with paths limit 25
return path

with source,target where id(source)<>id(target)

此处是为了保证起始和最终结点不相同。

注:两个unwind把结点集合打散,并以笛卡尔积的形式组成结点对。

参考:https://blog.csdn.net/wry2008wry/article/details/80762811

原文地址:https://blog.csdn.net/qq_34233510/article/details/83110854                                </div>

最新文章

  1. PHP+MYSQL网站SQL Injection攻防
  2. pc/app 项目/功能设计
  3. DICOM:Ubuntu14环境下安装dcm4chee+oviyam2.1
  4. Quartz2D简介及基本线条绘制
  5. I.MX6 U-boot Kernel backlight setting
  6. css09浮动属性
  7. Validate Binary Search Tree 解答
  8. android 常用调用系统功能
  9. 手动搭建apache james邮件服务器,实现邮件功能
  10. ubuntu14.04下chrome浏览器的安装
  11. jQuery图片上传前先在本地预览(不经过后端处理)
  12. POJ3621 Sightseeing Cows(最优比率环)
  13. Day20 Ajax
  14. [转] spring framework体系结构及内部各模块jar之间的maven依赖关系
  15. ArcGIS自定义脚本-通过txt/excel/dbf/table生成多边形要素类
  16. 【转】LoadRunner--Analysis各项指标详解
  17. 【使用JSOUP实现网络爬虫】修改数据-设置属性的值
  18. hdu 5943(素数间隔+二分图匹配)
  19. easyui input设置为disabled提交后获取不到属性值
  20. 1711 Number Sequence(kmp)

热门文章

  1. XSLT学习(九)通过JavaScript转化xml
  2. Linux学习笔记1-在CentOS 7中安装配置JDK8
  3. protocols - 协议定义文件
  4. Thymeleaf入门——入门与基本概述
  5. 事件日期转BCD码
  6. Zen Coding – 超快地写网页代码(注:已更名为Emmet)
  7. Vue:对象更改检测注意事项
  8. 在项目中使用 Maven 私服
  9. Python的pip源切换为国内阿里云镜像
  10. Git 最全命令使用