Hamiltonicity (Hamiltonian Cycle) is one of the prototype NP-complete problems from Karp’s 1972 paper . An important NP-complete special case of Hamiltonicity is its restriction to (square) grid graphs , where vertices lie on the 2D integer square grid and edges connect all unit-distance vertex pairs. Hamiltonicity in grid graphs has been the basis for NP-hardness reductions to many geometric and planar-graph problems, such as Euclidean TSP , Euclidean degree-bounded minimum spanning tree , 2D platform games with item collection and time limits , the Slither Link puzzle , the Hashiwokakero puzzle , lawn mowing and milling (e.g., 3D printing) , and minimum-turn milling ; see .
Given all these applications, it is natural to wonder how special we can make the grid graphs, and whether we can change the grid to triangular or hexagonal, and still keep Hamiltonicity NP-complete. Two notable examples are NP-completeness in maximum-degree-3 square grid graphs  and a polynomial-time algorithm for solid square grid graphs . In 2007, Arkin et al.  initiated a systematic study of the complexity of Hamiltonicity in square, triangular, or hexagonal grid graphs, restricted to several special cases: polygonal, thin, superthin, degree-bounded, or solid grid graphs. See  or Section 2 for definitions. Table 1 (nonbold) summarizes the many results they obtained, including several NP-completeness results and a few polynomial-time algorithms.
Arkin et al.  left unsolved three of the combinations between grid shape and special property: Hamiltonicity of Square Polygonal Grid Graphs, Hamiltonicity of Hexagonal Thin Grid Graphs, and Hamiltonicity of Hexagonal Solid Grid Graphs. In this paper, we prove that the first two of these, Hamiltonicity of Square Polygonal Grid Graphs and Hamiltonicity of Hexagonal Thin Grid Graphs, are NP-complete. In addition, we consider another case not considered in that paper, namely, thin polygonal grid graphs (the fusion of two special cases). We show that Hamiltonicity becomes polynomially solvable in this case, for all three shapes of grid graph.
Table 1 (bold) summarizes our new results.
In Section 2, we briefly define the several types of grid graphs. In Section 3, we show that Hamiltonicity can be solved in polynomial time in the three thin polygonal grid graph cases; this is particularly challenging for hexagonal grid graphs, where the problem reduces to the polynomially solvable problem Max-Degree- Tree-Residue Vertex-Breaking. In Section 4, we prove NP-completeness of Hamiltonicity of Hexagonal Thin Grid Graphs. In Section 5, we prove NP-completeness of the Hamiltonicity of Square Polygonal Grid Graphs problem. Finally, in Section 6, we discuss the final remaining open problem, Hamiltonicity of Hexagonal Solid Grid Graphs.
2 Grid Graph Terminology
In this section, we introduce the definitions of several terms relating to grid graphs. We restrict our attention to only those terms and concepts relevant to the contents of this paper. See Arkin et al.  for a more general overview of these concepts.
We begin with a general definition.
The sets , , and refer to the sets of vertices of the tilings of the plane with unit-side squares, equilateral triangles, and regular hexagons. A grid graph is a finite graph where is a subset of , , or and is the set of pairs of elements of such that and are at a distance of from each other. If , the grid graph is said to be a square grid graph. Similarly, if then is said to be a triangular grid graph and if then is said to be a hexagonal grid graph.
Because we are concerned with Hamiltonicity, we restrict our attention to connected grid graphs with no degree-1 vertices. This does not affect the hardness of any Hamiltonicity problems because all grid graphs which are disconnected or which contain a degree-1 vertex are trivially not Hamiltonian and can be easily recognized.
In order to define the grid graph properties we are interested in, we need some more terminology:
Let be a grid graph. Consider the faces of the graph. There is one unbounded face. The cycle bordering this unbounded face is called the outer boundary of . The bounded faces of fall into two categories. Any bounded face containing a lattice point in its interior is called a hole. The cycles bordering the holes of are called the inner boundaries of . The other category of bounded face is the category without lattice points in the interior; any such face must necessarily have a minimal length cycle (length 3, 4, or 6 for triangular, square, or hexagonal grid graphs) as its boundary. This type of face is called a pixel. Any vertex on the inner or outer boundaries is called a boundary vertex. All other vertices are interior vertices.
The above terminology allows us to define the grid graph properties of interest:
A polygonal grid graph is a grid graph such that every vertex in and every edge in belongs to a pixel and such that no vertex can be removed to merge two boundaries (See Figure 1, top.)
A grid graph is called solid if it has no holes, or equivalently if every bounded face is a pixel. (See Figure 1, middle.)
A grid graph is called thin if every vertex in the graph is a boundary vertex. Note that a thin grid graph need not be polygonal. (See Figure 1, bottom.)
Now that we have defined all of the relevant terms, we can state the problems in question: the Hamiltonicity of [Square/Hexagonal/Triangular] [Polygonal/Thin/Polygonal Thin] Grid Graphs problem asks whether a given [square/hexagonal/triangular] [polygonal/thin/polygonal and thin] grid graph is Hamiltonian.
3 Polygonal Thin Grid Graph Hamiltonicity is Easy
In this section, we show that the three polygonal thin grid graph Hamiltonicity problems are all polynomial-time solvable. This is trivial for triangular grids and easy for square grids, but is non-trivial to show for hexagonal grids.
3.1 Triangular Grids
Theorem 3.1 ().
The Hamiltonicity of Triangular Polygonal Thin Grid Graphs problem is polynomially solvable.
Proof: This problem is a special case of the Hamiltonicity of Triangular Polygonal Grid Graphs problem, which was shown to be polynomially solvable in .
3.2 Square Grids
We prove below that
Every polygonal thin square grid graph is Hamiltonian.
and therefore conclude that
There exists a polynomial time algorithm which decides the Hamiltonicity of Square Polygonal Thin Grid Graphs problem (the “always accept” algorithm).
To prove Theorem 3.2, we will provide a polynomial-time algorithm for finding a Hamiltonian cycle in a polygonal thin square grid graph, prove that if the algorithm produces an output then the output is a Hamiltonian cycle, and prove that following the algorithm is always possible.
First, the algorithm: Suppose the set of pixels in input graph is . Initialize to be the empty set. Then repeat the following step until contains no cycles of pixels: identify a cycle of pixels in , find a pixel in this cycle such that exactly two pixels in neighbor and the two neighboring pixels are on opposite sides of , and add to . Once this loop is finished, let be the set of pixels in but not in . Treating as a region, output the boundary of that region as a Hamiltonian cycle.
Clearly, this algorithm is a polynomial-time algorithm. The only questions are (1) whether the output is actually a Hamiltonian cycle if the algorithm succeeds and (2) whether a given cycle of pixels always contains a pixel such that exactly two pixels in neighbor and the two neighboring pixels are on opposite sides of . We prove that the answer to both these questions is “yes” below:
Provided the given algorithm succeeds at each step on input , the final output will be a Hamiltonian cycle in .
Proof: Since is connected, the pixels in are connected as well. At every step of the algorithm, remains connected since the only pixel added to (and therefore removed from ) is a pixel in a cycle with no neighbors outside the cycle. Furthermore, the final value of (also known as ) will be acyclic since that is the terminating condition of the loop. Thus is connected and acyclic. In other words is a tree of pixels. As a result, the region defined by is connected and hole-free. Therefore the boundary of is one cycle. All that is left to show is that every vertex in is on this boundary.
Consider any vertex in . is a thin grid graph, so every vertex, including , is on the boundary of . Then provided is adjacent to some pixel in , we also have that is on the boundary of . Thus we need to show that every vertex is adjacent to at least one pixel in .
Consider any pair of adjacent pixels and such that each of the two pixels has exactly two neighbors. As soon as one of these pixels is added to (if this ever occurs), the other will forevermore have at most one neighbor in . As a result, this second pixel will never be in a cycle of pixels in . Then this second pixel will never itself be added to , or in other words at most one of and will be added to . Thus, the final value of the set will contain no two adjacent pixels, or in other words every pixel adjacent to a pixel in will be in .
But if contains pixel then every vertex adjacent to is also adjacent to one of the two neighbors of (since the two neighbors must be on opposite sides of ). Since these neighbors are in , we see that every vertex adjacent to a pixel in is also adjacent to a pixel in .
Since the graph is polygonal, every vertex in the graph is adjacent to some pixel: either a pixel in or a pixel in . In either case, we can conclude that the vertex is adjacent to a pixel in , and therefore, as argued above, the boundary of is a Hamiltonian cycle in .
For any cycle of pixels in a polygonal thin grid graph , there exists a pixel in that cycle such that exactly two pixels in neighbor and the two neighboring pixels are on opposite sides of .
Consider the leftmost column of pixels which contains any pixels in and let be the bottom-most pixel of in this column. Assign and coordinates to the pixels in so that has coordinates and the coordinates increase as we go up and to the right. See Figure 1(a).
By definition of , we know that is not a pixel in for any and neither is . These pixels are crossed out in Figure 1(b).
But is a cycle so must have exactly two neighbors in . Therefore and must both be in . Then in order for to be thin, pixel cannot be in (nor in ). is a pixel in and therefore must have two neighbors in . Since neither nor are pixels in we can conclude that these two neighbors must be and . In particular, must be a pixel in .
Suppose for the sake of contradiction that is a pixel in . Then , , , , and are all pixels in . As a result, all four vertices on the boundary of pixel are in , and so since is an induced subgraph, the edges between these vertices are in as well. As a result, we can conclude that pixel is in , which is a contradiction. Thus is not a pixel in .
Pixel is in and therefore must have two neighbors in . Since neither nor is in we can conclude that these two neighbors must be and . In particular, must be in .
We have shown that , , , and are all pixels in and that and are not pixels in . See Figure 1(c). Since is thin, either or must be a pixel not in . Then for some we have that is a pixel in , are pixels in , and are pixels not in .
That pixel is the pixel we wished to find: a pixel in such that exactly two pixels in neighbor and the two neighboring pixels are on opposite sides of .
3.3 Hexagonal Grids
Consider the following problem:
The Tree-Residue Vertex-Breaking problem asks for a given multigraph in which every vertex is labeled as “breakable” or “unbreakable” whether there exists a subset of the breakable vertices such that “breaking” those vertices results in a tree.
Here the operation of breaking a vertex in a multigraph (shown in Figure 3) results in a new multigraph by removing the vertex, adding a number of new vertices equal to the degree of the vertex in the original multigraph, and connecting these new vertices to the neighbors of the vertex in a one-to-one manner.
This problem and its variants were studied in :
 The Max-Degree- Tree-Residue Vertex-Breaking problem asks the same question as the Tree-Residue Vertex-Breaking problem, but restricts the inputs to be graphs whose vertices each have degree at most . The Max-Degree- Tree-Residue Vertex-Breaking problem is polynomial-time solvable.
