Enumerating Hamiltonian Cycles

A dynamic programming method for enumerating hamiltonian cycles in arbitrary graphs is presented. The method is applied to grid graphs, king’s graphs, triangular grids, and three-dimensional grid graphs, and results are obtained for larger cases than previously published. The approach can easily be modified to enumerate hamiltonian paths and other similar structures.


Introduction
A hamiltonian cycle in a graph G is a cycle that visits each vertex exactly once.Hamiltonian cycles have been studied extensively in graph theory [21,Section 7]; popular research topics include existence and enumeration.The problem of determining whether a given graph contains a hamiltonian cycle and the problem of enumerating hamiltonian cycles are known to be NP-complete [3, p. 199] and #P-complete [20] respectively for general graphs.
Given a graph, it can be difficult to obtain a closed form solution to the problem of enumerating hamiltonian cycles.Many of the recent results on enumerating hamiltonian cycles rely on algorithmic approaches instead.Examples of such approaches include [5] and [7] which enumerate hamiltonian cycles in the n-cube (for n 6) and in the n × n grid graph (for n 20) respectively.
The algorithms used in [5,7] are based on dynamic programming, where large computational problems are solved by breaking them down to small reoccurring subproblems.Figure 1: A hamiltonian cycle in the 4 × 4 grid graph Dynamic programming has recently been used to enumerate other combinatorial structures as well, such as paths, latin squares, and perfect matchings [6,10,13].Examples of other methods for enumerating hamiltonian cycles include backtrack search and ZDD trees by Knuth [8,.
We present a method based on dynamic programming to enumerate hamiltonian cycles in arbitrary graphs.We apply the method to grid graphs, king's graphs, triangular grids, and three-dimensional grid graphs, and obtain results for larger cases than previously published.Notable sources of ideas for our method include [5], which considers enumerating hamiltonian cycles in bipartite graphs, and [6], which considers enumerating certain paths in the grid graph.
Some of the progress in computational methods for enumerating hamiltonian cycles can be illustrated conveniently using the the n×n grid graph as an example.Hamiltonian cycles in the n × n grid graph have received a lot of research attention over the years, partly due to applications in physics.The grid graph is the cartesian product of paths and the n × n grid graph is the special case P n × P n .Figure 1 shows a hamiltonian cycle in the 4 × 4 grid graph.
One early paper on enumerating hamiltonian cycles in the n × n grid graph is [16] from 1984, where the authors use a transfer matrix method to obtain the values for n 10.Several articles were published in the following years, in particular on determining exact formulas for n × m grids with fixed small n [19].However, for square grids the next case n = 12 was resolved as late as 2006 [11] along with n = 14.Soon after, in 2007 the cases n 20 were resolved [7].This large jump was made possible not only by improvements in computing hardware, but by using a more advanced dynamic programming method.In 2010 the case n = 22 was resolved [15].Compared with [7], the method used in [15] contains a few modifications that trade running time for memory requirements.Unfortunately, this tradeoff makes the method too slow to be practical for larger cases.In the current work we compute the cases up to n = 26 by introducing several improvements to the method, all of which are generalized to arbitrary graphs.
Our method can be easily modified to solve the existence problem of hamiltonian cycles.It may be particularly useful for graphs which are almost nonhamiltonian in the sense that normal constructive methods do not find a cycle in reasonable time.
the electronic journal of combinatorics 21(4) (2014), #P4.7 In the current work we focus on hamiltonian cycles.It should be noted that there are many structures that are closely related to hamiltonian cycles, and some of the computational methods that work for hamiltonian cycles can be applied to these closely related structures with few modifications.A hamiltonian path in a graph G is a path that visits each vertex exactly once.A bent hamiltonian path is a hamiltonian path in the grid graph that changes direction at each step [12].
The article is organized as follows.In Section 2 we present our method for enumerating hamiltonian cycles.In Section 3 we describe the graphs to which we apply the method, and finally, in Section 4 we show the results for each graph and discuss how to gain confidence in the the correctness of the results.

Enumerating Hamiltonian Cycles
The method presented in the current work builds hamiltonian cycles by extending incomplete cycles one vertex at a time.With this in mind, we begin with some definitions.Let G be a graph and H a hamiltonian cycle in G. Definition 1.For any U ⊂ V (G), the border of U , denoted by B G (U ), is the set of vertices in U that are adjacent to a vertex in V (G) \ U .
The subgraph of H induced by U , denoted by H(U ), is a collection of (possibly trivial) paths, the endpoints of which are in B G (U ).Definition 2. For U ⊂ V (G), a partial hamiltonian cycle on U is a collection of (possibly trivial) paths in the subgraph of G induced by U such that every vertex in U is contained in exactly one path and every endpoint of a path is in B G (U ).One way to complete the partial hamiltonian cycle in Figure 2 is to add the edges between vertices 3 and 7, vertices 7 and 6, vertices 6 and 8, and finally vertices 8 and 5.Note that a partial hamiltonian cycle as defined in Definition 2 is not necessarily a subgraph of any complete hamiltonian cycle.Definition 3.For U ⊂ V (G), a border structure on U is a partition of a subset of B G (U ) such that each part contains one or two vertices.Definition 4. For U ⊂ V (G), S G (U ) is the set of all possible border structures on U .
We denote the set of endpoints of a path p by e(p).If p is a trivial path, e(p) contains just one vertex.Definition 5.For U ⊂ V (G) and a partial hamiltonian cycle C on U , S(C) is the border structure {e(p) : p is a maximal path in C}.
Note that S(C) ∈ S G (U ).Intuitively, S(C) is the border structure that corresponds to C. In Figure 2, we have Similar definitions appear under many different names in the literature concerning dynamic programming algorithms for graphs.For example, [6] uses the terms frontier and frontier state that correspond to border and border structure respectively.Definition 6.For s ∈ S G (U ), the function representation of s is the function f : Definition 7.For s ∈ S G (U ), and π = (π 0 , π 1 , . . ., π m−1 ) the vertices in B G (U ) in some order, and f the function representation of s, the string representation of s ordered by π is the string (π and the string representation of S(C) ordered by, say, (3,4,5,6), is is the set of all a partial hamiltonian cycles on U ∪ v that can be obtained by adding such edges to C that are incident to v and a vertex in U .
Intuitively, N (s, v) is the set of border structures that can be obtained from s by adding edges between v and U .For brevity of definition, in Definition 9 we define the extension only for border structures that have a corresponding partial hamiltonian cycle.It would be possible to define the extension analogously for all border structures, but this is not necessary for our purposes.
In Figure 2 the extension of S(C) with, say, vertex 7, is the multiset where the multiplicity is 1 for both border structures.
The method presented in the current work relies on the following observations.Given a graph G and a vertex v ∈ V (G), the set of all hamiltonian cycles in G can be generated from the set of partial hamiltonian cycles on V (G) \ v by adding edges between v and V (G)\v in all possible ways.These partial hamiltonian cycles can in turn be generated by adding edges to smaller partial hamiltonian cycles.Furthermore, when determining how edges can be added between a vertex and a partial hamiltonian cycle, the internal structure of the cycle is irrelevant and only the border structure matters.These observations lead to the approach shown in Algorithm 1.
In Algorithm 1, we iterate over the vertices of G one by one, and at every point we keep track of all the partial hamiltonian cycles on the iterated vertices.These partial hamiltonian cycles are grouped based on their border structure, in the sense that we do not store the individual partial cycles in memory, we only store their total number for each border structure in the counters C k .
The efficiency of the method depends on how each part is implemented.There are several alternatives for implementing different parts of the method, which are discussed in the following subsections.

Ordering vertices
The order of vertices v i is crucial for the performance of the method.One possible criterion for choosing the order is that the maximum border size should be as small as possible.The motivation behind this criterion is that the running time and memory requirements for one step of the method depend strongly on the size of the corresponding border.Finding such an order of vertices that minimizes the maximum border size is known as determining the vertex separation number, or equivalently pathwidth.Determining the pathwidth is NP-complete for general graphs, but many results exist for specific graph families [2,4,14].Instead of extending partial hamiltonian cycles one vertex at a time, one could extend several vertices at a time.Some examples of this approach include [5,15].Extending several vertices at a time can help reduce the maximum border size.Additionally, it allows the use of symmetries in some cases.The downside is that the running time for the extension step depends strongly on the number of vertices being added at one time.

Storing counters 2.2.1 Data structure
In Algorithm 1, the values of the counters C k need to be stored in memory.As done in [6], we store the values in a vector u, which has the advantage that we need not store representations of border structures.Vector u has size |S k | and the index corresponding to a border structure s is rank(s, S k , <), for some (arbitrary) ordering <.Furthermore, various compression techniques can be used for storing u.
The choice of S k is important from the point of view of running time and memory efficiency.We will discuss how S k can be chosen for various classes of graphs in Section 2.3.
the electronic journal of combinatorics 21(4) (2014), #P4.7 Here we described how the value of counters C k can be stored in a vector.Another alternative would be storing the counters in a hash table that has representations of border structures as keys and the corresponding counters as values.This works reasonably well in many cases.One example of this approach is [7].

Chinese remainder theorem
The memory efficiency of our method can be improved by computing the number of hamiltonian cycles modulo a prime power for several small prime powers, i.e., prime numbers raised to a positive integer power.The algorithm needs to be run several times, once for each prime power.The total number of hamiltonian cycles can then be reconstructed with the Chinese remainder theorem.This results in a tradeoff, as memory requirements will be roughly inversely proportional to running time.A similar approach was used in [7].
Note that we can store the values of counters C k modulo a prime power throughout the algorithm, because the only operations performed on the counters are multiplication and addition.
The least common multiple of prime powers of size at most 256 is approximately 1.7 • 10 109 , which is larger than the number of hamiltonian cycles in the graphs considered in this article, so here only one byte is needed to store the value of a counter.We do not use all prime powers up to 256 for each graph, we only use as many as necessary to accurately reconstruct the total number of hamiltonian cycles.To determine how many prime powers we need for a graph, it is sufficient that the least common multiple of the prime powers is larger than an upper bound for the number of hamiltonian cycles in the graph.

Choosing S k
In Algorithm 1, we use S k as the set of border structures for which we store the values of counters.Recall that in the algorithm the value of S k is any set of border structures such that The choice of S k is important from the point of view of performance and memory efficiency.Since the running time and memory requirements of our method are proportional to |S k |, we would like |S k | to be as small as possible.The only limitations on the choice of S k are that S k should contain H G (U ) and we should be able to compute rank(s, S k , <) for any s ∈ S k , for some ordering <.Those border structures in S k that are not in H G (U ) are unnecessary for enumerating hamiltonian cycles, since they cannot be extended to any complete hamiltonian cycle.Ideally, we would want S k = H G (U ), but H G (U ) is difficult to determine.Instead, we the electronic journal of combinatorics 21(4) (2014), #P4.7 approximate H G (U ) as accurately as we can for each graph family, so that S k is as small as possible while still containing H G (U ).
For general graphs, we can simply choose S k = S G (U ).In the following subsections we show possible choices of S k for planar graphs and grid graphs.

Planar graphs
Consider a planar graph G, with U ⊂ V (G), and C a partial hamiltonian cycle on U .Assume there is a planar embedding of G such that the vertices in B G (U ) are on a line, the vertices in V (G) \ U are above the line, and all other vertices are below the line.This is the case in Figure 2. Now the paths in C cannot cross in this embedding.Let the order of vertices along the line be π = (π 1 , π 2 , . . ., π m ).
The condition of paths not crossing is equivalent to the property that the endpoints of paths in the string representation of s ordered by π be properly nested.Thus, we can choose S k = S p G (U ), where S p G (U ) is defined as follows.Here v is the index of vertex v in π.
The string representations of the border structures in S p G (U ) ordered by π are equivalent to 2-colored Motzkin words [17], which are strings with the character set {α 1 , α 2 , opening bracket '(', closing bracket ')'}, and the property that the brackets be properly nested.The string representation of a border structure can be mapped to the corresponding 2-colored Motzkin word by replacing endpoints of paths with opening and closing brackets, vertices forming a trivial path with α 1 , and vertices with degree two with α 2 .

Grid graphs
The n × n grid graph is planar, but it has some additional structure that allows us to choose an S k smaller than S p G (U ).Given a border structure s, the following theorem gives a sufficient condition for s ∈ H G (U ).Theorem 12.A border structure s on U , where a vertex v with |{{v, a} ∈ E(G) : a ∈ V (G) \ U }| 1 forms a trivial path, cannot be extended to a complete cycle.
Proof.Assume that C is a partial hamiltonian cycle such that S(C) = s, and assume that C can be extended to a complete hamiltonian cycle Γ.
The degree of v in Γ is |{{v, a} ∈ E(Γ)}|, which can be divided into two parts |{{v, a} ∈ By combining these two results we obtain that the degree of v in Γ is at most 1, a contradiction.
the electronic journal of combinatorics 21(4) (2014), #P4.7 Some of the borders B G (U ) we use in the computations for grid graphs have the property that |{{v, a} ∈ E(G) : a ∈ V (G) \ U }| 1 holds for every vertex v ∈ B G (U ).For such borders, we can choose S k = S g G (U ), where S g G (U ) is defined as follows.
S g G (U ) = {s ∈ S p G (U ) : s contains no trivial paths} Other borders B G (U ) we use in the computations for grid graphs have the property that |{{v, a} ∈ E(G) : a ∈ V (G) \ U }| 1 holds for every vertex v ∈ B G (U ) except one, v 1 .For these borders, we can choose S k = S g,v 1 G (U ), where S g,v 1 G (U ) is defined as follows.
S g,v 1 G (U ) = {s ∈ S p G (U ) : s contains no trivial paths other than {v 1 }} Let π be defined as in Section 2.3.1.The string representation of S g G (U ) ordered by π corresponds to Motzkin words [1], which are strings with the character set {α 1 , opening bracket '(', closing bracket ')'}, and the property that the brackets be properly nested.

The size of S k
To understand the memory requirements of our method, it is useful to know |S k | for each of the four choices of S k discussed in the previous subsections, namely, S G (U ), S p G (U ), S g G (U ), and Proof.For any border structure in S G (U ), the corresponding function representation f fulfills the following property: Conversely, every function with property (i) corresponds to a unique border structure on B G (U ).Thus, |S G (U )| is the same as the number of functions f : B G (U ) → B G (U ) ∪ {∞} with property (i).This number depends only on the size of B G (U ), and is denoted by Clearly, T 0 = 1 and The result follows by taking the sum of both cases.
| is the number of 2-colored Motzkin words of length n, which is known to be the (n + 1)th Catalan number C n+1 [17].Similarly, |S g G (U )| is the number of Motzkin words of length n, i.e., the nth Motzkin number M n [1].Lastly, The size of the first part is M n−1 and the size of the second part is M n .
In the Open Encyclopedia of Integer Sequences [18], T n , C n , and M n are the sequences A005425, A000108, and A001006 respectively.Table 1 shows the approximate sizes of S G (U ), S p G (U ), S g G (U ), and S g,v 1 G (U ) for small n.It turns out that in the computations for the king's graph almost all border structures in S G (U ) are encountered at any given step of the algorithm.In the computations for the grid graph, approximately half of the border structures in S g,v 1 G (U ) are encountered.

Computing rank 2.4.1 General graphs
We can compute the rank of border structures in S G (U ) with standard methods as follows.
We order the border structures lexicographically based on their string representation ordered by π, for some arbitrary π.Denote the size of the border B G (U ) by n.Consequently, the length of each string representation is n.Consider some s ∈ S G (U ), and let s be the string representation of s ordered by π.It is easy to see that the lexicographic rank of s is rank(s, S G (U ), <) = n−1 i=0 F (i, s ), where F (i, s ) is the number of border structures in S G (U ) whose string agrees with s on indices smaller than i and is strictly smaller on index i.
The value of F (i, s ) can in turn be computed as follows.We call the indices from 0 to i − 1 the left part of the string, and from i + 1 to n − 1 the right part of the string.Let N be the number of indices in the right part of the string for which the value is already fixed in the left part, i.e., the number of indices k such that k > i > s (k).Let L be the number the electronic journal of combinatorics 21(4) (2014), #P4.7 The number of hamiltonian cycles in an n × n grid is sequence A003763 in the Open Encyclopedia of Integer Sequences [18].Note that this sequence only shows the values for even n, since the number of hamiltonian cycles in the n × n grid graph is zero for odd n.For n × n king's graphs and triangular grids with n vertices on one side the numbers of hamiltonian cycles are in sequences A140519 and A112676 respectively.
In the dynamic programming method, we order the vertices of these graphs as follows.For the grid graph and the king's graph we order the vertices lexicographically, i.e., from left to right, and from top to bottom.For the triangular grid we use reverse lexicographic ordering.For the three-dimensional grid graph we order the vertices based on the distance to one of the corners, with ties broken lexicographically.

Results
Results for all the graph families described in Section 3 are shown in Tables 2 to 5, and new results are denoted with a star.In all of the cases studied here the computations were bounded more by memory than CPU-time.Enumerating hamiltonian cycles in the grid graph for n = 26 required the most resources, approximately 3 core-years of CPU-time and 160 gigabytes of memory using four bytes per counter.
The following two observations give some confidence in the correctness of the results and the implementation.First, the results agree with previously published work for small parameter values.Second, the total number of hamiltonian cycles given by the Chinese remainder theorem always converges, in the sense that the final value remains the same after computing the number of cycles for a few more prime powers than necessary.

Figure 2
Figure 2 illustrates these definitions.Vertices belonging to U are in bold, and edges belonging to the partial hamiltonian cycle C are thick.Here U = {1, 2, 3, 4, 5, 6}, and ∞ otherwise.In Figure 2 the function representation of S(C) has values

Table 3 :
Hamiltonian cycles in n × n king's graphs