为什么所有对最短路径找不到子网之间的某些路径?

问题描述 投票:0回答:1

例如,在我的情况下,应该有从 sub2 到 sub6、sub2 到 sub8、sub2 到 tgw2 等的路径。但我没有得到任何这些路径。

我的数据:

CREATE 
(s1:Subnet{name:"sub1"}),
(s2:Subnet{name:"sub2"}),
(s3:Subnet{name:"sub3"}),
(s4:Subnet{name:"sub4"}),
(n1:Network{name:"net1"}),
(s1)-[:SUB_OF]-> (n1),
(s3)-[:SUB_OF]-> (n1),
(s2)-[:SUB_OF]-> (n1),
(s4)-[:SUB_OF]-> (n1),
(s5:Subnet{name:"sub5"}),
(s6:Subnet{name:"sub6"}),
(s7:Subnet{name:"sub7"}),
(s8:Subnet{name:"sub8"}),
(n2:Network{name:"net2"}),
(t1:TGW{name:"tgw1"}),
(t2:TGW{name:"tgw2"}),
(s5)-[:SUB_OF]-> (n2),
(s6)-[:SUB_OF]-> (n2),
(s7)-[:SUB_OF]-> (n2),
(s8)-[:SUB_OF]-> (n2),
(s5)-[:CONNECT_TO]->(t1),
(s3)-[:CONNECT_TO]->(t1),
(s4)-[:CONNECT_TO]->(t2),
(s8)-[:CONNECT_TO]->(t2);

我的预测是:

MATCH (src:Subnet)-[r:SUB_OF | CONNECT_TO]->(trg:Network|TGW)
RETURN gds.graph.project(
  'cypherGraph10',
  src,
  trg,
  {relationshipType: type(r)},
  {undirectedRelationshipTypes: [type(r)]
})

尝试此操作后,我没有得到所有的答案:

CALL gds.allShortestPaths.stream('cypherGraph10') YIELD sourceNodeId, targetNodeId, distance
WHERE gds.util.isFinite(distance) AND sourceNodeId <> targetNodeId
WITH gds.util.asNode(sourceNodeId) AS source, gds.util.asNode(targetNodeId) AS target, distance
WHERE NONE(n IN [source, target] WHERE n:Network|TGW)
RETURN source.name AS source, target.name AS target, distance
ORDER BY distance DESC, source ASC, target ASC
graph neo4j cypher graph-data-science
1个回答
1
投票

您设置的

undirectedRelationshipTypes
不正确。请改用以下内容:

MATCH (src:Subnet)-[r:SUB_OF|CONNECT_TO]->(trg:Network|TGW)
RETURN gds.graph.project(
  'cypherGraph10',
  src,
  trg,
  {relationshipType: type(r)},
  {undirectedRelationshipTypes: ['*']})

问题在于您动态设置

undirectedRelationshipTypes
。图形投影只会设置为无向关系,这些关系的类型为
type(r)
第一次调用时所具有的任何值,即关系
r
的类型,无论恰好是
MATCH
子句中的第一个记录。例如,如果第一条记录恰好与类型
SUB_OF
有关系,则只有这些关系在投影中才会是无向的。

这很重要的原因是 APSP 只会按照其方向遍历有向关系。如果您查看图表,如果所有

CONNECT_TO
关系都是有向的,那么图表将在绿色
TGW
节点处进行分区:

在这种情况下,您将不会获得连接 sub2 到 sub6 的路径。

© www.soinside.com 2019 - 2024. All rights reserved.