在 Neo4j 中枚举通过节点的 k 条路径

Enumerating k number of paths passing through a node in Neo4j

目前,我正在使用 Neo4j 中的随机游走算法进行一些路径计算。随机游走算法可以选择指定可以构建游走的源节点。但是,我想知道是否有一种方法可以枚举通过某个节点的 k 条路径,而不是将其作为源节点并组合路径?这里 k 是一个参数,比如 2 路径或 3 路径。

如果我没看错你的问题,你想找到 k 长度为 i 且经过节点 n 的随机路径,而你不想合并这些路径两个查询。 如果是这样,那么最简单的方法可能是找到所有包含 n 的路径并随机取其中的一个子集,即:

MATCH (n) WHERE id(n) = $n_id // Find the node you want the paths to contain
path = MATCH (foo)-[:REL_TYPE*..i]->(bar) // Find all paths containing the node
WHERE n in nodes(path) 
AND NOT (foo = n OR bar = n) // Paths should not start or end with n
WITH collect(path) AS paths // Put all the paths in a list
RETURN apoc.coll.randomItems(paths, $k) // Return a random subset of size k