Search code examples
neo4jcypherrandom-walk

Random walk in Neo4j doesn't output (default) expected number of walks


I am extracting a simple graph with just 4 nodes, all nodes connected, meaning there are no isolated nodes and there is a path between any two of them. Then I am running a random walk algorithm. However, the algorithm outputs only two walks while technically it should be four - one for each node. Not sure what is the problem and any help will be appreciated. Here is the code -

// Cypher query to extract the subgraph with 4 nodes - any two have a path

MATCH p=(m1)-[r]->(m2) 
WHERE (m1 IN events AND m2 IN events)
WITH COLLECT(m1) AS m1s, COLLECT(m2) AS m2s, COLLECT({source:m1, target:m2, type: TYPE(r)}) AS rels 

// Create a projection

WITH apoc.coll.toSet(m1s + m2s) AS nodes, rels

CALL gds.graph.create.cypher(
  "example",
   "UNWIND $nodes AS n RETURN id(n) AS id, labels(n) AS labels",
   "UNWIND $rels AS rel RETURN id(rel['source']) AS source , id(rel['target']) AS target, 
   { LINKS: { orientation: 'UNDIRECTED' } }, rel['type'] AS type", 
   {parameters: {nodes:nodes, rels:rels} }

YIELD graphName AS graph, nodeQuery, 
      nodeCount AS nodes_count, relationshipQuery, 
      relationshipCount AS rels_count

// Call the Random Walk algorithm

CALL gds.beta.randomWalk.stream(
  'example',
  {
    walkLength: 3,
    walksPerNode: 1,
    randomSeed: 42,
    concurrency: 1
  }
)

YIELD nodeIds, path

RETURN nodeIds, [node IN nodes(path) | node.name ] AS event_name


Solution

  • Upon @micro5 suggestion (see comments above) I replaced the statement:

    MATCH p=(m1)-[r]->(m2) with MATCH p=(m1)-[r]-(m2) and the random walk executes as expected.