Counting the number of restricted forests on the Möbius ladder of length n

13

OEIS sequence A020872 counts the number of restricted forests on the Möbius ladder Mn.

The Challenge

The challenge is to write a program that takes an integer as an input n > 1 and returns A020872(n), the number of restricted forests on the Möbius ladder Mn. This is , so shortest code wins. (An ulterior motive is to perhaps extend the length of this sequence by a bit.)

Definitions

A restricted forest is a partition of the graph such that each part is either a (undirected) path or an isolated vertex.

The Möbius ladder Mn is a graph which can be thought of the 2n-gon with diagonals drawn between all opposite vertices.

Example

Here are the 34 restricted forests on M2 (a square with diagonals drawn). Notice that the first graph is partitioned into four isolated vertices, the second is partitioned into one path and two isolated vertices, etc. A020872(2)

Peter Kagey

Posted 2019-03-26T01:23:48.297

Reputation: 2 789

1Test cases from 2 to 12: 34, 241, 1582, 10204, 65197, 415076, 2638366, 16759249, 106427154, 675771276, 4290678337. I'm not sure why input 1 isn't also required, with output 2. – Peter Taylor – 2019-03-26T13:43:32.857

@PeterTaylor, Thanks for adding those terms to OEIS! I excluded input 1 because M_1 isn't clearly defined in the Wikipedia article. (In particular, either it has multiple edges or it is not a cubic graph.) – Peter Kagey – 2019-03-26T19:52:24.277

1This actually sounds like a good candidate for a fastest-code or fastest-algorithm challenge. – mypetlion – 2019-03-26T22:16:25.227

1

Further test cases (generation code): 13 to 17 are 27242281044, 172964658642, 1098170541121, 6972388689086, 44268329738124

– Peter Taylor – 2019-03-27T12:01:08.343

1Right, I think your ulterior motive is more than satisfied. – Peter Taylor – 2019-03-30T10:12:57.923

Answers

10

CJam (58 56 chars)

Some characters unprintable, and one is a tab which will be mangled by the StackExchange software:

"¶3¬î¿Á·    7ÛÈmÈÚÚ¡"256b454b212f-{__W%.*A<1b+}qi*-4=

Online demo. This will run online for n=400 in about three seconds.

Encoded by xxd:

0000000: 22b6 0233 93ac eebf c1b7 0609 3794 dbc8  "..3........7...
0000010: 6dc8 1015 dada a122 3235 3662 3435 3462  m......"256b454b
0000020: 3231 3266 2d7b 5f5f 5725 2e2a 413c 3162  212f-{__W%.*A<1b
0000030: 2b7d 7169 2a2d 343d                      +}qi*-4=

Explanation

A Möbius ladder is basically a ladder with two extra edges. Given a restricted forest on a ladder, it can be lifted to between 1 and 4 restricted forests on the Möbius ladder. The edges can be added provided that doesn't create a vertex of degree 3 or a cycle. The degrees of the four corners and their interconnections form 116 classes of restricted forest on the ladder, although some of them are equivalent due to symmetries of the rectangle. I wrote a program to analyse the extensions of a ladder of length n to one of length n+1, and then merged the classes into 26 equivalence classes. This gives a closed form

$$\begin{bmatrix} 1 \\ 1 \\ 1 \\ 1\end{bmatrix}^T \begin{bmatrix} 1 & 2 & 2 & 0 \\ 1 & 2 & 1 & 1 \\ 2 & 3 & 4 & 1 \\ 0 & 0 & 1 & 0 \\ \end{bmatrix}^{n-2} \begin{bmatrix} 0 \\ 1 \\ 0 \\ 0\end{bmatrix} + $$

