10

A frog is travelling from point A $(0,0)$ to point B $(5,6)$ but each step can only be $1$ unit up or $1$ unit to the right. Also, the frog refuses to move three steps in the same direction consecutively. Compute the number of ways the frog can move from A to B.

Is this a $3$D DP problem? If we remove the "three steps in the same direction consecutively" condition then we can define $f(i,j)$ to be the number of paths from $(0,0)$ to $(i,j)$ then from recurrence/DP we have

$$f(i,j) = f(i-1,j)+f(i,j-1)$$ where we will go one step back for bottom and result in $f(i,j-1)$ and one step left and result in $f(i-1,j)$. Then for no of steps we need to add two options. How do we modify it by adding these condition? Is there specifically a Dynamic programming approach? It will be useful for me to solve these problems under a similar concept.

This is a math question. I would be very happy if you can explain me without code. Using a tree and a recurrence relation. For simplicity you can also reduce the dimension from $(5,6)$ to any smaller number.

Ri-Li
  • 9,466
  • 4
  • 56
  • 104
  • you may be interested in this answer to a similar question: https://math.stackexchange.com/questions/870446/number-of-paths-from-0-0-to-n-k-where-all-four-directions-are-allowed-u/1088231#1088231 – Henry Lee Aug 20 '24 at 11:56
  • I think this is different. If not can you direct me how to use the linked result in my problem? – Ri-Li Aug 20 '24 at 11:58
  • 3
    There's no need for a recursion in the unconstrained count...you have to make exactly $5$ right moves and exactly $6$ up moves in whatever order you like, so there are $\binom {11}5=462$ unconstrained paths. If you are automating this, that's a small enough number to simply check all of them for the constraint. – lulu Aug 20 '24 at 12:06
  • To do it by hand: Imagine the $6$ up moves are in a line, with gaps between them (and before and after). The $5$ right moves must be placed, somehow, in these gaps. No more than two right moves can go in a single gap. There must be at least two empty gaps, so I'd count from there. Fix the number of empty gaps and count each case. – lulu Aug 20 '24 at 12:10
  • You can denote each path from $(0,0)$ to $(5,6)$ as a permutation of the string $\text{LLLLLUUUUUU}$ where you denote an upward movement by $U$ and a leftward movement by $L$. There are exactly $\frac{11!}{5!6!}=462$ such strings. You now need to remove all strings that contain either consecutive $\text{UUU}$ or consecutive $\text{LLL}$. To do that we count number of said strings and apply principle of inclusion-exclusion. But I'm not sure how you'd count said strings. – Sahaj Aug 20 '24 at 14:30
  • A simple python program however, can do the computation in a very quick time. – Sahaj Aug 20 '24 at 14:31
  • What makes you think that $f(i,j,k)$ with $i,j$ as above, and $k$ with number of times you already moved in the same direction doesn't work? – ConnFus Aug 20 '24 at 18:36
  • @ConnFus so do you think it can be done as 3D DP? Can you frame the structure with $f(i,j,k)$ and compute the initial few values? – Ri-Li Aug 20 '24 at 21:33
  • @lulu can you elaborate and write it as an answer? I got your intuition about the gaps. – Ri-Li Aug 20 '24 at 21:33
  • @Sahaj you can include your code if you want. – Ri-Li Aug 20 '24 at 21:34
  • @Ri-Li No, not as 3D, but it's not particularly hard to do as dynamic program. I'd advise you to try it yourself first. You need 4 dimensions – ConnFus Aug 20 '24 at 22:07
  • A general comment if you think the easiest process will be by code. Can you please write the code with the logic that you are using. – Ri-Li Aug 21 '24 at 02:00

5 Answers5

6

The bivariate generating function for the words on the alphabet $\{ L, U \}$ not containing the subwords $LLL$, $UUU$ is

$$ g(x,y) = \frac{1}{1-x-y + \frac{x^3}{1+x+x^2} + \frac{y^3}{1+y+y^2} } $$

This is deduced with the Jackson-Goulden cluster method. The two bad words don't have any overlap with each other, and do have (all possible) overlaps with themselves. So we have the diagonal system (using $w$ to denote the weight function)

$$\begin{aligned} w(C[LLL]) &= -w(LLL) - (w(L)+w(LL))w(C[LLL]) \\ w(C[UUU]) &= -w(UUU) - (w(U)+w(UU))w(C[UUU]) \\ \end{aligned}$$

i.e.

$$\begin{aligned} w(C[LLL]) &= -x^3 - (x+x^2)w(C[LLL]) \\ w(C[UUU]) &= -y^3 - (y+y^2)w(C[UUU]) \\ \end{aligned}$$