In this section, we will show that
There exists a polynomial-time reduction from the Hamiltonicity of Hexagonal Polygonal Thin Grid Graphs problem to the Max-Degree- Tree-Residue Vertex-Breaking problem.
and therefore that
The Hamiltonicity of Hexagonal Polygonal Thin Grid Graphs problem is polynomial-time solvable.
We prove Theorem 3.7 with the following reduction: On input a hexagonal polygonal thin grid graph , construct the graph whose vertices are pixels and whose edges connect adjacent vertices. Label a vertex in as breakable if it has degree-. Otherwise label the vertex unbreakable. Output the resulting labeled graph.
To prove the correctness of this reduction, we first consider the behavior of a Hamiltonian cycle in the local vicinity of a pixel, then narrow down the possibilities using non-local constraints, and finally use the global constraints imposed by the existence of a Hamiltonian cycle to characterize the hexagonal polygonal thin grid graphs with Hamiltonian cycles. We will show using this characterization that a hexagonal polygonal thin grid graph is Hamiltonian if and only if the corresponding reduction output is a “yes” instance of Max-Degree- Tree-Residue Vertex-Breaking.
Proof: Consider the pattern of pixels in Figure 3(a). In any grid graph containing three pixels in this arrangement, the circled vertex is not on the boundary. Thus, because every vertex must be on the boundary of a thin grid graph, the three-pixel pattern does not occur in any hexagonal thin grid graph.
Next consider a single pixel. It can have up to 6 neighboring pixels, but in order to avoid the three pixel arrangement from Figure 3(a), it will have at most 3 neighbors.
If a pixel has zero neighbors (i.e., Figure 3(b)), it is a single connected component of the graph. Because the graphs we consider are connected, that means that the pixel is the entire graph. In that case, there is a Hamiltonian cycle (consisting of the whole graph). Since in this case we can easily solve the Hamiltonicity problem, we restrict our attention to other cases.
If a pixel has exactly one neighbor, the cycle must pass through it as shown in Figure 3(c) (up to rotation).
If a pixel has exactly two neighbors, they can be arranged (up to rotation) in two ways. If the two neighboring pixels are opposite, the cycle must pass through the pixel as shown in Figure 3(d) (left). In the other arrangement, there are two possibilities as shown in Figure 3(d) (right).
Finally, there is exactly one way to arrange a pixel with three neighboring pixels, and the cycle can pass through this type of pixel in seven different ways. This is shown in Figure 3(e).
The different possibilities of how to arrange the cycle in a given diagram were computed by exhaustive search subject to the constraints that (1) every vertex must be in the cycle and (2) exactly two edges next to each vertex must be in the cycle.
Proof: Suppose that we have three pixels arranged as shown in the leftmost part of Figure 4(a) with the three bold edges definitely included in the Hamiltonian Cycle. Because the circled vertex in the second part of the figure has to have two edges, we can conclude that the situation must be as shown in the third part of the figure. But the bottom right edge must be part of a pixel, and because every vertex must be on the boundary, only one of the two possible pixels will work. This yields the situation in the fourth part of the figure. Consider the circled vertex in the fifth part of the figure. That vertex must have two edges in the Hamiltonian cycle touching it. One is already accounted for, so we just have to decide on the other.
Consider for the sake of contradiction that the chosen edge was as shown in the first part of Figure 4(b). Then because of the circled vertex in the second part of the figure, the situation must be as shown in the third part of the figure. Again, every edge must be part of a pixel, and again only one of the two possible pixels would yield a thin graph. Thus we arrive at the situation in the fourth part of the figure. Because of the circled vertex in the fifth part of the figure, we arrive at the situation in the sixth. But then the dotted edge in the seventh figure must also be in the graph (because no two adjacent vertices can exist in the graph without the edge between them also being in the graph). This, however, yields a graph that is not thin. Thus we have a contradiction.
Therefore, what must actually have happened is shown in the left side of Figure 4(c). Looking only at the bottom three pixels in this new situation and ignoring some of the bold lines, we have the situation in the right half of the figure. Note, however, that this situation is identical (up to reflection and translation down) to the situation at the start of Figure 4(a). Thus, having the pattern in the first part of Figure 4(a) necessitates another copy of the same pattern lower down (and flipped). That in turn necessitates another copy lower down, which necessitates another copy lower down, etc… In other words, no finite graph can contain the pattern shown at the start of Figure 4(a).
As a result of this, many of the “possible” local solutions at a pixel are actually not allowed.
We can restrict the possibilities even more, however. Consider the penultimate scenario from Figure 3(e). Under this scenario, consider the circled vertices in the first part of Figure 4(d). The constraints at those vertices lead to the scenario in the center part of that figure, which in turn leads to the existence of the edge added in the final part of the figure. This contradicts the fact that the grid graph must be thin, so the actual list of possible local solutions does not include the penultimate scenario from Figure 3(e).
Then the restricted list of possibilities is as shown in Figure 4(e) where we include only those local solutions which omit the pattern at the start of Figure 4(a) and where we exclude the penultimate scenario from Figure 3(e).
Finally, we wish to show that the last situation listed in Figure 4(e) directly leads to the situation shown in the last part of Figure 4(f). The first part of Figure 4(f) is exactly the final situation listed in Figure 4(e). Consider the circled vertices in the second part of Figure 4(f). The constraints at these vertices lead to the situation shown in the third part of Figure 4(f). Next, due to the thin and polygonal properties, the pixels added in the penultimate part of Figure 4(f) must be present in the graph. Consider the rightmost pixel with three neighbors. We have a list of situations that are possible in the vicinity of a pixel with three neighbors (in Figure 4(e)), and only one of those possibilities matches the already chosen edges. Thus, we must have the situation shown in the last part of Figure 4(f).
There exists a Hamiltonian cycle in a hexagonal polygonal thin grid graph if and only if there exists a tree of pixels such that every pixel with fewer than three neighbors is in the tree and such that at least one pixel out of every pair of adjacent degree- pixels is in the tree.
Proof: First suppose there exists a Hamiltonian cycle in a hexagonal polygonal thin grid graph.
Based on the local solutions for degree- and degree- pixels, whenever one pixel with degree at most two is adjacent to another, the two pixels are on the same side of the cycle (both inside or both outside). Based on the local solutions for degree- pixels (and based on the specific situation that the final situation listed in Figure 4(e) necessarily leads to), whenever two degree- or degree- pixels are adjacent to the same degree- pixel, the two pixels are on the same side of the cycle. Finally, whenever two degree- pixels are neighbors, their other four neighbors are all degree- or degree- pixels and are all on the same side of the cycle. All together, this implies that all pixels with at most two neighbors must be on the same side of the cycle. And because the unbounded face is outside the cycle, the faces next to it—pixels—must be inside the cycle.
We can conclude that all pixels with two or fewer neighbors are always inside the cycle. This immediately implies that all holes are outside the cycle.
Clearly, the pixels inside the Hamiltonian cycle are connected. Furthermore, they are acyclic because a cycle inside the Hamiltonian cycle would imply either that the Hamiltonian cycle contains a hole or that the graph contains an interior vertex. Thus the pixels inside the Hamiltonian cycle form a tree.
In addition, it is easy to verify that whenever two degree- pixels are adjacent, they are not both outside the Hamiltonian cycle.
Next suppose that there exists a tree of pixels such that every pixel with fewer than three neighbors is in the tree and such that at least one pixel out of every pair of adjacent degree- pixels is in the tree. Then consider the perimeter of this tree of pixels. We claim that the perimeter is a Hamiltonian cycle. Clearly, the perimeter is a cycle, so the only question is whether it touches every vertex. Every vertex is either between two degree- vertices or adjacent to at least one degree- or degree- pixel. In either case, the assumptions are sufficient to show that the vertex is on the perimeter of at least one pixel in the tree. Because the graph is thin, there are no interior vertices; in other words, every vertex on the perimeter of a pixel in the tree is also on the perimeter of the entire tree of pixels. Thus the perimeter of the tree of pixels hits every vertex and is therefore a Hamiltonian cycle.
We have now shown both directions of the desired statement.
Suppose is a hexagonal polygonal thin grid graph and is the output of the reduction on input . Then is a “yes” instance of Max-Degree- Tree-Residue Vertex-Breaking if and only if there exists a tree of pixels in such that every pixel with fewer than three neighbors is in the tree and such that at least one pixel out of every pair of adjacent degree- pixels is in the tree.
Proof: First suppose that is a “yes” instance of Max-Degree- Tree-Residue Vertex-Breaking. Then let be the set of breakable vertices in whose breaking yields a tree. Vertices in correspond with pixels in , so define to be the set of pixels in that do not correspond to vertices in .
Breaking a vertex in corresponds to removing the vertex and then adding some number of degree- vertices. The result of breaking the vertices of in is a tree . If instead of breaking the vertices we simply removed them, we would get a graph which could instead be obtained by removing the degree- vertices that were added during the breaking operation. Since removing degree- vertices from a tree yields a tree, we can conclude that is a tree. But has as vertices the pixels of and as edges the adjacencies between pixels. Thus forms a tree of pixels.
By the way the reduction was defined, every breakable vertex corresponds to a pixel in of degree-. Thus contains only vertices of degree- and so contains every pixel that has fewer than three neighbors.
Consider any pair of adjacent degree- pixels. If the vertices corresponding to both pixels were in then both vertices would be broken, leading to the edge between those two vertices being disconnected from the rest of the graph. Since breaking the vertices of in yields a tree, this is not the case. Thus, at least one of the two vertices is not in , and so at least one of the pair of pixels is in .
We have shown that is a tree of pixels in such that every pixel with fewer than three neighbors is in the tree and such that at least one pixel out of every pair of adjacent degree- pixels is in the tree. Thus whenever is a “yes” instance of Max-Degree- Tree-Residue Vertex-Breaking such a tree of pixels must exist.
Next we prove the other direction. Suppose that is a tree of pixels in such that every pixel with fewer than three neighbors is in the tree and such that at least one pixel out of every pair of adjacent degree- pixels is in the tree. Define to be the set of breakable vertices in corresponding to pixels of that are not in .
Consider the graph , which is constructed from by removing every vertex in . The vertices of this graph are the pixels in and the edges are adjacencies of pixels. Thus, since is a tree of pixels, is a tree.
Also consider the graph that results from breaking the vertices in . Breaking a vertex can be accomplished by removing it and then adding some number of degree- vertices. Therefore the graph could also be constructed from by adding some number of degree- vertices. If every vertex that is added has as its sole neighbor a vertex from then the addition of these degree- vertices maintains the tree property, allowing us to conclude that is a tree.
Note that by the definition of , no two vertices in are adjacent. Suppose is a degree- vertex created to neighbor vertex during the breaking operation of vertex . Since contains no adjacent pairs of vertices, , and therefore is never broken when constructing . Thus the sole neighbor of in is , which is a vertex from . As stated above, applying this logic to every allows us to assert that is a tree, and therefore (since the maximum degree of vertices in is at most ) that is a “yes” instance of Max-Degree- Tree-Residue Vertex-Breaking.
We have shown both directions, thus proving the desired equivalence.
Putting the last two lemmas together, we conclude that the given reduction is correct.
4 Hexagonal Thin Grid Graph Hamiltonicity is NP-complete
In this section, we show that the Hexagonal Thin Grid Graph Hamiltonicity problem is NP-complete. Membership in NP is trivial, while NP-hardness follows via reduction from -Regular Breakable Planar Tree-Residue Vertex-Breaking.
Recall the definition of the Tree-Residue Vertex-Breaking problem from Section 3.3:
The problem we will reduce from is one of the variants of Tree-Residue Vertex-Breaking from :
 The -Regular Breakable Planar Tree-Residue Vertex-Breaking problem asks the same question as the Tree-Residue Vertex-Breaking problem, but restricts the inputs to be a planar multigraph whose vertices are each breakable and have degree exactly . The -Regular Breakable Planar Tree-Residue Vertex-Breaking problem is NP-complete.