$$\begin{bmatrix} 2 \\ 2 \\ 1 \\ 1 \\ 1 \\ 1 \\ 1 \\ 2 \\ 2\end{bmatrix}^T \begin{bmatrix} 2 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & 0 & 1 & 0 & 0 & 1 & 0 & 1 & 0 \\ 0 & 0 & 2 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 \\ 1 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 1 \\ 3 & 2 & 2 & 1 & 1 & 2 & 1 & 4 & 2 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 2 \\ \end{bmatrix}^{n-2} \begin{bmatrix} 0 \\ 0 \\ 2 \\ 2 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0\end{bmatrix} + $$

$$\begin{bmatrix} 1 \\ 2 \\ 4 \\ 4 \\ 1 \\ 1 \\ 3 \\ 2 \\ 2 \\ 2 \\ 3 \\ 4 \\ 4\end{bmatrix}^T \begin{bmatrix} 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 2 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 2 & 0 & 1 & 1 & 0 & 1 & 1 & 0 & 1 & 1 & 1 \\ 1 & 0 & 0 & 4 & 0 & 0 & 3 & 0 & 0 & 2 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 2 & 1 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\ 1 & 0 & 0 & 1 & 0 & 0 & 2 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 1 & 2 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 1 & 2 \\ 0 & 1 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 2 & 0 & 0 & 2 & 0 & 0 & 1 & 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 & 2 & 2 & 0 \\ 0 & 2 & 3 & 0 & 1 & 1 & 0 & 2 & 1 & 0 & 1 & 2 & 4 \\ \end{bmatrix}^{n-2} \begin{bmatrix} 1 \\ 0 \\ 1 \\ 1 \\ 2 \\ 0 \\ 1 \\ 0 \\ 0 \\ 0 \\ 1 \\ 2 \\ 1\end{bmatrix}$$

so values can be computed fast by taking three linear recurrences and then adding them, but this isn't looking very golfy.

However, if we take the irreducible factors of the various characteristic polynomials and multiply together one of each (ignoring multiplicity) we get a polynomial of degree 10 which gives a working single linear recurrence.

Constructive approach (58 chars)