Then

$$ g(x,y) = \frac{1}{1-w(\text{all alphabets})-w[LLL]-w[UUU]} \\ = \frac{1}{1-x-y + \frac{x^3}{1+x+x^2} + \frac{y^3}{1+y+y^2} }. $$

From this rational generating function a recurrence can be acquired. But it is kind of cumbersome. We can massage it to

$$ g(x,y) = \frac{1}{1 - \frac{x+x^2}{1+x+x^2} - \frac{y+y^2}{1+y+y^2} } = \frac{(1+x+x^2)(1+y+y^2)}{(1+x+x^2)(1+y+y^2) - x(1+x)(1+y+y^2) - y(1+y)(1+x+x^2) } = \frac{(1+x+x^2)(1+y+y^2)}{1-xy(1+x)(1+y) } \\ = (1+x+x^2)(1+y+y^2) \sum_{j=0}^\infty (xy(1+x)(1+y))^j \\ = (1+x+x^2)(1+y+y^2) \sum_{j=0}^\infty \sum_{a=0}^j \sum_{b=0}^j \binom{j}{a} \binom{j}{b} x^{j+a}y^{j+b} \\ $$

And then extract the terms $x^5y^6$. The answer: $113$.

ploosu2
  • 12,367
  • Could you explain how you got the generating function? The link that you mentioned seems to discuss generating functions of one variable but you have two. – Orbifold Nov 06 '24 at 22:14
  • @Orbifold I edited that in. Multivariateness doesn't affect it much, just use the weight function as is. One thing that changes in the final formula ($f(s)$ on page 9 in the paper) is the weight of all alphabet which is $ds$ in the paper, but now it is $x+y$. – ploosu2 Nov 07 '24 at 06:45
  • @ploosu2 i have posted a question about goulden jackson if you interest – Fernando Luis Esquivel Jun 14 '25 at 16:36
5

An elementary (if tedious and error prone) method.

Note: I am including this at the OP's request. In practice, I would automate the search...the pencil and paper method is a bit too error prone to be relied on. That said, it is fast and not especially difficult. Took me well under $10$ minutes by hand (which won't mean much if an error is detected).

We need to make $6$ up moves and $5$ right moves. The only constraint on the order is that we can't make three consecutive moves of the same type. So, arrange the $6$ up moves in a line and consider the seven gaps between them. The $5$ right moves must go in those gaps, no more than two of them can go in a single gap. Label the gaps $g_i$ from left to right (so $g_1,g_7$ are on the ends).

We consider the ways to choose the empty gaps, consistent with the constraint.

The bad selections are those that contain (at least) one of $(g_2,g_3), (g_3,g_4), (g_4,g_5), (g_5,g_6)$

Case I: exactly two empty gaps. We've seen that there are exactly $4$ bad choices, and $\binom 72-4=\boxed {17}$

Case II: exactly three empty gaps. the bad choices are now:

$$(g_1,g_2,g_3), (g_2,g_3,g_4), (g_2,g_3, g_5), (g_2,g_3,g_6), (g_2,g_3,g_7)$$ $$(g_1,g_3,g_4),(g_3,g_4,g_5),(g_3,g_4,g_6),(g_3,g_4,g_7)$$ $$(g_1,g_4,g_5),(g_2,g_4,g_5), (g_4,g_5,g_6),(g_4,g_5,g_7)$$ $$(g_1,g_5,g_6),(g_2,g_5,g_6),(g_3,g_5,g_6), (g_5,g_6,g_7)$$

Thus there are $17$ bad choices, so $\binom 73-17=18$ good ones. For each good choice, we need to put two right moves in one non-empty gap and one each in the other three, so there are $4$ ways to finish. Thus, there are $4\times 18=\boxed {72}$ .

Case III. exactly four empty gaps. In this case, since there are only three occupied gaps, it is easier to count the good choices. Looking at the list of excluded pairs (of empty gaps) we see that we must occupy at least one of $g_2,g_3$ and at least one of $g_5,g_6$ and then we only have one more free choice. Not hard to conclude that the good selections are: $$(g_1,g_3,g_5), (g_2,g_3,g_5),(g_3,g_5,g_6),(g_2,g_4,g_5)$$ $$(g_2,g_4,g_6),(g_3,g_4,g_5),(g_3,g_4,g_6),(g_3,g_5,g_7)$$

Given a good choice we need to place two of the right moves in two of the occupied gaps and one right move in the other, so there are three choices for each good choice. Thus there are $8\times 3=\boxed {24}$.

Finally: in total there are $17+72+24=\boxed {113}$ possible solutions.

