How many arrangements of $\{a,2b,3c,4d, 5e\}$ have no identical consecutive letters?

Solution 1:

This is an alternative approach to compute this numbers using a computer. The set of all words we are trying to count is of course finite, to it is a regular language. Luckily, it is easy to construct an automaton which recognizes it, so we can look at the adjacency matrix of the machine and compute its powers. The obvious automaton has $(2+n)n!$ vertices, which is pretty huge.

The actual computation of the matrix powers takes time similar to $$\log\binom{n+1}{2}(n+2)^3n!^3$$ if one uses repeated squaring.

Can someone get an asymtotics idea of the actual result we are computing to compare this algorithm with something like Jiri's (which obviously taes time linear in the result) ?

In mathematica, we can code this with

vertices[n_] := 
 Join[
  {start, bottom},
  Flatten[
   Table[st[a, is], {a, 1, n},  {is, 
     Tuples[Table[Range[0, i], {i, 1, n}]]}
    ], 1]
  ]

rules[n_] := 
 Join[
  Table[{start, i} -> st[i, MapAt[# - 1 &, Range[n], i]], {i, 
    1, n}],
  Table[{bottom, i} -> bottom, {i, 1, n}],
  Flatten[Table[
    {st[a, is], b} -> 
     If[a == b || is[[b]] == 0, 
      bottom, st[b, MapAt[# - 1 &, is, b]]],
    {a, 1, n}, {b, 1, n}, {is, Tuples[Table[Range[0, i], {i, 1, n}]]}
    ], 2]
  ]

toMatrix[rs_, n_, vs_] := 
 SparseArray[
  Flatten@Table[
    With[{src = Position[vs, v][[1, 1]], 
      dest = Position[vs, {v, i} /. rs][[1, 1]]},
     {src, dest} -> If[src === bottom || dest === bottom, 0, 1]
     ], {v, vs}, {i, n}] ,
  {Length[vs], Length[vs]}
  ]

go[n_] := Module[{vs = vertices[n], a, m},
  a = Position[vs, start][[1, 1]];
  Print[n, ": Matrix size : ", Length[vs]];
  m = MatrixPower[
    Transpose@toMatrix[rules[n], n, vs], 
    Binomial[n + 1, 2],
    SparseArray[{a -> 1}, {Length[vs]}]
    ];
  Total[m[[Position[vs, st[_, {0 ..}]][[All, 1]]]]]
  ]

Using this code, I can evaluate

go /@ Range[1..5]

to get

1, 1, 10, 1074, 1637124

in 57 seconds.

Solution 2:

I do not have any explicit formula (this could be hard), but invested some time into an enumeration program. This is a nice exercise in backtracking. The heart of the algorithm is the recursive procedure generate():

void generate(int* avail, int N, int* a, int pos) {
    // avail[i]: number of available letters i (i = 0, 1, .. N-1)
    // a: generated arrangement a[0], a[1], ... a[n-1] (small n, n = N(N+1)/2)
    // pos: currently generated position in the arrangement a

    int n = N * (N + 1) / 2; // length of the generated sequence
    if (pos >= n) {
        visit(a, n); // one final arrangement, break recursion
        return;
    }
    for (int k = 0; k < N; ++k) {
        if (avail[k] > 0) { // skip if no available letters
            if (pos == 0 || a[pos-1] != k) { // skip equal neighbors
                a[pos] = k; // fill position pos with letter k
                avail[k] -= 1; // consume the letter k
                generate(avail, N, a, pos + 1); // recurse at next position
                avail[k] += 1; // put the used letter back
            }
        }
    }
}

I provide a general complete running program in C++, for all arrangements {1*a, 2*b, 3*c, ... N*letter[N]} (well, until overflow occurs - this could be avoided by porting to Java and using BigInteger).

Number of arrangements: 
1*a : 1
1*a 2*b : 1
1*a 2*b 3*c : 10
1*a 2*b 3*c 4*d : 1074
1*a 2*b 3*c 4*d 5*e : 1637124

The results agree with those on OEIS.