qi:Q2*,Wa*e!{Wa/{_W%e<}%$}%_&{{,1>},2few:~{:-z(Q(%}%0-!},,

Online demo. It will run online for n=2 without problems and for n=3 with a bit of patience. For n=1 it crashes, but since OP has chosen to exclude that case from the requirements it's not a fundamental problem.

Dissection

qi:Q          e# Take input from stdin, parse to int, store in Q
2*,Wa*e!      e# Take all permutations of (0, -1, 1, -1, 2, -1, ..., -1, 2*Q-1)
{             e# Map to canonical form...
  Wa/         e#   Split around the -1s
  {_W%e<}%    e#   Reverse paths where necessary to get a canonical form
  $           e#   Sort paths
}%
_&            e# Filter to distinct path sets
{             e# Filter to path sets with valid paths:
  {,1>},      e#   Ignore paths with fewer than two elements (can't be invalid; break 2ew)
  2few:~      e#   Break paths into their edges
  {:-z(Q(%}%  e#   The difference between the endpoints of an edge should be +/-1 or Q (mod 2Q)
              e#   So their absolute values should be 1, Q, 2Q-1.
              e#   d => (abs(d)-1) % (Q-1) maps those differences, and no other possible ones, to 0
              e#   NB {:-zQ(%}% to map them all to 1 would save a byte, but wouldn't work for Q=2
  0-!         e#   Test that all values obtained are 0
},
,             e# Count the filtered distinct path sets

A more efficient version takes 98 bytes:

qi2*:Q{a{__0=[1Q2/Q(]f+Qf%_&1$-\f{+E}~}:E~}/]{_W%>!},:MW=0{_{M\f{__3$_@&@:e<@|^{=}{^j}?}1b}{,)}?}j

Online demo

This builds the possible paths by depth-first search, then uses a memoised function which counts the possible restricted forests for a given set of vertices. The function works recursively on the basis that any restricted forest for a given non-empty set of vertices consists of a path containing the smallest vertex and a restricted forest covering the vertices not in that path.

Peter Taylor

Posted 2019-03-26T01:23:48.297

Reputation: 41 901

On the grid graph, this can be described with a linear recursion, so it wouldn’t surprise me to find out that this is nice. – Peter Kagey – 2019-03-28T05:47:30.293

6

JavaScript (ES6),  160 158  146 bytes

n=>(g=(e,v,p)=>[...Array(N=2*n),N-1,1,n].reduce((s,x,i)=>(m=1<<(x=i<N?i:(p+x)%N))&v?s:s+g((i>=N)/p?[...e,1<<p|m]:e,v|m,x),g[e.sort()]^(g[e]=1)))``

Try it online!

Notes:

  • This is quite inefficient and will time-out on TIO for \$n>4\$.
  • \$a(5) = 10204\$ was found in a bit less than 3 minutes on my laptop.

Commented

n => (                        // n = input
  g = (                       // g = recursive function taking:
    e,                        //   e[] = array holding visited edges
    v,                        //   v   = bitmask holding visited vertices
    p                         //   p   = previous vertex
  ) =>                        // we iterate over an array of N + 3 entries, where N = 2n:
    [ ...Array(N = 2 * n),    //   - 0...N-1: each vertex of the N-gon (starting points)
      N - 1,                  //   - N      : previous vertex \
      1,                      //   - N+1    : next vertex      }-- connected to p
      n                       //   - N+2    : opposite vertex /
    ].reduce((s, x, i) =>     // reduce() loop with s = accumulator, x = vertex, i = index:
      ( m = 1 << (            //   m is a bitmask where only the x-th bit is set
          x = i < N           //   and x is either:
              ? i             //   - i if i < N
              : (p + x) % N   //   - or (p + x) mod N otherwise
      )) & v ?                //   if this vertex was already visited:
        s                     //     leave s unchanged
      :                       //   else:
        s +                   //     add to s
        g(                    //     the result of a recursive call:
          (i >= N) / p ?      //       if p and x are connected (i >= N and p is defined):
            [ ...e,           //         append to e[]:
              1 << p | m      //           the edge formed by p and x
            ]                 //           and uniquely identified by 1 << p | 1 << x
          :                   //       else:
            e,                //         leave e[] unchanged
          v | m,              //       mark the vertex x as visited
          x                   //       previous vertex = x
        ),                    //     end of recursive call
      g[e.sort()] ^           //   sort the edges and yield 1 if this list of edges has not
      (g[e] = 1)              //   already been encountered; either way, save it in g
    )                         // end of reduce()
)``                           // initial call to g with e = ['']

Arnauld

Posted 2019-03-26T01:23:48.297

Reputation: 111 334

2

Jelly, 61 58 bytes

®R,³;Ø+
Ḥ©Ḷµ1ị+¢%®ḟ€;€€1¦-Ẏ;€)Ẏ$ƬẎṣ€-Ẉ’ẠƊƇU¹Ø.ị>/Ɗ?€€Ṣ€QL‘

Try it online!

This is the shorter version; it is optimised for shorter length versus algorithmic complexity and speed.

Jelly, 85 bytes

%®ḟ
1ị+³;Ø+¤ç,1ị+®_3¤R‘¤Ʋç;€-Ʋ“”2ị>-Ʋ?Ẏ;€
Ḥ©Ḷ;€-Ç€Ẏ$ƬẎṣ€-Ẉ=1ẸƊÐḟU¹1ị>0ị$Ʋ?€€Ṣ€QL‘+=2$

Try it online!

Here’s a longer version that adds extra code to avoid trying redundant paths. The check for n=2 at the end is to cope with the specific case for n=2 which looks like a red/blue cross in the example and isn’t generated by this code. This second version completed n=4 in less than 13 seconds on TIO, but times out for higher numbers.

Nick Kennedy

Posted 2019-03-26T01:23:48.297

Reputation: 11 829