Sanity Check for case III: it is easy to see that $(g_a,g_b,g_c)$ is a good selection if and only if $(g_{8-c},g_{8-b},g_{8-a})$ is and the above list satisfies that.

lulu
  • 76,951
  • Thanks a lot @lulu. For automating and using code also, I was curious what is the logic behind the code. If you have that idea. You can include along with code. I have an idea with and wrote a code which gave me 113 as an answer. I will add it if I am confident about the logic. – Ri-Li Aug 21 '24 at 01:01
  • +1 from my side. – Ri-Li Aug 21 '24 at 02:00
  • The numbers are so small that the automation is easy. While there are algorithms for listing binary strings with a fixed length and fixed number of $1's$, you don't need them here. Just list the natural numbers up to $2^{12}-1$ (in base $2$), then check each for two things: are there exactly $5$ ones? Are there any blocks of the form $111$ or $000$? This crude approach won't work for bigger numbers, but neither would the enumeration I used. – lulu Aug 21 '24 at 10:53
  • Did you mean to put $(g_3, g_5, g_6)$ instead of $(g_2, g_5, g_6)$ for case 3? – sushister Aug 22 '24 at 16:01
  • @sushiter Thanks for catching the error. Corrected now. – lulu Aug 22 '24 at 16:10
  • I just added a 2nd answer that extends the analysis in your answer. The extension leads to a closed form formula (of sorts). – user2661923 Nov 26 '24 at 02:01
5

Alternative approach, which is based on partitioning an integer.

To partition $~5~$ into terms all less than 3:

  • P5a: 2 - 2 - 1

  • P5b: 2 - 1 - 1 - 1

  • P5c: 1 - 1 - 1 - 1 - 1

To partition $~6~$ into terms all less than 3:

  • P6a: 2 - 2 - 2

  • P6b: 2 - 2 - 1 - 1

  • P6c: 2 - 1 - 1 - 1 - 1

  • P6d: 1 - 1 - 1 - 1 - 1 - 1

When combining a P5x with a P6y, if they both have the same number of terms, then either can go first. Otherwise, the one that has one extra term must go first.

Now, it is simply case work:


$\underline{\text{P5a : P6a}}$

$$2 \times 3 = 6.$$

The first factor represents that either P5a or P6a can have the first move. The second factor is the number of distinct ways that the P5a = 2 - 2 - 1 can distribute its moves.


$\underline{\text{P5a : P6b}}$

$$1 \times \binom{3}{1} \times \binom{4}{2} = 18.$$


$\underline{\text{P5b : P6a}}$

$$1 \times 4 = 4.$$


$\underline{\text{P5b : P6b}}$

$$2 \times 4 \times 6 = 48.$$


$\underline{\text{P5b : P6c}}$

$$1 \times 4 \times 5 = 20.$$


$\underline{\text{P5c : P6b}}$

$$1 \times 6 = 6.$$


$\underline{\text{P5c : P6c}}$

$$2 \times 5 = 10.$$


$\underline{\text{P5c : P6d}}$

$$1 = 1.$$


$\underline{\text{Final Computation}}$

$$6 + 18 + 4 + 48 + 20 + 6 + 10 + 1 = 113.$$

user2661923
  • 42,303
  • 3
  • 21
  • 46
  • Can you tell me your thought process behind why you didn't consider cases like $ P5a & P6c and P5b & P6d $? I understand that these combinations can't reach $ (5,6) $ from $ (0,0) $, but I can't develop the intuition to understand why we're doing so. Can you help me out in this regard? – Shs Tht Oct 07 '24 at 05:36
  • 1
    @ShsTht P5a has 3 terms, and P6c has 5 terms. If the two partitions each have the same number of terms, then either partition can go first. If the number of terms differ by 1, then the partition with the larger number of terms must go first. This is because the partitions must alternate moves. So, if the number of terms differ by more than 1, then the two partitions are incompatible. – user2661923 Oct 07 '24 at 06:54
  • Thanks! Using your method, If I change the question slightly and consider the frog moves from point A (0,0) to point B (5,4), will the answer now be 42? – Shs Tht Oct 07 '24 at 11:16
  • @ShsTht It is not nice to ask MathSE responders to try and hit a moving target. Your comment represents a different question. Standard MathSE protocol is that you present your new math problem as a separate MathSE posted question. If you do that, then I suggest that you provide a link from the old question to the new, and from the new question to the old (by editing your original posted question to add the new link). – user2661923 Oct 07 '24 at 16:01
  • Oh, I'm so sorry! I am still quite new to MathSE. I have posted it as a new question as you have suggested. Please find it at https://math.stackexchange.com/questions/4981697/number-of-ways-the-frog-moves-from-a-to-b Thank you!! – Shs Tht Oct 08 '24 at 05:01
