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