Monday, December 31, 2018

Two Recurrences for Counting Rooted Trees

A rooted tree is a tree with a special node called the root. The number of rooted trees with \(n\) nodes is given by the sequence A000081 in the OEIS. They provide the following recurrence relation: \[ a_{n+1} = \frac{1}{n} \sum_{k=1}^n \left( \sum_{d \mid k} d \cdot a_d \right) a_{n-k+1} \] I came up with a different one. I was surprised that they can produce the same sequence despite appearing so different. \[ a_{n+1} = \sum_{\left( p_1^{r_1} \, \ldots \, p_\ell^{r_\ell} \right) \, \vdash \, n} \left( \prod_{i=1}^{\ell} \left(\!\!\binom{a_{p_i}}{r_i}\!\!\right) \right) \]

The sum is over all partitions of \(n\), and \( \left( p_1^{r_1} \ldots p_\ell^{r_\ell} \right) \) is the frequency representation of a partition. \( \left(\!\binom{n}{k}\!\right) \) is the multiset coefficient. It represents the number of ways to choose \(k\) elements out of \(n\) allowing repetition, and is equal to \( \binom{n+k-1}{k} \).

The image below shows how the second formula can be derived with an example for \(a_{25}\). Every node that is directly attached to the root is the root of a subtree, and the subtrees are arranged in a partition. Changing the partition changes the tree. You can switch out subtrees for another subtree with the same number of nodes, to get a new tree without changing the partition.

Using the program below I have verified that both recurrence relations are correct up to at least the \(30^\text{th}\) term.

Sunday, December 30, 2018

The Binary Look-and-Say Sequence

I ran across an interesting math problem here. I've taken the solution posted by Mark Hennings and filled it out with more details.

Problem

Consider the binary look-and-say sequence:

\[ 1, 11, 101, 111011, 11110101, 100110111011, \ldots \]

Each term of the sequence can be generated from the previous term by reading off the digits, always saying how many times the same digit appears consecutively (but converting this number to binary). For example, the fourth term is "three 1s, one 0, two 1s". Three is 11 in binary and two is 10. So the fifth term is 11 1, 1 0, 10 1, or 11110101.

Let \(f(n)\) denote the number of times the string "100" appears in the \(n^\text{th}\) term. Then \(f(8) = 2\), for example, because the \(8^\text{th}\) term equals 111100111010110100110111011.

What is \(\displaystyle\lim_{n\to\infty}\frac{f(n)}{f(n-1)}\)?

Step 1. Express \(f(n)\) as a matrix equation

The terms in this sequence can all be expressed as collections of 10 specific strings. The table below details the strings, and what each string contributes to the next term. This transformation can also be expressed by the matrix \(M\). If \(v(n)\) is a column vector representing the number of times each string occurs in the \(n^\text{th}\) term of the sequence, then \(v(n+1) = M v(n)\).

\[ \begin{array}{ccc} \hline \mathrm{Label} & \mathrm{String} & \mathrm{Becomes} \\ \hline A & 1 & B \\ B & 11 & CA\\ C & 10 & E \\ D & 110 & CD \\ E & 1110 & F \\ F & 11110 & GD \\ G & 100 & I \\ H & 1100 & CH \\ I & 11100 & J \\ J & 111100 & GH \\ \hline \end{array} \qquad M = \left(\begin{array}{cccccccccc} 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ \end{array}\right) \]

It follows that:

\[f(n) = \left( \begin{array}{cccccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \end{array}\right) M^{n-1} v(1)\]

Step 2. Simplify the expression for \(f(n)\)

In order to easily compute powers of \(M\), we can put it in Jordan normal form. First, the characteristic polynomial of \(M\) is \( \chi_M(t) = t^4(t-1)^2(t+1)(t^3 - t^2 - 1) \). From here we can calculate the Jordan normal form \(J\) and transition matrix \(P\). The numbers \(x\), \(y\), and \(z\) are the roots of \(t^3-t^2-1\).

\[ J = \left(\begin{array}{cccccccccc} 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & x & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & y & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & z \end{array}\right) \] \[ P = \left(\begin{array}{cccccccccc} 0 & 0 & 0 & 0 & 0 & 1 & 3 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & -3 & 0 & 0 & 0 \\ 0 & 0 & -1 & 1 & 0 & 0 & 4 & x^2 & y^2 & z^2 \\ 1 & 0 & 0 & 0 & 1 & 0 & -2 & x^2 & y^2 & z^2 \\ 0 & -1 & 1 & 0 & 0 & 0 & -4 & x & y & z \\ -1 & 1 & 0 & 0 & 0 & 0 & 4 & 1 & 1 & 1 \\ 0 & 0 & 1 & -1 & 0 & 0 & -2 & 1 & 1 & 1 \\ -1 & 0 & 0 & -1 & -1 & -1 & 1 & 1 & 1 & 1 \\ 0 & 1 & -1 & 0 & 0 & 0 & 2 & 1/x & 1/y & 1/z \\ 1 & -1 & 0 & 1 & 0 & 0 & -2 & 1/x^2 & 1/y^2 & 1/z^2 \end{array}\right) \]

Substitute \(M=P J P^{-1}\) into the equation for \(f(n)\). Since \(P^{-1}\) is multiplied by \(v(1)\), we only need to find its first column, which we'll call \(p\). We can find it by using Gaussian elimination on the equation \(P p = e_1\), where \(e_1\) is the first column of the identity matrix. It will be necessary to make extensive use of identities like the ones below, which hold for any root \(r\) or any three distinct roots \(a\), \(b\), \(c\) of \(t^3-t^2-1\).

We can almost get away with using dummy variables for the entries of \(p\), which would save a lot of work, but I think it's necessary for later to show that \(p_8 \neq 0\). Besides, it's kind of neat to have a more explicit expression for \(f\).

\[ p = \left(\begin{array}{c} 0 \\ 0 \\ 0 \\ -1 \\ 0 \\ 1/2 \\ 1/6 \\ \frac{(x-1)(y+1)(z+1)}{3(x-y)(x-z)} \\ \frac{(x+1)(y-1)(z+1)}{3(y-x)(y-z)} \\ \frac{(x+1)(y+1)(z-1)}{3(z-x)(z-y)} \end{array}\right) \qquad\qquad\begin{array}{c} \hline \mathrm{Identities} \\ \hline r^3-r^2=1 \\ r-1=1/r^2 \\ abc=1 \\ a+b+c=1 \\ ab=c^2-c \\ a^2b^2=c-1 \\ \hline \end{array} \]

Multiplying everything out, and again making use of the identities, we find:

\begin{multline*} f(n) = -\frac{1}{2} + \frac{1}{6} (-1)^{n} + p_8 x^{n+2} + p_9 y^{n+2} + p_{10} z^{n+2}\qquad\text{if}\quad n > 1 \end{multline*}

Step 3. Find the limit

Now, find the value of \(\lim_{n\to\infty} \frac{f(n)}{f(n-1)}\). Let \(g(n)\) consist of all terms of \(f(n)\) that do not contain \(x\), assuming WLOG that \(x\) is the real root. Since the powers of \(y\) and \(z\) converge to 0, and \(-\frac{1}{2} + \frac{1}{6} (-1)^{n}\) is bounded, \(g(n)\) can be killed off by dividing by \(x^{n+1}\). Note that \(p_8 \neq 0\).

\[ \lim_{n\to\infty} \frac{f(n)}{f(n-1)} = \lim_{n\to\infty}\frac {-\frac{1}{2} + \frac{1}{6} (-1)^{n} + p_8 x^{n+2} + p_9 y^{n+2} + p_{10} z^{n+2}} {-\frac{1}{2} + \frac{1}{6} (-1)^{n-1} + p_8 x^{n+1} + p_9 y^{n+1} + p_{10} z^{n+1}} \] \[ = \lim_{n\to\infty}\cfrac {\cfrac{g(n)}{x^{n+1}} + p_8 x} {\cfrac{g(n-1)}{x^{n+1}} + p_8} = x \approx \boxed{1.46557} \]

Congratulations! If you would like to celebrate with a video, this is John Conway, known for the Game of Life, talking about a similar problem relating to the decimal version of the look-and-say sequence.

Here is a blog post written by Nathaniel Johnston, where he finds the rate of growth and the ratio of ones to zeros for the binary look-and-say sequence. He mentions that "it follows from standard theory of linear homogeneous recurrence relations that we can ... read off all of the long-term behaviour of the binary look-and-say sequence from the eigenvalues and eigenvectors of [the matrix]."

The closed-form expression for the \(n^\text{th}\) Fibonacci number is \(F_n = (\varphi{}^n - \psi{}^n) / \sqrt{5} \), and \(\lim_{n\to\infty} \frac{F_{n+1}}{F_n} = \varphi\). If you write the recurrence in matrix form, \(\varphi\) and \(\psi\) are the eigenvalues.

My computer can calculate values for \(f\) much more quickly using the expression found above, compared to generating the terms of the sequence and searching for occurrences of the substring "100". It takes more than 20 seconds to calculate the values up to \(n = 40\) the slow way, but less than a millisecond the fast way. In this case, math is more powerful than programming! Here's the code I used to take these measurements:

Tuesday, October 30, 2018

Circles Packed in an Ellipse

This animation1 shows four identical circles packed in an ellipse. The circles are tangent to each other and to the ellipse. There are two circles on the x-axis and two on the y-axis. Let \(\theta\) be the angle between the origin, the center of the lower circle, and the center of one of the circles to the left or the right.2 Now let's find the equations of the circles as \(\theta\) varies over time between \(0\) and \(\frac{\pi}{2}\). Assume the radius is \(1\).

\[ \left( x \pm 2\sin\theta \right)^2 + y^2 = 1 \] \[ x^2 + \left( y \pm 2\cos\theta \right)^2 = 1 \]

To find the equation of the ellipse, we need to know the major and minor axes. Finding the major axis is tricky because it sometimes ends on the perimeter of a circle, but sometimes it extends past it. It turns out we actually need three different equations, for when \(\theta\) is in different intervals. The three equations are represented by different colors in the animation.

\begin{align} x^2 + y^2\sin\theta = \left( 1 + 2\sin\theta \right)^2 \qquad & \text{if } \; 0 \leq \theta \leq \alpha \tag{1} \\ \\ \frac{x^2}{\left(1+2\sin\theta\right)^2}+\frac{y^2}{\left(1+2\cos\theta\right)^2}=1 \qquad & \text{if } \; \alpha < \theta \leq \beta \tag{2} \\ \\ x^2\cos\theta+y^2=\left(1+2\cos\theta\right)^2 \qquad & \text{if } \; \beta < \theta \leq \frac{\pi}{2} \tag{3} \\ \\ & \text{where } \alpha \approx 0.355,\; \beta \approx 1.216 \end{align}

The Major Axis

Let's derive equation \((3)\) (the other two aren't any harder). That means \(\theta\) will be near \(\frac{\pi}{2}\). Start with the general equation for an ellipse. The minor axis \(b\) is vertical and it extends from the origin, along the y-axis to the far perimeter of one of the circles, so \(b = 1+2\cos\theta\). To find the major axis \(a\), let's look at the intersections between the ellipse and one of the lateral circles. Solve both equations for \(y^2\) and set them equal to each other.