2

This is not a 3D DP problem (in an algorithm sense) given the constraint that the frog can't move $k$ steps in the same direction consecutively ($k=3$ in this case), because the state of the frog is finite. That said, we can use another dimension to track its state:

  1. It has moved 1 unit up consecutively;
  2. It has moved 2 unit up consecutively;
  3. It has moved 1 unit to the right consecutively;
  4. It has moved 2 unit to the right consecutively.

The recurrence relations can thus be written as $$ \begin{aligned} f(i, j, 1) &= f(i,j-1,3) + f(i,j-1,4),\\ f(i,j, 2) &= f(i - 1, j, 1),\\ f(i, j, 3) &= f(i-1, j, 1) + f(i -1, j, 2),\\ f(i, j, 4) &= f(i, j-1, 3) \end{aligned} $$ with the initial values $f(1, 0, 1) = 1$ and so on. The third variable of the function $f$ corresponds to one of the 4 scenarios above. The size of the DP array should be the product of $B$'s coordinates and the number of states. Our desired value would be $\sum_{l=1}^4{f(x,y,l)}$ for some $B=(x,y)$.

This could, however, become a 3D DP problem like you said if $k$ is also mutable. The recurrence pattern would be lengthy while more or less the same as $k=3$. But in your case, the result would be $113$.

Here's a validation:

def dp(x, y):
    dp = np.zeros((x + 1, y + 1, 5), dtype=int)
dp[1, 0, 0] = dp[2, 0, 1] = dp[0, 1, 2] = dp[0, 2, 3] = 1
for i in range(1, x + 1):
    for j in range(1, y + 1):
        dp[i, j, 0] = dp[i - 1, j, 2] + dp[i - 1, j, 3]
        dp[i, j, 1] = dp[i - 1, j, 0]
        dp[i, j, 2] = dp[i, j - 1, 0] + dp[i, j - 1, 1]
        dp[i, j, 3] = dp[i, j - 1, 2]

return sum(dp[-1, -1])

print(dp(5, 6))

It gives exactly $113$.

This can be generalized for any $(x, y, k)$.

0

I am adding another answer, which is inspired by lulu's answer. Excerpting from lulu's answer:

We need to make $6$ up moves and $5$ right moves. The only constraint on the order is that we can't make three consecutive moves of the same type. So, arrange the $6$ up moves in a line and consider the seven gaps between them. The $5$ right moves must go in those gaps, no more than two of them can go in a single gap.

Note:
You also have an added constraint. Ignoring the $~2~$ outer gaps, focus only on the $~5~$ inner gaps. You can not have two consecutive inner gaps, each of size $~0.~$ This is because you (also) can not have $~3~$ consecutive up moves.

Setting $~n = 5, k = 6,~$ the desired enumeration is the number of solutions to

  • $x_1 + x_2 + \cdots + x_{k+1} = n.$

  • $x_1, ~x_2, ~\cdots, ~x_{k+1} \in \{0,1,2\}.$

  • For $~i \in \{2, ~\cdots, ~k-1\},~$ you can not have
    $x_i = 0 = x_{i+1}.$

The purpose of this answer is to extend lulu's analysis to provide a general closed form formula, in terms of $~n~$ and $~k,~$ for the desired enumeration.

If you ignore the third bullet point above, then you could simply follow the model in this answer.


Because of the third bullet point, a special strategy will be needed.

Assume that of the $~k - 1~$ inner variables, that are represented by $~x_2, ~x_3, ~\cdots, x_k,~$ that exactly $~r~$ of them are forced to equal $~0,~$ and that the other $~(k - 1 - r)~$ of these variables are forced to each be $~\geq 1.$

Then, the lower bound on $~r~$ is $~\max( ~0, ~k - 1 - n ~),~$ and the upper bound on $~r~$ is $~\displaystyle \left\lfloor \frac{(k-1) + 1}{2}\right\rfloor = \left\lfloor\frac{k}{2}\right\rfloor.$ The upper bound reflects that you must avoid having two consecutive inner variables that are both equal to $~0.$

Let $~r~$ be any element in the appropriate range, and let $~f(n,k,r)~$ denote the number of permitted ways of having exactly $~r~$ of the $~(k-1)~$ inner variables equal to $~0.~$

Then, let $~g(n,k,r)~$ denote the enumeration of the number of solutions to

  • $x_1 + x_2 + \cdots + x_{k+1} = n.$

  • $x_1, ~x_{k+1} \in \{0,1,2\}.$

  • $x_2, ~\cdots, ~x_{r+1} = 0.$

  • $x_{r+2}, ~\cdots, ~x_k \in \{1,2\}.$

