概述
两点间的最短路径是两点间边数最少的路径。使用以下最短路径选择器可将路径模式匹配的结果限制为最短路径:
选择器 |
描述 |
---|---|
ALL SHORTEST |
从每个分区中选择所有最短路径 |
SHORTEST k |
从每个分区中选择k [1]条最短路径;非确定性选择 |
SHORTEST k GROUP [2] |
根据路径长度对路径分组,升序排列结果,再从每个分区中选择前k 个组中的所有路径;确定性选择 |
[1] k
为非负整数。若路径数少于k
,将保留全部路径。若未设定k
,取其默认值1。
[2] 关键字GROUP
和GROUPS
可以互换使用,不会影响结果。
最短路径通常从图中两点间的所有可能路径中选择,路径有无长度限制均可。使用带量词的路径模式可以重复路径的某些部分,并指定重复次数的范围,从而探索更多潜在路径。
GQL暂不支持选择“最低开销”路径(类似于“最短”路径,但最小化了路径的总成本,即边权重)。如需此功能,您可考虑使用UQL的命令
ab()
或autonet()
。
最短路径暂不支持子路径变量。
示例图集
以下示例根据该图集运行:
在空图集中运行以下语句创建示例图集:
INSERT (zenith:City {_id: "Zenith"}),
(arcadia:City {_id: "Arcadia"}),
(verona:City {_id: "Verona"}),
(nebula:City {_id: "Nebula"}),
(mirage:City {_id: "Mirage"}),
(lunaria:City {_id: "Lunaria"}),
(solara:City {_id: "Solara"}),
(eldoria:City {_id: "Eldoria"}),
(nexis:City {_id: "Nexis"}),
(arcadia)-[:Links]->(zenith),
(arcadia)-[:Links]->(verona),
(arcadia)-[:Links]->(solara),
(mirage)-[:Links]->(arcadia),
(nebula)-[:Links]->(verona),
(mirage)-[:Links]->(nebula),
(verona)-[:Links]->(mirage),
(mirage)-[:Links]->(eldoria),
(solara)-[:Links]->(eldoria),
(lunaria)-[:Links]->(solara)
ALL SHORTEST
使用选择器ALL SHORTEST
获取全部最短路径。
MATCH p = ALL SHORTEST (a)-{,10}(b)
WHERE a._id = 'Arcadia' AND b._id = 'Eldoria'
RETURN p
结果:
p |
---|
(:City {_id: "Arcadia"})<-[:Links]-(:City {_id: "Mirage"})-[:Links]->(:City {_id: "Eldoria"}) |
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Solara"})-[:Links]->(:City {_id: "Eldoria"}) |
SHORTEST k
使用选择器SHORTEST k
获取每个分区中k
条最短路径。
MATCH p = SHORTEST 2 (a:City)-{,10}(b:City)
WHERE a._id = 'Arcadia' AND b._id = 'Eldoria'
RETURN p
结果:
p |
---|
(:City {_id: "Arcadia"})<-[:Links]-(:City {_id: "Mirage"})-[:Links]->(:City {_id: "Eldoria"}) |
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Solara"})-[:Links]->(:City {_id: "Eldoria"}) |
MATCH p = SHORTEST 3 (a:City)-{,10}(b:City)
WHERE a._id = 'Arcadia' AND b._id = 'Eldoria'
RETURN p
由于Arcadia
和Eldoria
之间只有2条总长为2
的最短路径,因此需要再选择1条第二短的路径。本例中第二短的路径总长为3
,且只有1条,因此返回以下3条路径:
p |
---|
(:City {_id: "Arcadia"})<-[:Links]-(:City {_id: "Mirage"})-[:Links]->(:City {_id: "Eldoria"}) |
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Solara"})-[:Links]->(:City {_id: "Eldoria"}) |
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Verona"})-[:Links]->(:City {_id: "Mirage"})-[:Links]->(:City {_id: "Eldoria"}) |
SHORTEST k GROUP
使用选择器SHORTEST k GROUP
从每个分区中获得第1短到第k
短路径。该选择器根据路径长度对路径分组,升序排列结果,再从每个分区中选择前k
个组中的所有路径。
MATCH p = SHORTEST 3 GROUP (a:City)-[]-+(b:City)
WHERE a._id = 'Arcadia' AND b._id = 'Eldoria'
RETURN p
查询返回了2条总长为2
的最短路径,1条总长为3
的第二短路径,以及1条总长为4
的第三短路径:
p |
---|
(:City {_id: "Arcadia"})<-[:Links]-(:City {_id: "Mirage"})-[:Links]->(:City {_id: "Eldoria"}) |
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Solara"})-[:Links]->(:City {_id: "Eldoria"}) |
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Verona"})-[:Links]->(:City {_id: "Mirage"})-[:Links]->(:City {_id: "Eldoria"}) |
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Verona"})<-[:Links]-(:City {_id: "Nebula"})<-[:Links]-(:City {_id: "Mirage"})-[:Links]->(:City {_id: "Eldoria"}) |
分区
从理论上说,当路径模式匹配到多个起点和终点时,匹配结果根据不同的起始点对进行分区,随后选择器在各分区中选择最短路径。
本条查询中,首先生成a
和b
的笛卡尔积,随后变量a
和b
在最短路径中被引用,因此共有4条最短路径从4个分区中被选中:
MMATCH p = SHORTEST 1 (a:City WHERE a._id = 'Zenith' OR a._id = 'Arcadia')-{,10}(b:City WHERE b._id = 'Eldoria' OR b._id = 'Nebula')
RETURN p
结果:
p |
---|
(:City {_id: "Zenith"})<-[:Links]-(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Verona"})<-[:Links]-(:City {_id: "Nebula"}) |
(:City {_id: "Zenith"})<-[:Links]-(:City {_id: "Arcadia"})<-[:Links]-(:City {_id: "Mirage"})-[:Links]->(:City {_id: "Eldoria"}) |
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Verona"})<-[:Links]-(:City {_id: "Nebula"}) |
(:City {_id: "Arcadia"})<-[:Links]-(:City {_id: "Mirage"})-[:Links]->(:City {_id: "Eldoria"}) |
单起点最短路径
本条查询获取Arcadia
和其他每个城市间的1条最短路径:
MATCH p = SHORTEST 1 (c1:City {_id: 'Arcadia'})-{,10}(c2:City)
WHERE c2._id <> c1._id
RETURN p
在本图里,Arcadia
可以到达除Nexis
外的7个城市。以下为可能的返回结果:
p |
---|
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Verona"}) |
(:City {_id: "Arcadia"})<-[:Links]-(:City {_id: "Mirage"}) |
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Solara"}) |
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Zenith"}) |
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Verona"})<-[:Links]-(:City {_id: "Nebula"}) |
(:City {_id: "Arcadia"})<-[:Links]-(:City {_id: "Mirage"})-[:Links]->(:City {_id: "Eldoria"}) |
(:City {_id: "Arcadia"})-[:Links]->(:City {_id: "Solara"})<-[:Links]-(:City {_id: "Lunaria"}) |