Problem
Consider a 2x12 rectangle with dotted lines marking the 24 squares. How many sets R are there such that R is connected, R is obtained by cutting on the dotted lines, and R contains at least one square at the left end and at least one square at the right end?
βConnectedβ means that one can get from any square in R to any other by a path of adjacent squares, adjacency meaning that at least one edge is shared. Symmetry is ignored: if R is congruent to Rβ but they involve different squares, they count as different. There is in fact a formula for the count for the case (and some extensions to ). Here is an example of a set R.
For a rectangle there are seven such sets, shown below.
Source: S. Durham and T. Richmond, Connected subsets of an nx2 rectangle, College Math. J., 51, (Jan. 2020), 32-42.
Solution
Let be the number of sets as in the problem for 2xn grid. Let be the number of sets as in the problem, but that have in the rightmost column a square on the top but not the bottom. Note that this is the same, by symmetry, if we count sets that have a square on the bottom but not the top in the last column. Let be the number of sets as in the problem that have the rightmost column contain both squares. Note that .
We have , so
Also , so
Now the above give us,
Solving the recurrence relation using the initial conditions and gives us the sequence for .
What is interesting about this sequence: is that it is identical with the numerators of the convergents of the continued fraction of :
.
Here is the Python code for calculating and generating the connected paths.
def num_connected_paths(n):
if n == 1:
return 3
if n == 2:
return 7
cnt = 2
t_n_2, t_n_1 = 3, 7
while(cnt != n):
t_n_2, t_n_1 = t_n_1, 2*t_n_1 + t_n_2
cnt += 1
return t_n_1
print(num_connected_paths(5))
def connected_paths(n):
if n == 1:
return [[(1,0)],[(0,1)],[(1,1)]]
ext_paths = []
for path in connected_paths(n-1):
s1, s2 = path[-1]
ext_paths.append(path + [(1,1)])
if (s1,s2) == (1,1):
ext_paths.append(path + [(1,0)])
ext_paths.append(path + [(0,1)])
else:
ext_paths.append(path + [(s1,s2)])
return ext_paths
print(len(connected_paths(5)))