0

Given a grid size(m*n), how could one generate a random path between a start(x1, y1) and end(x2, y2) point with the added condition that the random path covers all the nodes

e.g.

  • Grid of size 5*6
  • Start Point -> (1, 1)
  • End Point -> (1, 6)

What approach can be used to generate a random path that begins at (1, 1) and ends at (1, 6), plus the path travels through remaining 28 nodes?

future.open
  • 25
  • 1
  • 5
  • Use a [depth-first search](https://en.wikipedia.org/wiki/Depth-first_search) to find every possible path (there are 397 between (1,1) and (1,6)). Then generate a random number between 0 and 396, and that's the path to choose. Note that some choices of start and end, e.g. (1,1) and (5,1), have no paths. – user3386109 Jun 20 '20 at 04:25
  • @user3386109 Thanks, That is my approach currently. This is leading to some abnormally high run-times for higher dimension grids. I was hoping there would be another approach :). Appreciate your comment, at least, now I know I am not alone in thinking of this approach – future.open Jun 21 '20 at 14:25
  • Yup, I looked at this a little more today. Bottom line: the search space is huge, and the number of unique [Hamiltonian paths](https://en.wikipedia.org/wiki/Hamiltonian_path_problem) is small. So although it's easy to find a Hamiltonian path through the grid (e.g. zig zag from left to right, then right to left), finding a path that looks random is not easy. – user3386109 Jun 22 '20 at 22:43
  • The issue is demonstrated by [this simple example](https://i.stack.imgur.com/lhGOF.png). Having followed the solid line to point (3,4), the only valid way forward is to follow the green dotted path. But a naive DFS could spend hours exploring paths that go down or right from the end of the solid path. – user3386109 Jun 22 '20 at 22:43

0 Answers0