Then, the overall computation will be:

$$\sum_{r ~\text{in range}} [ ~f(n,k,r) \times g(n,k,r) ~].$$


When the lower bound for $~r~$ is $~0,~$ then for $~r \in \{0,1\},~$ you have that $~f(n,k,r) = \displaystyle \binom{k-1}{r}.~$

In the remainder of this section, it is assumed that $~r~$ is in range, and that $~r \geq 2.$

To visualize the computation of $~f(n,k,r),~$ temporarily assume that $~n~$ is large enough, and that $~k = 8, ~r = 3,~$ and consider the following tableau:

x2 x3=0 x4 x5=0 x6 x7=0 x8

In the above tableau, since $~k = 8,~$ the inner variables are represented by $~x_2, ~x_3, ~\cdots, ~x_8.~$ So, you have a tableau with $~7~$ positions. The $~3~$ variables set to $~0~$ create $~4~$ islands. Reading the size of these islands from left to right, you can let $~y_1, ~y_2, y_3, ~y_4~$ denote the size of these islands.

In the above tableau, since you are starting with $~7~$ positions, and since you have $~3~$ of these positions taken by the zero-variables of $~x_3, x_5, x_7,~$ you have that $~y_1 + y_2 + y_3 + y_4 = 7 - 3 = 4.$

Further, the placement of the $~3~$ zero variables will be satisfactory if and only if the variables $~y_2, y_3~$ are both greater than $~0.~$

Therefore, for $~r~$ in range, with $~r \geq 2, ~~f(n,k,r)~$ equals the enumeration of the number of solutions to

  • $y_1 + \cdots + y_{r+1} = k - 1 - r.$

  • $y_1, ~y_{r+1} \in \Bbb{Z_{\geq 0}}.$

  • $y_2, ~\cdots, ~y_r \in \Bbb{Z_{\geq 1}}.$

By basic Stars and Bars theory, for $~r~$ in range and $~r \geq 2:$

$$f(n,k,r) = \binom{[k - 1 - r] - [r - 1] + r}{r} = \binom{k-r}{r}.$$

In the present problem, with $~n = 5, ~k = 6,~$ you have that $~r \in \{0,1,2,3\}.~$ Then, $~f(5,6,0) = 1, ~f(5,6,1) = 5,~$ and for $~r \geq 2,~$ $~f(5,6,r) = \displaystyle \binom{6-r}{r}.$


Assuming that $~r~$ is in range, to compute $~g(n,k,r)~$ the original problem of

  • $x_1 + x_2 + \cdots + x_{k+1} = n.$

  • $x_1, ~x_{k+1} \in \{0,1,2\}.$

  • $x_2, ~\cdots, ~x_{r+1} = 0.$

  • $x_{r+2}, ~\cdots, ~x_k \in \{1,2\}.$

should be adjusted by eliminating the variables forced to equal $~0,~$ and re-indexing the remaining variables. With this adjustment, the enumeration problem becomes

  • $x_1 + x_2 + \cdots + x_{k+1-r} = n.$

  • $x_1, ~x_{k+1-r} \in \{0,1,2\}.$

  • $x_{2}, ~\cdots, ~x_{k-r} \in \{1,2\}.$

The lower bounds of the variables are changed to $~0~$ by further changing the enumeration to

  • $x_1 + x_2 + \cdots + x_{k+1-r} = n - (k-r-1) = (n + 1 + r - k).$

  • $x_1, ~x_{k+1-r} \in \{0,1,2\}.$

  • $x_{2}, ~\cdots, ~x_{k-r} \in \{0,1\}.$

Then, to compute $~g(n,k,r)~$ you can use the methods given in this answer.

In the present problem, with $~n = 5, ~k = 6,~$ you have that $~g(5,6,r)~$ equals the number of solutions to:

  • $x_1 + x_2 + \cdots + x_{7-r} = r.$

  • $x_1, ~x_{7-r} \in \{0,1,2\}.$

  • $x_{2}, ~\cdots, ~x_{6-r} \in \{0,1\}.$

So, $~g(5,6,0) = 1, ~g(5,6,1) = 6, ~g(5,6,2) = 12, ~g(5,6,3) = 10.$

Therefore, the final enumeration is

$$\sum_{r=0}^3 f(5,6,r) \times g(5,6,r)$$

$$= [ ~1 \times 1 ~] + [ ~5 \times 6 ~] + [ ~6 \times 12 ~] + [ ~1 \times ~10] = 113.$$

user2661923
  • 42,303
  • 3
  • 21
  • 46