[go: up one dir, main page]

login
Number of (directed) Hamiltonian paths (or Gray codes) on the n-cube.
9

%I #31 Sep 12 2024 14:44:46

%S 2,8,144,91392,187499658240

%N Number of (directed) Hamiltonian paths (or Gray codes) on the n-cube.

%C More precisely, this is the number of ways of making a list of the 2^n nodes of the n-cube, with a distinguished starting position and a direction, such that each node is adjacent to the previous one. The final node may or may not be adjacent to the first.

%D M. Gardner, Knotted Doughnuts and Other Mathematical Entertainments. Freeman, NY, 1986, p. 24.

%H Eric Weisstein's World of Mathematics, <a href="http://mathworld.wolfram.com/HamiltonianPath.html">Hamiltonian Path</a>

%H Eric Weisstein's World of Mathematics, <a href="http://mathworld.wolfram.com/HypercubeGraph.html">Hypercube Graph</a>

%e a(1) = 2: we have 1,2 or 2,1. a(2) = 8: label the nodes 1, 2, ..., 4. Then the 8 possibilities are 1,2,3,4; 1,3,4,2; 2,3,4,1; 2,1,4,3; etc.

%o (Python)

%o # A function that calculates A091299[n] from Janez Brank.

%o def CountGray(n):

%o def Recurse(unused, lastVal, nextSet):

%o count = 0

%o for changedBit in range(0, min(nextSet + 1, n)):

%o newVal = lastVal ^ (1 << changedBit)

%o mask = 1 << newVal

%o if unused & mask:

%o if unused == mask:

%o count += 1

%o else:

%o count += Recurse(

%o unused & ~mask, newVal, max(nextSet, changedBit + 1)

%o )

%o return count

%o count = Recurse((1 << (1 << n)) - 2, 0, 0)

%o for i in range(1, n + 1):

%o count *= 2 * i

%o return max(1, count)

%o [CountGray(n) for n in range(1, 4)]

%Y Equals A006069 + A006070. Divide by 2^n to get A003043.

%Y Cf. A003042, A066037, A091302, A284673.

%K nonn,hard,more

%O 1,1

%A _N. J. A. Sloane_, Feb 20 2004

%E a(5) from Janez Brank (janez.brank(AT)ijs.si), Mar 02 2005