1

I have CALL apoc.algo.allSimplePaths(A, B, '', 3) result that look like this:

  • [A –>relation1 –>B]
  • [A –>relation2 –>B]
  • [A –>relation2->C->relation 1–>B]
  • [A –>relation5->D->relation 3–>B]
  • [A –>relation2->Y->relation 1–>B]
  • [A –>relation2->E->relation 1–>B]
  • [A –>relation2->D–>relation2->F->relation 1–>B]
  • [A –>relation2->F–>relation4->Y->relation 2–>B]

Desired result: FOREACH pathlength, randomly return 1 row

  • [A –>relation2 –>B]

  • [A –>relation2->Y->relation 1–>B]

  • [A –>relation2->D–>relation2->F->relation 1–>B]

The result is representative of all pathlengths:

The first row: 

[A –>relation2 –>B]     is a sample from path length 1 The second row:

[A –>relation2->Y->relation 1–>B   is a sample from path length 2  the third row:

[A –>relation2->D–>relation2->F->relation 1–>B].   is a sample from path length 3 

1 Answers1

1

To solve this, we'll need to collect the paths according to their length, so we have a row and list per length, then shuffle the collected paths and take one.

...
CALL apoc.algo.allSimplePaths(A, B, '', 3) YIELD path
WITH length(path) as length, collect(path) as paths
WITH length, apoc.coll.shuffle(paths)[0] as randomPath
ORDER BY length ASC
RETURN randomPath
InverseFalcon
  • 29,576
  • 4
  • 38
  • 51