'Enumerating k number of paths passing through a node in Neo4j

Currently, I am using the random walk algorithm in Neo4j to do some path computations. The random walk algorithm has the option of specifying source nodes from which the walks can be constructed. However, I am wondering if there is a way to enumerate k number of paths passing through a certain node than making it a source node and combining the paths? Here k is a parameter, say, 2 paths or 3 paths.



Solution 1:[1]

If I'm reading your question correctly you want to find k random paths of length i that pass through node n and you don't want to combine the paths of two queries. If so then the easiest way might be to find all paths containing n and take a random subset of that, i.e.:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Simon Thordal