\begin{equation*} b^2 \left(1 - \frac{x^2}{a^2} \right) = 1 - \left(x + 2\sin\theta \right)^2 \tag{4} \end{equation*}

The blue lines in the image to the right show the values of \(x\) that solve equation \((4)\), for varying values of \(a\) and fixed \(\theta\). Note that some of the solutions are extraneous (when the blue line doesn't intersect the ellipse). We want to choose \(a\) so that the ellipse is tangent to the circle. At that moment, there is exactly one solution for \(x\) (represented as one blue line).3 The equation is quadratic in \(x\), so let's take a look at the discriminant.

\[ D = 16 (1 + \cos\theta) \left( \frac{\left( 1 + 2\cos\theta \right)^2}{a^2} - \cos\theta \right) \]

Setting this to \(0\), we get \(a = \frac{1+2\cos\theta}{\sqrt{\cos\theta}}\).

Bounds

If you don't use the proper bounds \(\alpha\) and \(\beta\), the ellipse will either detach from the circles unnecessarily (as shown), or intersect the circles non-tangentially. I followed a process similar to finding the major axis. I started by looking for intersections between an ellipse equation and one of the circle equations. At the bounds, the number of intersections changes, and I again used the discriminant. This time the only unknown is \(\theta\), and after a lot of simplification, I ended up with a cubic in terms of \(\sin\theta\). Let \(r\) be the real root of \( 4x^3 + 4x^2 + x - 1 \). Then \(\alpha = \sin^{-1} r\) and \(\beta = \cos^{-1} r\).

I thought it was interesting that the different ellipse curves transition smoothly into one another at the bounds, so I graphed their areas as functions of \(\theta\) (in Cartesian coordinates). Not only do they meet at the bounds, but their derivatives are also the same.


1. Created with Desmos.

2. This work was inspired by this problem, which asks for the minimum area of the ellipse.

3. Without the benefit of the animation, how could I conclude that equation \((4)\) should have exactly one solution at the moment of tangency? What if, at that moment, there are two solutions and one of them is extraneous?

Tuesday, September 25, 2018

Young Tableaux

Problem

1 3 6
2 4 7
5 8 9

How many ways can you arrange the numbers 1 through 9 in a 3-by-3 grid such that the following conditions hold?1

  • Every number is greater than the number directly above it.
  • Every number is greater than the number immediately to the left of it.

Solution

Insert the numbers into the grid one at a time, in order from 1 to 9. Each number has to be placed in an upper-left corner (there may be more than one way to do this). Forgetting about which number is in which box, just look at the shape they make. You will get a diagram like the one below. The number of ways to arrange numbers for each shape is always equal to the sum of the shapes feeding into it. The solution, 42, appears at the bottom of the diagram.2


1. I originally saw this problem posted here.

2. Number-filled grids with these properties are known as standard Young tableaux. The diagram used in the solution appears to be a subset of Young's lattice. Another way to find the solution is by using the Hook length formula.

Friday, August 31, 2018

Kanji Cross-Reference

This is a small program I wrote to easily see what words a kanji is used in, and its readings in the context of those words. It uses WWWJDIC for looking up words and definitions. To use it, you must access this page with HTTP, not HTTPS