The main work of this reduction is constructing a gadget to simulate a degree- breakable vertex. The desired behavior of the gadget is shown in Figure 6. If we define a wire to be a path of pixels then the idea of the gadget is to connect incoming wires in such a way that the local constraints on a hypothetical Hamiltonian cycle allow only two possible solutions within the gadget. In one of the locally allowed solutions (Figure 5(b)) the regions inside the six wires are connected through the gadget while in the other (Figure 5(c)) the regions are all disconnected at the gadget. Note that the gadget shown in Figure 6 is only a schematic and cannot be used as the actual gadget for the reduction because it does not lie on the hexagonal grid.
Below, we will (1) provide and analyze a reduction from the -Regular Breakable Planar Tree-Residue Vertex-Breaking problem under the assumption of the existance of a degree- breakable vertex gadget and (2) provide the gadget and prove that it has the desired behavior.
Suppose we have a degree-6 breakable vertex gadget with the desired behavior. Then to complete the reduction from the -Regular Breakable Planar Tree-Residue Vertex-Breaking problem, we simply lay out the given multigraph in the plane, replace every vertex with the degree- breakable vertex gadget, and extend a wire for each edge from the gadget of one endpoint to the gadget of the other. Since finding a planar embedding for a graph is a polynomial time operation (provided one exists), such a reduction can be completed in polynomial time.
Below, we define more precisely what constraints a degree- vertex gadget must satisfy and then prove that the above reduction is correct. In short, the idea is that there is a correspondence between breaking a vertex and choosing the gadget solution shown in Figure 5(c); under this correspondence the shape of the region inside the candidate set of edges is the same as the post-breaking multigraph. This region is connected and hole-free if and only if the post-breaking graph is connected and acyclic. Since a region has as its boundary a single cycle if and only if the region is connected and hole-free, we conclude that the candidate set of edges is a Hamiltonian cycle if and only if the answer to the corresponding -Regular Breakable Planar Tree-Residue Vertex-Breaking instance is “yes.”
Suppose we start with an instance of -Regular Breakable Planar Tree-Residue Vertex-Breaking consisting of multigraph and produce via the above reduction a grid graph .
Define a candidate solution for the Hexagonal Thin Grid Graph Hamiltonicity instance to be a set of edges in satisfying the following constraints: (1) every vertex in is the endpoint of exactly two edges in and (2) no one gadget or wire contains a cycle of edges from entirely inside it.
Note that failing to satisfy either of the constraints in the above definition is sufficient to disqualify a set of edges from being a Hamiltonian cycle. Thus the question of whether a Hamiltonian cycle exists in is the same as the question of whether a candidate solution consisting of just one cycle exists.
Before, we described a degree- breakable vertex gadget by saying that the local constraints on a hypothetical Hamiltonian cycle allow only two possible solutions within the gadget. To make this more precise, intersecting the set of edges in the gadget with the set of edges in a candidate solution should only have two possible results. As before, the two possibilities are shown in Figure 6. We will refer to the posibilities in Figure 5(b) and Figure 5(c) as the connecting and disconnecting solutions respectively. We will refer to a vertex gadget using one of these solutions as a connecting or disconnecting vertex gadget.
Consider a wire between two gadgets and let be any candidate solution. In both possible solutions of a gadget, there are two edges from the gadget entering into the wire. Thus two edges from must enter the wire from each end. Simply applying the definition of a candidate solution (no cycles within a wire and every vertex must touch two edges), we can conclude that the edges in the wire that belong to must be the boundary edges of the wire. For example, see Figure 7.
As a result, a candidate solution is completely constrained by the choice of behavior at each degree- vertex gadget. We can identify the disconnecting solution of a vertex gadget with the choice of breaking the corresponding vertex and identify the connecting solution with the choice of leaving the vertex unbroken. Under this correspondence we have a bijection between the choice of candidate solution in the Hexagonal Thin Grid Graph Hamiltonicity instance and the choice of what vertices to break in the -Regular Breakable Planar Tree-Residue Vertex-Breaking instance . We show below that the candidate solutions which are Hamiltonian cycles correspond under this bijection with the choices of vertices to break whose breaking converts into a tree. As a result, a Hamiltonian cycle exists in if and only if it is possible to break vertices in so as to obtain a tree, so we conclude that the reduction is correct.
By definition, a candidate solution is a disjoint cycle cover of (since every vertex has two edges in incident on it). Therefore seperates the plane into several regions. Among these regions, consider the ones which contain the interior of a wire. Let be the union of these regions.
Looking at the possible behaviors of a candidate solution in a vertex gadget, it is easy to see that will consist of the interior of each wire, one connected hole-free region from each connecting vertex gadget, and six connected hole-free regions from each disconnecting vertex gadget. In fact, the boundary of is exactly the set of edges .
Consider the above decomposition of into sub-regions. The choice of candidate solution corresponds with a set of vertices to break in . Let be the version of with the vertices of broken. The sub-regions of exactly correspond with the vertices and edges of . If and are vertices in then the edge in also occurrs in and corresponds to the interior of the wire between the vertex gadgets for and . If is a vertex of then is also a vertex in and corresponds to the sub-region of from the vertex gadget for . If is a vertex of then there are six vertices in that result from the breaking of , and these six vertices corresponds to the six sub-regions of from the vertex gadget for .
Clearly, the vertices and edges of correspond to the sub-regions of . Furthermore, it can be easily verified that two sub-regions of touch if and only if those two sub-regions correspond to a vertex and an edge in such that the is an endpoint of . Furthermore, there is no hole in at such a point of contact between two sub-regions. We show below using these facts that is connected if and only if is connected and that is acyclic if and only if is hole-free.
is connected if and only if there exists a path through from any edge or vertex to any other edge or vertex . Such a path can be expressed as a list of edges and vertices such that every consecutive pair and consists of one edge and one vertex that is an endpoint of that edge. We can create a corresponding list of sub-regions of of the form where is the sub-region corresponding to edge or vertex . Note that in this list, every consecutive pair and consists of two touching subregions. Thus we see that is connected if and only if for every pair of sub-regions , there exists a list of the form with touching for each . Since every sub-region is itself connected, this last condition is equivalent to the condition that there exist a path in between any two points; or in other words, is connected if and only if is connected.
Consider any cycle in consisting of vertices and edges . This cycle corresponds to a cycle of sub-regions . Such a cycle of sub-regions will have an inner boundary, or in other words a hole. On the other hand, for any hole in , we can list the subregions going around that hole: . Since each sub-region is hole free and each sub-region contact point does not have a hole, this list of subregions will correspond to a cycle of vertices and edges in . Thus we see that is acyclic if and only if is hole-free.
is a tree, or in other words connected and acyclic, if and only if is hole-free and connected. But is hole-free and connected if and only if the boundary of (which happens to be ) is exactly one cycle. Since is a cycle cover of , is a Hamiltonian cycle if and only if consists of exactly one cycle. Putting that all together, we see that is a tree if and only if is a Hamiltonian cycle in .
As you can see, the constructed grid graph is Hamiltonian if and only if the input -Regular Breakable Planar Tree-Residue Vertex-Breaking instance is a “yes” instance, and therefore the reduction is correct.
4.2 Degree- breakable vertex gadget
Thus, all that is left is to demonstrate a degree- breakable vertex gadget of the desired form. The gadget we will use is shown in Figure 7(a).
We show below that the gadget only has the two desired solutions.
Figure 7(b) shows the gadget from Figure 7(a), but with all edges that must be in any candidate solution bold. These constraints can be derived purely from the fact that every vertex must be the endpoint of exactly two edges in a candidate solution.
The gadget contains six contiguous regions of pixels, arranged in a cycle with a single edge that is not part of any pixel between adjacent regions. We claim that either all of these single edges must be in the candidate solution or none of them. It can be verified that these two possibilities lead, again via the constraints on candidate solutions, to the two solutions shown in Figures 7(c) and 7(d).
All that’s left to show is that our claim is correct: that using only some of the single edges in a candidate solution is impossible. Suppose for the sake of contradiction that a candidate solution exists which uses some but not all of these edges. Going around the cycle of regions, there will be some region where we transition from using the single edge to not using it. That region will have exactly one of its two single edges in the candidate solution. Thus, the candidate solution will enter or exit the region exactly three times: twice through the wire that exists the gadget from this region and once through the single edge. Since this is impossible for a cycle cover, we arrive at the desired contradiction.
5 Square Polygonal Grid Graph Hamiltonicity is NP-complete
In this section, we show that the Square Polygonal Grid Graph Hamiltonicity problem is NP-complete. Membership in NP is trivial, while NP-hardness follows from a polynomial-time reduction. We reduce from the Planar Monotone Rectilinear 3SAT problem—which was shown NP-hard in —to the Square Polygonal Grid Graph Hamiltonicity problem. In order to state the Planar Monotone Rectilinear 3SAT problem, we first need some preliminary terms:
A rectilinear embedding of a CNF formula into a plane is a planar embedding of the variable-clause bipartite graph for the CNF formula into the plane such that the vertices (variables and clauses) are mapped to horizontal segments, the edges are mapped to vertical segments, and the variables all lie on the axis. Furthermore, a monotone rectilinear embedding of a CNF formula into a plane is a rectilinear embedding with the additional property that the clauses positioned above the axis contain only positive literals while clauses positioned below the axis contain only negative literals.
Figure 9 shows an example of a rectilinear embedding. This embedding could represent several possible CNF formulas, such as or . If, however, this is a monotone rectilinear embedding then a specific CNF formula, is implied.
Now the problem we are reducing from can be stated as follows:
The problem Planar Monotone Rectilinear 3SAT decides, for every monotone rectilinear embedding of a 3-CNF formula into the plane, whether the given instance is satisfiable.
Our overall strategy for reducing Planar Monotone Rectilinear 3SAT to Hamiltonicity of Square Polygonal Grid Graphs is the following:
We begin by describing several simple gadgets.
A wire gadget consists of a path of pixels.
A one-enforcer gadget, when inserted into a wire, enforces the fact that any Hamiltonian cycle in the grid graph must pass through the wire only once, zig-zagging through the wire in one direction.
A two-enforcer gadget, when inserted into a wire, enforces the fact that any Hamiltonian cycle in the grid graph must pass through the wire twice, once in each direction.
We can then combine these gadgets to form variable and clause gadgets, which we will use to simulate a Planar Monotone Rectilinear 3SAT instance.
A variable gadget consists of a loop of wire, which when correctly inserted into a one-enforced wire enforces certain constraints on any Hamiltonian cycle (provided one exists). In particular, among the two wires in the gadget (the top and bottom halves of the loop of wire), one will be one-enforced, and the other will be two-enforced. This choice corresponds to a choice of value for the variable.
A clause gadget attaches to three variable gadgets (attaching to either the top or bottom wires of each) in such a way that the previously stated facts about variable gadgets continue to hold. If all three attaching points connect the clause gadget to one-enforced wire (due to the choices at the variable gadgets) then there exists no Hamiltonian cycle in the grid graph. Conversely, whenever this fails to occur, it is possible to modify one of the pieces-of-cycle passing through the variable gadgets to also pass through every point in the clause gadget.
Next, we assemble a grid graph out of these gadgets to simulate a Planar Monotone Rectilinear 3SAT instance. We create a variable gadget for every variable and connect them in a loop with one-enforcers appropriately placed. We then add clause gadgets outside the loop and inside the loop to represent the positive and negative clauses. For an example, see Figure 10.
If there exists a satisfying assignment then we can construct a cycle passing through the one-enforced loop and through the variable gadgets in such a way that each clause gadget attaches to at least one two-enforced wire (among the three variable gadget wires that it attaches to). Then by the clause gadget properties listed above, we can modify that cycle to also pass through every point in each clause gadget. This yields a Hamiltonian cycle. If there exists no satisfying assignment then any cycle passing through the one-enforced wire loop and variable gadgets will result in at least one clause gadget being attached to three one-enforced wires. Then by the clause gadget properties listed above, no Hamiltonian cycle exists. This shows that the reduction is answer preserving.
We follow this outline below to prove that
There exists a polynomial time reduction from the Planar Monotone Rectilinear 3SAT problem to the Hamiltonicity of Square Polygonal Grid Graphs problem.
and therefore that
The Hamiltonicity of Square Polygonal Grid Graphs problem is NP-complete.
5.1 Simple Gadgets
The simplest gadget we will use will be the wire, which is simply a path of pixels. As shown in Figure 11, there are multiple ways to pass a cycle through a wire. The important distinction to make is between “two-enforced” wires for which the cycle passes through most pixels twice, once in each direction (the first two solutions in Figure 11), and “one-enforced” wires for which the cycle passes through most pixels once (the final two solutions in Figure 11).
Note that the distinction between one-enforced and two-enforced wires must propagate through a wire by a simple contradiction argument: if a cycle goes into a wire once from one side and comes out twice from the other then it enters that wire an odd number of times in all, which is impossible for a cycle.
Furthermore, as seen in Figure 12, even if the wire turns (provided the turns are not too close), it is possible to propagate this property.
In both solutions, the behavior of the cycle in the wire attached to the gadget is the same: a one-enforced wire of a particular parity. The parity is different on each side; in other words, the gadget causes a parity shift in addition to its other effects.
Inserting a two-enforcer into a wire causes that wire to be two-enforced. The shape of a two enforcer is shown in Figure 13(a). Every edge adjacent to a vertex with only two neighbors must be in a Hamiltonian cycle if one exists. Thus all the bold edges in Figure 13(b) must be in the cycle. But then the section of gadget in the middle is a two-enforced wire and this property propagates along the wire in both directions away from the gadget.
5.2 Variable Gadget
A variable gadget consists of a loop of wire of odd width and height 6 that is inserted into a one-enforced wire of a particular parity. Figure 14(a) shows a variable gadget together with the appropriate one-enforcers necessary for the gadget to properly function.
Consider just one half of the gadget (Figure 15(a)). The possible ways for a cycle to pass through this gadget half are listed in Figure 15(b). The important thing to note is that in all cases one of the two branches is one-enforced and the other is two-enforced.
Putting the variable gadget together from two halves, we can figure out the legal solutions by assigning solutions to the two halves in ways that don’t result in isolated cycles. The only two possible ways (up to reflection) for a cycle to pass through a variable gadget are shown in Figures 14(b) and 14(c). As you can see, the variable gadget allows a choice: either the top or bottom wire must be two-enforced while the other must be one-enforced.
5.3 Clause Gadget
A clause gadget is a long horizontal wire with three two-enforced vertical wires brought vertically down (or up) from the horizontal wire. It is shown in Figure 17 with the two-enforcers omitted but with bold edges as appropriate to indicate that the wires are two-enforced.
When used, this gadget will attach each of the three two-enforced vertical wires to a variable gadget. A variable gadget has a top horizontal wire and a bottom horizontal wire. The vertical two-enforced wire from the clause is connected directly (in a particular place described later) to one of these two wires (the bottom one if the clause’s vertical wires go up from the horizontal clause section and the top if the clause’s vertical wires go down from the horizontal clause section). This is a modification to the variable gadget, and so we must verify that the variable gadget continues to work as expected (and no other way) with this addition.
In fact, a rigorous derivation of the behavior of a cycle in a variable gadget relies on a repeated application of the same parity argument. For example, to show that the two wires in a variable half cannot be both one-enforced or both two-enforced, we would argue that the total number of arcs of cycle at the edge of that gadget must be even, and therefore cannot be 3 or 5 (which would occur if the wires were both one- or two-enforced). Similarly, to show that the two-enforced wire from one half of the variable gadget cannot coincide with the one-enforced wire from the other, we argue based on the parity of the number of cycle arcs entering the wire. Since our modification only attaches a two-enforced wire, and since that addition only ever adds an even number of cycle arcs to a region, we see that the same arguments must continue to hold and therefore that the set of possible ways for a cycle to pass through a vertex gadget remains the same (one variable wire will be two-enforced while the other will be one-enforced).
So consider the horizontal variable wire that the clause gadget connects to. That wire could be either one-enforced or two-enforced. Furthermore, if the wire is one-enforced, the cycle follows a particular parity of zigzag that is known in advance. When attaching a clause gadget to the horizontal variable wire, the vertical clause wire is aligned so that if the variable wire is one-enforced, the two wires match up as shown in Figure 18.
We prove that if the variable wire is one-enforced, the situation from Figure 18 is exactly what will occur. Without loss of generality, suppose the horizontal wire is the top wire of the variable node. The cycle in the one-enforced variable wire propagates from the left through this wire. And the two arcs of the cycle in the two-enforced vertical clause wire propagate down. At this point, all we know is what is shown in Figure 18(a). Consider the circled node in that figure. It must contribute the next two edges as shown in Figure 18(b). Then the next circled node contributes the next edge as shown in Figure 18(c). Finally, the circled vertex in that figure contributes two edges to the cycle in the horizontal variable wire as shown in Figure 18(d). Thus the horizontal variable wire continues to be one-enforced despite the fact that the vertical wire is attached. As desired, the cycle must pass through this clause/variable junction as shown in Figure 18.
On the other hand, when the horizontal variable wire is two-enforced, the parts of the cycle inside the variable and clause gadgets are free to connect as shown in Figure 20. Note however, that the parts of the cycle in the two gadgets are also free to not connect, which is useful when multiple variables satisfy a clause.
Thus we can conclude the following two facts about our clause gadget.
If a Hamiltonian cycle passes through each vertex gadget such that some clause gadget attaches to some three one-enforced wires, then the part of the cycle in the clause gadget has no way to connect to the part in the variable gadgets and so we don’t actually have a Hamiltonian cycle (rather we have a contradiction). In other words, a Hamiltonian cycle passing through each vertex gadget must attach each clause gadget to at least one two-enforced horizontal variable wire.
If a grid graph has a Hamiltonian cycle and we add a clause gadget, attaching it to three horizontal vertex wires of which at least one is two-enforced, then the new graph is also Hamiltonian. This is because we can simply join the cycle that forms the boundary of the clause gadget to the pre-existing Hamiltonian cycle at one of the two-enforced horizontal variable wires (as in Figure 20), merging them into a Hamiltonian cycle for the entire graph.
5.4 Overall Reduction
Suppose we are given an instance of Planar Monotone Rectilinear 3SAT, or in other words a monotone rectilinear embedding of a 3-CNF formula in the plane. We convert this instance into a grid graph as follows. Each variable segment gets replaced by a variable gadget, which are connected in order with wires and one-enforcers. Then a long wire (possibly with extra one-enforcers for parity) connects the two variable gadgets at the ends, forming a big loop of variable gadgets (and one-enforcers). After that, we add clause gadgets for each clause. The positive clauses get a clause gadget above the variable gadgets while the negative clauses get a clause gadget inside the loop. An example schematic of what the final result might be is shown in Figure 21, corresponding to the Planar Monotone Rectilinear 3SAT instance shown in Figure 10.
As you can see, the resulting instance of Hamiltonicity of Square Polygonal Grid Graphs (the resulting grid graph) is very similar in shape to the original embedding. This similarity in shape between the embedding inputted into the reduction and the resulting instance of Hamiltonicity of Square Polygonal Grid Graphs can be used as an argument that this is a polynomial time reduction. It is very easy to construct the Hamiltonicity of Square Polygonal Grid Graphs instance from the embedding, and furthermore, the resulting graph is barely larger than the embedding. Due to the simplicity of the reduction, it is possible to construct the grid graph in time proportional to the size of the grid graph, but that size is itself linear in the size of the embedding. Thus we see that the reduction given above runs in polynomial time.
The remaining goal is to show that the reduction is answer preserving.
Consider first the case that the grid graph is Hamiltonian. Then we construct a variable assignment by assigning the value true to a variable if and only if that variable’s variable gadget has the top wire two-enforced. Consider any [positive/negative] clause [/] under this assignment. The corresponding gadget for a [positive/negative] clause is [outside/inside] the large loop in the grid graph; thus the clause gadget is attached to the [top/bottom] wires of the variable gadgets for variables , , and . By one of our derived properties for clause gadgets, we know that each clause is attached to at least one two-enforced wire; thus the [top/bottom] wire for the , , or variable gadget must be two-enforced. Equivalently, either , , or must be [true/false]. Thus, at least one variable in the clause is [true/false], and so at least one literal in the clause is true. In other words, the entire clause must be true. Then we see that we have found a satisfying assignment.
Next consider the case that a satisfying assignment for the formula exists. Consider the grid graph with all the clause gadgets removed (just a loop of variable gadgets and one-enforcers). Certainly a Hamiltonian cycle exists in this graph; in fact, many do: the cycle has two choices of behavior at each variable gadget. Construct the particular Hamiltonian cycle in which a variable gadget’s top wire is two-enforced if and only if the variable is assigned a value of true. We will add the clause gadgets back into the graph one at a time. By one of our derived properties for clause gadgets, we know that if the clause being added attaches to at least one two-enforced wire then we can extend the Hamiltonian cycle to the new graph. Consider a clause gadget for a [positive/negative] clause [/]. The gadget is [outside/inside] the large loop in the grid graph and is therefore attached to the [top/bottom] wires of the variable gadgets for variables , , and . But since the clause is satisfied, either , , or must be [true/false] and so the [top/bottom] wire for that variable gadget must be two-enforced. Thus the clause attaches to at least one two-enforced wire. Thus, as we add the clause gadgets back into the graph one at a time, the graph remains Hamiltonian. Clearly then the full graph is Hamiltonian.
As desired, we see that the grid graph in question is Hamiltonian if and only if the 3-CNF formula is satisfiable and that therefore we have described a polynomial-time answer-preserving reduction.
6 Conclusion and Further Work
We showed that Hamiltonicity of Thin Polygonal Grid Graphs is solvable in polynomial time for every shape of grid graph. In addition, we showed that Hamiltonicity of Square Polygonal Grid Graphs and Hamiltonicity of Hexagonal Thin Grid Graphs are both NP-complete. Having determined this, we have proved the hardness of two of the problems left open by Arkin et al. , as well as determining the complexity of three problems not addressed in that paper.
This leaves only one of Arkin et al.’s open problems, namely, the complexity of Hamiltonicity of Hexagonal Solid Grid Graphs. Arkin et al. conjectured that this problem can be solved in polynomial time, based on the idea that the polynomial-time algorithm for Hamiltonicity of Square Solid Grid Graphs could be adapted to also solve Hamiltonicity of Hexagonal Solid Grid Graphs. The algorithm is a cycle-merging algorithm which starts with a 2-factor for the grid graph and progressively merges cycles until the 2-factor has one component (a Hamiltonian cycle) or until further merging is impossible. Many of the ideas appear relevant to hexagonal grid graphs as well, but a direct translation of the algorithm from solid square grid graphs and the relevant correctness proofs to hexagonal grid graphs fails. In order to make this approach work, some new insight seems necessary. Nevertheless, having struggled with this problem for some time, we believe the conjecture to be correct: Hamiltonicity of Hexagonal Solid Grid Graphs
can probably be solved in polynomial time.
This research was initiated during the open problem sessions of MIT’s graduate class 6.890: Algorithmic Lower Bounds, Fall 2014. We thank the other participants of those sessions—in particular, Quanquan Liu and Yun William Yu—for helpful discussions and for providing a stimulating research environment.
-  Daniel Andersson. Hashiwokakero is NP-complete. Information Processing Letters, 109(19):1145–1146, 2009.
-  Esther M. Arkin, Michael A. Bender, Erik D. Demaine, Sándor P. Fekete, Joseph S. B. Mitchell, and Saurabh Sethia. Optimal covering tours with turn costs. SIAM Journal on Computing, 35(3):531–566, 2005.
-  Esther M. Arkin, Sándor P. Fekete, Kamrul Islam, Henk Meijer, Joseph S. B. Mitchell, Yurai Núñez-Rodríguez, Valentin Polishchuk, David Rappaport, and Henry Xiao. Not being (super)thin or solid is hard: A study of grid hamiltonicity. Computational Geometry: Theory and Applications, 42(6–7):582–605, 2009. Originally published at EuroComb’07.
-  Esther M. Arkin, Sándor P. Fekete, and Joseph S.B. Mitchell. Approximation algorithms for lawn mowing and milling. Computational Geometry: Theory and Applications, 17(1–2):25–50, 2000.
-  Mark Berg and Amirali Khosravi. Optimal binary space partitions in the plane. In Proceedings of the 16th Annual International Conference on Computing and Combinatorics, volume 6196 of Lecture Notes in Computer Science, pages 216–225, Nha Trang, Vietnam, July 2010.
-  Erik D. Demaine. Lecture 8: Hamiltonicity. In MIT 6.890: Algorithmic Lower Bounds. 2014. http://courses.csail.mit.edu/6.890/fall14/lectures/L08.html.
-  Erik D. Demaine and Mikhail Rudoy. Tree-residue vertex-breaking: a new tool for proving hardness. arXiv:1706.07900, June 2017. http://arxiv.org/abs/1706.07900.
-  Michal Forišek. Computational complexity of two-dimensional platform games. In Proceedings of the 5th International Conference on Fun with Algorithms, pages 214–227, Ischia, Italy, June 2010.
-  Alon Itai, Christos H. Papadimitriou, and Jayme Luiz Szwarcfiter. Hamilton paths in grid graphs. SIAM Journal on Computing, 11(4):676–686, November 1982.
-  Richard M. Karp. Reducibility among combinatorial problems. In Proceedings of a Symposium on the Complexity of Computer Computations, pages 85–103, Yorktown Heights, New York, March 1972.
-  Christos H. Papadimitriou and Umesh V. Vazirani. On two geometric problems related to the travelling salesman problem. Journal of Algorithms, 5(2):231–246, 1984.
-  Christopher Umans and William Lenhart. Hamiltonian cycles in solid grid graphs. In Proceedings of the 38th Annual IEEE Conference on Foundations of Computer Science, pages 496–505, Miami, Florida, October 1997.
-  Takayuki Yato. On the NP-completeness of the Slither Link puzzle. IPSJ SiG Notes, AL-74:25–32, 2000.