Neo4j 如何获得多个输入节点的最小关系范围?

Neo4j 如何获得多个输入节点的最小关系范围?,neo4j,cypher,Neo4j,Cypher,下面的语句在Neo4j中创建了一个具有代表性的数据集 我想返回给定输入节点集的最小关系图 e、 g.,节点集“C”、“g”、“D”的最小关系图为“C,g,D”;而对于“A”,“E”则是“A,G,E” 特定节点的数量是任意的,但要求返回的范围是最小的 如何编写此查询 样本数据 这里有一个简单的解决方案。生成集合中每对节点之间的最短路径的完整列表。然后将结果路径减少到不同的节点集 // set up the input nodes as a collection of attribute value

下面的语句在Neo4j中创建了一个具有代表性的数据集

我想返回给定输入节点集的最小关系图

e、 g.,节点集“C”、“g”、“D”的最小关系图为“C,g,D”;而对于“A”,“E”则是“A,G,E”

特定节点的数量是任意的,但要求返回的范围是最小的

如何编写此查询

样本数据
这里有一个简单的解决方案。生成集合中每对节点之间的最短路径的完整列表。然后将结果路径减少到不同的节点集

// set up the input nodes as a collection of attribute values to be matched
WITH ['C','G','D'] as inputs

// build a list of pairs that are not the same 
UNWIND RANGE(0,size(inputs)-1) as i
UNWIND RANGE(0,size(inputs)-1) as j
WITH CASE 
  WHEN i > j THEN [inputs[i],inputs[j]]
  ELSE null
END as pair

// find all of the shortest paths for each pair
MATCH p=allShortestPaths((a:Table {name: pair[0]})-[:Link*]-(b:Table {name: pair[1]}))
WITH p

// recollect the distinct nodes from the shortest paths
UNWIND nodes(p) as n
RETURN COLLECT(DISTINCT n) as min_relational_scope

这似乎是一个最短路径函数的工作。。。最短路径只用于两个节点,不能得到多个输入节点的最简单关系。求答案,在某些情况下是不正确的。对于这种情况:CREATE(A:Table{name:'A'})CREATE(B:Table{name:'B'})CREATE(C:Table{name:'C'})CREATE(D:Table{name:'D'})CREATE(A)-[:Link]>(B),(B)-[:Link]>(C),(C)-[:Link]>(D),[:Link]>(A)我可能期望输入节点“A,B,C”的结果是“A,B,C”,但它返回“A,B,C,D”。
// set up the input nodes as a collection of attribute values to be matched
WITH ['C','G','D'] as inputs

// build a list of pairs that are not the same 
UNWIND RANGE(0,size(inputs)-1) as i
UNWIND RANGE(0,size(inputs)-1) as j
WITH CASE 
  WHEN i > j THEN [inputs[i],inputs[j]]
  ELSE null
END as pair

// find all of the shortest paths for each pair
MATCH p=allShortestPaths((a:Table {name: pair[0]})-[:Link*]-(b:Table {name: pair[1]}))
WITH p

// recollect the distinct nodes from the shortest paths
UNWIND nodes(p) as n
RETURN COLLECT(DISTINCT n) as min_relational_scope