# Solve recurrence relation that depends on depth of recursion

The specific problem I'm working on is the puzzle presented in this video. For those who don't want to watch the video, my summary of the puzzle is:

A frog is sitting on the edge of a pond facing the opposite bank. In front of the frog, running along the pond's diameter, is a line of $$N$$ lilypads. This is a very athletic frog: it is able in a single leap to land on any of the $$N$$ lilypads in the pond or on the opposite bank (for a total of $$N + 1$$ possible destinations). The frog first considers all $$N+1$$ positions in front of it, chooses one randomly with uniform probability, and then jumps to this location. It then repeats the process of choosing a new random position from those remaining in front of it (never jumping backwards). This goes on until the frog eventually reaches the opposite bank.

The puzzle is twofold: First, if $$N=10$$, what is the expectation value of the number of jumps the frog will take to reach the other side of the pond? Second, find an explicit formula (NOT a recurrence relation!) for the expectation value of the number of jumps in general for $$N$$ lilypads.

(It's a great puzzle; if you haven't yet given it a shot, I'd highly recommend doing so before reading any possible spoilers in the rest of this post!).

To attempt this puzzle, I first calculated the first few terms by hand and then tried to generalize my result. I eventually came up with a recursive algorithm for the expectation value $$f$$:

def f(n, depth=1):
if n == 0:
return depth
else:
return (n * f(n - 1, depth) + f(n - 1, depth + 1)) / (n + 1)


However, the second part of the puzzle explicitly disallows recurrence relations as solutions, so I am trying to un-roll/solve the recurrence relation. There are already several questions on strategies for solving recurrence relations (see this and this), but none I can find on solving recurrence relations which are dependent on the depth of recursion. Any help on how to do this is appreciated.

• We denote the expected number of jumps by $A_n$. Try the following approach. Express $A_{n+1}$ through $A_0, A_1, \ldots, A_{n}$. Then, using the similar expression for $A_n$ through $A_0, A_1, \ldots, A_{n-1}$, derive a formula for $A_{n+1}$ depending only on $A_n$. The rest of the problem then becomes straightforward. – diplodoc Sep 25 '19 at 21:13
• @diplodoc That is the approach I tried, and is how I arrived at my recurrence relation. The problem I ran into is that the value of $A_n$ depends on $A_{n-1},\cdots,A_i,\cdots,A_0$ but each $A_i$ also depends on the depth $n - i$. In my attempts I was not able to eliminate the depth dependence. – fezziks Sep 26 '19 at 17:54
• $A_{n} = 1 + \frac{1}{n+1}\sum_{i=0}^{n-1}A_i$, then $(n+1)(A_n-1) = \sum_{i=0}^{n-1}A_i$. You can now exclude the sum from the expression for $A_{n+1}$ and obtain $A_{n+1} = A_n+\frac{1}{n+2}$ – diplodoc Sep 26 '19 at 18:24

diplodoc explained in the comments one way to solve the recursion. Your recursion is $$A_n = 1 + \frac{A_0 + \cdots + A_{n-1}}{n+1}, \quad A_0 = 1.$$ The trick is to calculate $$(n+1)A_n - n A_{n-1} = (n+1 + A_0 + \cdots + A_{n-1}) - (n + A_0 + \cdots + A_{n-2}) = 1 + A_{n-1},$$ and so $$A_n = A_{n-1} + 1/(n+1)$$. From here it follows easily that $$A_n$$ is the $$(n+1)$$th harmonic number.

Another possibility is to use generating functions. Let $$P(x) = \sum_{n=0}^\infty A_n x^n.$$ Since $$\frac{1}{1-x} = 1 + x + x^2 + \cdots$$, it follows that $$\frac{xP(x)}{1-x} = \sum_{n=0}^\infty (A_0 + \cdots + A_n) x^{n+1}.$$ Integrating, we get $$\int_0^x \frac{yP(y)}{1-y} \, dy = \sum_{n=0}^\infty \frac{A_0 + \cdots + A_n}{n+2} x^{n+1}.$$ It follows that $$\frac{1}{1-x} + \frac{1}{x} \int_0^x \frac{yP(y)}{1-y} \, dy = P(x).$$ In terms of $$Q(x) = \int_0^x \frac{yP(y)}{1-y} \, dy$$, this reads $$\frac{1}{1-x} + \frac{Q(x)}{x} = \frac{(1-x)Q'(x)}{x},$$ which simplifies to $$\frac{x}{1-x} + Q(x) = (1-x)Q'(x) = [(1-x)Q(x)]' + Q(x),$$ and so $$(1-x)Q(x) = \int^y \frac{y}{1-y} \, dy = \int^y \left(\frac{1}{1-y}-1\right) \, dy = C - x - \log(1-x).$$ It follows that $$Q(x) = \frac{C-x-\log(1-x)}{1-x}.$$ Since $$Q(0) = 0$$, we deduce $$C = 0$$. Differentiating and multiplying by $$(1-x)/x$$, we find that $$P(x) = \frac{-\log(1-x)}{x(1-x)} = \frac{\sum_{n=0}^\infty x^n/(n+1)}{1-x},$$ from which we can extract the coefficient of $$P(x)$$ to be $$H_{n+1}$$.

Finally, we can solve this without using any recurrences. Consider a random permutation of the numbers $$1,\ldots,n+1$$, which we think of as the $$n$$ places and the goal. The frog jumps to the first number. If it is not $$n+1$$, it scans the list to the first larger number, and jumps there. Repeat in this way, until you get to $$n+1$$. You can check that this defines the same random process.

Given a permutation, the number of jumps is the number of left-to-right maxima, that is, elements which are larger than all that precedes them. The probability that the $$i$$th number is a left-to-right maximum is exactly $$1/i$$, and so linearity of expectation shows that the expected number of jumps is $$1 + 1/2 + \cdots + 1/(n+1)$$.

As a bonus, the last solution allows us to calculate higher moments of the distribution. The main observation is that the indicator variables $$X_i$$ for the $$i$$th number being a left-to-right maxima are independent Bernoulli random variables. This implies that the variance of the number of left-to-right maxima is $$\sum_{i=1}^{n+1} \frac{1}{i} \left(1-\frac{1}{i}\right) = H_{n+1} - \sum_{i=1}^{n+1} \frac{1}{i^2} = H_{n+1} - \frac{\pi^2}{6} + O\left(\frac{1}{n}\right).$$

One checks that the central limit theorem applies, and so the total number of left-to-right maxima is asymptotically normal.

Why is the answer roughly $$\log n$$? One might expect the answer to be closer to $$\log_2 n$$, since at each step, $$n$$ is roughly cut in half. However, this argument assumes that the changes to $$n$$ are additive, whereas in reality they are multiplicative. It is better to consider instead the effect of each step on $$\log n$$. If $$m$$ is the new value of $$n$$, then the expected value of $$\log m$$ is $$E[\log m] = \frac{\log 1 + \cdots + \log n}{n+1} \approx \frac{1}{n}\int_0^n \log x \, dx = \left.\frac{1}{n} (x\log x-x)\right|_0^n = \log n - 1.$$ Therefore the value of $$\log n$$ decreases by roughly 1 each step, and so we expect the number of steps to be around $$\log n$$ rather than $$\log_2 n$$.

(With some work, this intuition can be made rigorous.)

• Thanks for the answer! You have presented several solutions to the puzzle; however, each one appears to hinge on an ansatz that I am unable to connect in a direct way to the puzzle description. Could you add some more explanation as to how you arrived at the recurrence relation and the generating functions you use in your solutions? – fezziks Sep 30 '19 at 14:08
• There are algorithms for solving such recurrences, implemented in computer algebra systems. It’s a rather broad topic. You can check the books generatingfunctionology and A=B. – Yuval Filmus Sep 30 '19 at 14:10