graph-shortest-paths
运算符查找一组源节点与图形中的一组目标节点之间的最短路径,并返回包含结果的表。
注意
此运算符与 生成图形运算符一起使用。
语法
G|
graph-shortest-paths
[output
=
OutputOption] Patternwhere
Predicateproject
[ColumnName=
] 表达式 [,
...]
参数
客户 | 类型 | 必需 | 说明 |
---|---|---|---|
G | 字符串 | ✔️ | 图形源,通常是 make-graph 操作的输出。 |
模式 | 字符串 | ✔️ | 描述要查找的路径的路径模式。 模式必须至少包含一个可变长度边缘,并且不能包含多个序列。 |
谓语 | 表达式 | 布尔表达式,由模式中的命名变量和常量的属性组成。 | |
表达式 | 表达式 | ✔️ | 标量表达式,使用常量和对模式中命名变量的属性的引用来定义每个找到的路径的输出行。 |
OutputOption | 字符串 | 将搜索输出指定为 any (默认值)或 all 。 对于每个源/目标对的单个最短路径,输出指定为 any ,对于长度相等的所有最短路径,则指定为 all 。 |
路径模式表示法
下表显示了支持的路径模式表示法。
元素 | 命名变量 | 匿名元素 |
---|---|---|
节点 |
(
n) |
() |
定向边缘:从左到右 |
-[
e]-> |
--> |
定向边缘:从右到左 |
<-[
e]- |
<-- |
任何方向边缘 |
-[
e]- |
-- |
可变长度边缘 |
-[
e*3..5]- |
-[*3..5]- |
可变长度边缘
可变长度边缘允许在定义的限制内多次重复的特定模式。 这种类型的边缘由星号(*
)表示,后跟最小值..
最大值格式的最小和最大出现值。 这些值必须是整数标量。 如果序列中的所有边缘满足 where
子句约束,则此范围中的任何边缘序列都可以匹配模式的可变边缘。
返回
graph-shortest-paths
运算符会返回表格结果,其中每个记录对应于图形中找到的路径。 返回的列使用模式中定义的节点和/或边缘属性在运算符的 project
子句中定义。 可变长度边缘的属性和函数作为动态数组返回。 数组中的每个值对应于可变长度边缘的一次出现。
示例
以下示例演示如何使用 graph-shortest-paths
运算符查找交通网络中的两个车站之间的最短路径。 该查询根据 connections
中的数据构造图形,并查找从 "South-West"
车站到 "North"
车站的最短路径,考虑最多五个连接长的路径。 由于默认输出为 any
,因此它会找到任何最短路径。
let connections = datatable(from_station:string, to_station:string, line:string)
[
"Central", "North", "red",
"North", "Central", "red",
"Central", "South", "red",
"South", "Central", "red",
"South", "South-West", "red",
"South-West", "South", "red",
"South-West", "West", "red",
"West", "South-West", "red",
"Central", "East", "blue",
"East", "Central", "blue",
"Central", "West", "blue",
"West", "Central", "blue",
];
connections
| make-graph from_station --> to_station with_node_id=station
| graph-shortest-paths (start)-[connections*1..5]->(destination)
where start.station == "South-West" and destination.station == "North"
project from = start.station, path = map(connections, to_station), line = map(connections, line), to = destination.station
输出
发件人 | 路径 | 线 | 接收方 |
---|---|---|---|
South-West | [ “南”, “Central”, “North” ] |
[ “red”, “red”, “red” ] |
北部 |
以下示例与前面的示例一样,查找运输网络中的最短路径。 但是,它使用 output=all
,因此会返回所有最短路径。
let connections = datatable(from_station:string, to_station:string, line:string)
[
"Central", "North", "red",
"North", "Central", "red",
"Central", "South", "red",
"South", "Central", "red",
"South", "South-West", "red",
"South-West", "South", "red",
"South-West", "West", "red",
"West", "South-West", "red",
"Central", "East", "blue",
"East", "Central", "blue",
"Central", "West", "blue",
"West", "Central", "blue",
];
connections
| make-graph from_station --> to_station with_node_id=station
| graph-shortest-paths output=all (start)-[connections*1..5]->(destination)
where start.station == "South-West" and destination.station == "North"
project from = start.station, path = map(connections, to_station), line = map(connections, line), to = destination.station
输出
发件人 | 路径 | 线 | 接收方 |
---|---|---|---|
South-West | [ “南”, “Central”, “North” ] |
[ “red”, “red”, “red” ] |
北部 |
South-West | [ “West”, “Central”, “North” ] |
[ “red”, “blue”, “red” ] |
北部 |