What algorithm to use to generate random path of given length

I have a map size of 8 x 8 cells with no obstacles. I should generate random path from start cell to the end cell with given length. Path can move up/down/left/right directions but not cross itself. What algorithm I can use to do this?

• You could start with randomized DFS. That is, when picking a neighbor, choose one randomly, e.g. uniformly at random. So this would be an instance of rejection sampling. The effectiveness of this strategy will depend on your other constraints. In graph-theoretic terms, I think you want to find a $k$-path from vertex $s$ to vertex $t$ in a grid graph.
– Juho
Jul 14 '15 at 17:29
• I doubt that there is an efficient algorithm for this. @Juho has a suggestion that might work well enough in practice, but the paths generated would not be uniformly distributed. Just counting the number of paths in a grid seems to be an open problem for which no efficient algorithm is known. Jul 15 '15 at 9:41
• Ingoring the start and end cells, this problem has been studied under the name "self-avoiding walk". The main take-away from the wikipedia entry (en.wikipedia.org/wiki/Self-avoiding_walk) is that they are tricky beasts (and in particular, counting them is difficult). Jul 17 '15 at 19:44

Algo for solving problem when path self-intersections are allowed

Auxiliary problem

First let's solve another problem:

How many paths of length $K$ exists starting at coordinate $(StartRow, StartColumn)$.

Such problem can be solved by dynamic programming: let $dp[K][R][C]$ be answer for question:

How many paths of length $K$ exists that start in $(StartRow, StartColumn)$ and ends in $(R, C)$".

Base case: $$dp[0][StartRow][StartColumn] = 1$$

Step of dynamic: $$dp[K][R][C] = dp[K-1][R-1][C] + dp[K-1][R+1][C] + dp[K-1][R][C-1] + dp[K-1][R][C+1]$$

Here we suppose $dp[K][X][Y]=0$, for any $X$ and $Y$ that outside of grid (e.g. $X < 1$ or $X > N$ or $Y < 1$ or $Y > N$) Also we can even add more constraints for problem and add obstacles: for doing so - we need to suppose $dp[K][R][C]=0, \forall K$, where cell $(R, C)$ is impassable (e.g. is obstacle).

This will take $O(N*M*K)$ time, where $N$ - number of rows, $M$ - number of columns, and $O(N*M*K)$ memory if we will keep all steps of dynamic or $O(N*M)$ if we will keep only last step of dynamic.

Original problem

Generate random path of length $K$ between cells $S(R_s, C_s)$ and $E(R_e, C_e)$.

1. First of all we need count how many paths of length $K$ exists between this cells, for that we can use previously described dynamic: futhermore run it from end cell (e.g. count how many paths of length $K$ exist that start at $(R_e, C_e)$) and save all step of dynamic (we will use it in next steps).

2. Now add order relation between all paths of length $K$ starting at cell $(R, C)$ and ending at cell $E(R_e, C_e)$:

• if $K=0$ we have $0$ or $1$ such paths (such collection is always ordered);
• if $K>0$ let's suppose: Paths that go UP $<$ Paths that go RIGHT $<$ Paths that go DOWN $<$ Paths that go LEFT (and for ordering paths in each of 4 groups we can recursevly apply this definition again).
3. Then we know how many paths of length $K$ exist between cells $S$ and $E$, lets name it $A=dp[k][R_s][C_s]$, now we can uniformly at random peek number $L\in[0, A)$ that will identify choosen path among all $A$ available.

4. We had ordered all $A$ paths and choose some path to output $L$ ($0-indexed$) and just need to find it. Let $choose(R_c, C_c, L, K)$ - function that return $Lth$ path of length $K$ staring at cell $(R_c, C_c)$ and ending at $E(R_e, C_e)$:

$$choose(R_c, C_c, L, K) =\small\left\{ \begin{array}{ll} \langle empty \rangle, K = 0\\ \langle UP, choose(R_c - 1, C_c, L - 0, K - 1)\rangle, \\ \hspace{45pt} L \in [0, Count_u)\\ \langle RIGHT, choose(R_c, C_c + 1, L - Count_u, K - 1)\rangle, \\ \hspace{45pt} L \in [Count_u, Count_u + Count_l)\\ \langle DOWN, choose(R_c + 1, C_c, L - (Count_u + Count_l), K - 1)\rangle, \\ \hspace{45pt} L \in [Count_u + Count_l, Count_u + Count_l + Count_d)\\ \langle LEFT, choose(R_c, C_c - 1, L - (Count_u + Count_l + Count_d), K - 1)\rangle, \\ \hspace{45pt} L \in [Count_u + Count_l + Count_d, A)\\ \end{array} \right.\\$$ $$Where \forall K, R_c, C_c\\ Count_u=dp[K - 1][R_c - 1][C_c]\\ Count_l=dp[K - 1][R_c][C_c + 1]\\ Count_d=dp[K - 1][R_c + 1][C_c]\\ Count_r=dp[K - 1][R_c][C_c - 1]\\$$

And $Count_u$ means how many paths of length $K$ starts at $(R_c, C_c)$ and ends at $(R_e, C_e)$ and first move is $UP$, $Count_l$, $Count_d$, $Count_r$ defines respectively for another directions.

More intuitively, imagine we at $X$ cell choosing for $7th$ path:

 ... _ 4 _ 5 X 0 _ 9 _ ... 

If we go $UP$ we will follow paths $\in [0, 4)$, there is no paths to $RIGHT$, if we go $DOWN$ we will follow paths $\in [4, 4+9)$, so we need to go $DOWN$ and choose $(7 - 4)th$ path among all that starts in that cell.

Note: due to we define ordering on paths $I'th$ path everytime will be the same (so it's possible to store path just as index number among all such paths and restore path when it is needed). Also need to notice that order $UP < RIGHT < DOWN < LEFT$ is not only right choise, you can use any order - just keep it thorugh all algorithm flow the same.

• Nice answer, but unless I'm missing something it does allow a path to cross itself. Jul 17 '15 at 17:20
• Yes, such algo allows path self-intersection. Almost forget that this is not allowed by original topic while thinking. Jul 17 '15 at 17:33