Abstract
The Hamiltonian path problem for general grid graphs is known to be NP-complete. In this paper, we give necessary and sufficient conditions for the existence of Hamiltonian paths in L-alphabet, C-alphabet, F-alphabet, and E-alphabet grid graphs. We also present linear-time algorithms for finding Hamiltonian paths in these graphs.
1. Introduction
Hamiltonian path in a graph is a simple path that visits every vertex exactly once. The problem of deciding whether a given graph has a Hamiltonian path is a well-known NP-complete problem and has many applications [1, 2]. However, for some special classes of graphs polynomial-time algorithms have been found. For more related results on Hamiltonian paths in general graphs see [3–8].
Rectangular grid graphs first appeared in [9], where Luccio and Mugnia tried to solve the Hamiltonian path problem. Itai et al. [10] gave necessary and sufficient conditions for the existence of Hamiltonian paths in rectangular grid graphs and proved that the problem for general grid graphs is NP-complete. Also, the authors in [11] presented sufficient conditions for a grid graph to be Hamiltonian and proved that all finite grid graphs of positive width have Hamiltonian line graphs. Later, Chen et al. [12] improved the algorithm of [10] and presented a parallel algorithm for the problem in mesh architecture. Also there is a polynomial-time algorithm for finding Hamiltonian cycle in solid grid graphs [13]. Recently, Salman [14] introduced alphabet grid graphs and determined classes of alphabet grid graphs which contain Hamiltonian cycles. More recently, Islam et al. [15] showed that the Hamiltonian cycle problem in hexagonal grid graphs is NP-complete. Also, Gordon et al. [16] proved that all connected, locally connected triangular grid graphs are Hamiltonian, and gave a sufficient condition for a connected graph to be fully cycle extendable and also showed that the Hamiltonian cycle problem for triangular grid graphs is NP-complete. Nandi et al. [17] gave methods to find the domination numbers of cylindrical grid graphs. Moreover, Keshavarz-Kohjerdi et al. [18, 19] gave sequential and parallel algorithms for the longest path problem in rectangular grid graphs.
In this paper, we obtain necessary and sufficient conditions for the existence of a Hamiltonian path in -alphabet, -alphabet, -alphabet, and -alphabet grid graphs. Also, we present linear-time algorithms for finding such a Hamiltonian path in these graphs. Solving the Hamiltonian path problem for alphabet grid graphs may arise results that can help in solving the problem for general solid grid graphs. The alphabet grid graphs that are considered in this paper have similar properties that motivate us to investigate them together. Other classes of alphabet grid graphs have enough differences that will be studied in a separate work.
2. Preliminaries
Some previously established results about the Hamiltonian path problem which plays an important role in this paper are summarized in this section.
The two-dimensional integer grid is an infinite graph with vertex set of all points of the Euclidean plane with integer coordinates. In this graph, there is an edge between any two vertices of unit distance. For a vertex of this graph, let and denote and coordinates of its corresponding point. A grid graph is a finite vertex-induced subgraph of the two-dimensional integer grid. In a grid graph , each vertex has degree of at most four. A rectangular grid graph (or for short) is a grid graph whose vertex set is . is called an -rectangle. A solid grid graph is a grid graph without holes.
By [10], a vertex is colored white if is even, and is colored black otherwise. The size of is defined to be . is called odd sized if is odd, and is called even sized otherwise. Two different vertices and in are called color compatible if either both and are white and is odd sized, or and have different colors and is even sized.
An alphabet grid graph is a finite vertex-induced subgraph of the rectangular grid graph of a certain type, as follows. For , an -alphabet grid graph (or for short), -alphabet grid graph (or for short), -alphabet grid graph (or for short), and -alphabet grid graph (or for short) are subgraphs of . These alphabet grid graphs are shown in Figure 1, for and . An alphabet grid graph is called odd sized if its corresponding rectangular graph is odd sized, and is called even sized otherwise.

(a)

(b)

(c)

(d)

(e)
In the following by we mean a grid graph . Let denote the problem of finding a Hamiltonian path between vertices and in grid graph , and let denote the grid graph with two specified distinct vertices and of it. Where is a rectangular grid graph, -alphabet, -alphabet, -alphabet, or -alphabet grid graph. is Hamiltonian if there is a Hamiltonian path between and in . In this paper, without loss of generality we assume and . In the figures, we assume that is the coordinates of the vertex in the lower left corner.
An even-sized rectangular grid graph contains the same number of black and white vertices. Hence, the two end vertices of any Hamiltonian path in the graph must have different colors. Similarly, in an odd sized rectangular grid graph the number of white vertices is one more than the number of black vertices. Therefore, the two end vertices of any Hamiltonian path in such a graph must be white. Hence, the color compatibility of and is a necessary condition for to be Hamiltonian. Furthermore, Itai et al. [10] showed that if one of the following conditions hold, then is not Hamiltonian:
is a 1-rectangle and either or is not a corner vertex (Figure 2(a)). is a 2-rectangle and is a nonboundary edge, that is an edge and it is not on the outer face (Figure 2(b)). is isomorphic to a 3-rectangle such that and are mapped to and and all of the following three conditions hold: is even, is black, is white, and (Figure 2(c)) or and (Figure 2(d)).

(a)

(b)

(c)

(d)
A Hamiltonian path problem is called acceptable if and are color compatible and does not satisfy any of conditions (F1), (F2), and (F3).
The following theorem has been proved in [10].
Theorem 2.1. Let be a rectangular grid graph and and be two distinct vertices. Then is Hamiltonian if and only if is acceptable.
Lemma 2.2 (see [12]). has a Hamiltonian cycle if and only if it is even-sized and .
Lemma 2.3 (see [14]). Any -alphabet grid graph has a Hamiltonian cycle if and only if is even.
Figure 3 shows a Hamiltonian cycle for an even-sized rectangular grid graph and -alphabet graph ), found by Lemmas 2.2 and 2.3, respectively. Each Hamiltonian cycle found by these lemmas contains all boundary edges on three sides of the rectangular graph and four sides of the -alphabet grid graph. This shows that for an even-sized rectangular graph and -alphabet graph , we can always find a Hamiltonian cycle, such that it contains all boundary edges, except of exactly one side of and two side of which contains an even number of vertices.

(a)

(b)
3. Necessary and Sufficient Conditions
In this section, we give necessary and sufficient conditions for the existence of a Hamiltonian path in -alphabet, -alphabet, -alphabet, and -alphabet grid graphs.
Definition 3.1. A separation of an -alphabet grid graph is a partition of into two disjoint rectangular grid graphs and , that is, , and ,an -alphabet graph is a partition of into a -alphabet graph and a rectangular grid graph , that is, , and ,an -alphabet grid graph is a partition of into a -alphabet grid graph and a rectangular grid graph (or four rectangular grid graphs to , that is, and (or and ,an -alphabet grid graph is a partition of into an -alphabet grid graph and a rectangular grid graph or a -alphabet grid graph and a rectangular grid graph , that is, , and or , and .
In the following, two nonincident edges and are parallel, if each end vertex of is adjacent to some end vertex of .
Lemma 3.2. Let be an -alphabet, -alphabet, -alphabet, or -alphabet grid graph and be the smallest rectangular grid graph that includes . If is Hamiltonian, then is also Hamiltonian.
Proof. We break the proof into two cases.Case 1 ( is an -alphabet or -alphabet grid graph). Let be a Hamiltonian path in (or ) that is found by Algorithm 1 (or 2). Since (or ) is an even-sized rectangular grid graph of (or ), then by Lemma 2.2 it has a Hamiltonian cycle (i.e., we can find a Hamiltonian cycle of (or ), such that it contains all edges of (or ) that are parallel to some edge of ). Using two parallel edges of and the Hamiltonian cycle of (or ) such as two darkened edges of Figure 4(a), we can combine them as illustrated in Figure 4(b) and obtain a Hamiltonian path for .Case 2. is an -alphabet or -alphabet grid graph. Let be a Hamiltonian path in (or ) that is found by Algorithm 3 (or 4). We consider the following cases.
Subcase 2.1 (). Since (or ) can be partitioned into three even-sized rectangular grid graphs of , , and (or , and ), then they have Hamiltonian cycles by Lemma 2.2. Then combine Hamiltonian cycles on , and (or , and ) to be a large Hamiltonian cycle and then using two parallel edges of and the Hamiltonian cycle of (or ), we can obtain a Hamiltonian path for .Subcase 2.2 (). Let be three rectangular grid graphs of , , and . We consider the following two subcases.
Subcase . Let two vertices be in . Using Algorithm 3 there exist two edges such that or is on the boundary of facing , see Figure 5(a). Hence by combining a Hamiltonian path and Hamiltonian cycles in and and , a Hamiltonian path between and is obtained, see Figures 5(b) and 5(c).
Subcase 2.2.2 . For , let four vertices be in . Using Algorithm 3 there exist four edges such that , , or are on the boundary of facing see Figure 6(a). Therefore, by merging and to these edges and Hamiltonian cycles in we obtain a Hamiltonian path for , see Figure 6(b). For other values of , the proof is similar to that of .
Similar to -alphabet grid graphs, Hamiltonian paths can be found in -alphabet grid graphs for .
|
|
|
|

(a)

(b)

(a)

(b)

(c)

(a)

(b)
Combining Lemma 3.2 and Theorem 2.1 the following corollary is trivial.
Corollary 3.3. Let be an -alphabet, -alphabet, -alphabet, or -alphabet grid graph and be the smallest rectangular grid graph that includes . If is Hamiltonian, then and must be color compatible in .
Therefore, the color compatibility of and in is a necessary condition for , , , and to be Hamiltonian.
Definition 3.4. The length of a path in a grid graph means the number of vertices of the path. In any grid graph, the length of any path between two same-colored vertices is odd and the length of any path between two different-colored vertices is even.
Lemma 3.5. Let and be a separation of such that three vertices , , and are in which are connected to . Assume that and are two given vertices of and and , if let . If and satisfies condition (F3), then does not have any Hamiltonian path between and .
Proof. Without loss of generality, let and be color compatible. Since and by Theorem 2.1 a rectangular grid graph does not have a Hamiltonian path only in condition (F3), so it suffices to prove the lemma for the case . Assume that satisfies condition (F3). We show that there is no Hamiltonian path in between and . Assume to the contrary that has a Hamiltonian path . Since there are exactly three vertices and in which are connected to , as shown in Figure 7(a). Then the following cases are possible. Case 1. and . The following subcases are possible for the Hamiltonian path .Subcase 1.1. The Hamiltonian path of that starts from may enter to for the first time through one of the vertices , or , pass through all the vertices of and end at , see Figure 7(b). This case is not possible because we assumed that satisfies (F3), in this case and .Subcase 1.2. The Hamiltonian path of may enter to , pass through some vertices of it, then leave it and enter it again and pass through all the remaining vertices of it and finally end at . In this case, two subpaths of which are in are called and , from to ( to or to ) and from to ( to or to ). This case is not also possible because the size of is odd (even) and the size of is even (odd), then is odd while is even, which is a contradiction.Case 2 (). The following cases may be considered. Subcase 2.1. The Hamiltonian path of which starts from may pass through some vertices of , leave at (or ), then passes through all the vertices of and reenter to at go to (or and pass through all the remaining vertices of and end at . In this case by connecting (or ) to we obtain a Hamiltonian path from to in , which contradicts the assumption that satisfies .Subcase 2.2. The Hamiltonian path of which starts from may leave at (or ), then pass through all the vertices of and reenter to at (or ) go to and pass through all the remaining vertices of and end at . In this case, two parts of reside in . The part starts from ends at (or ), and the part starts from (or ) ends at . The size of is even and the size of is odd while the size of is even, which is a contradiction.Subcase 2.3. Another case that may arise is that the Hamiltonian path of starts from leaves at and reenters at (or ) and then goes to . But in this case vertex (or ) cannot be in , which is a contradiction. Thus, the proof of Lemma 3.5 is completed.

(a)

(b)
Lemma 3.6. Let and be a separation of such that three vertices , , and are in which are connected to and is a adjacent vertex to . Assume that and are two given vertices of . If and , let and , , respectively. If does not have a Hamiltonian path or satisfies condition (F3), then does not have a Hamiltonian path between and .
Proof. The proof is similar to the proof of Lemma 3.5, for more details see Figure 8.

(a)

(b)

(c)
Lemma 3.7. Let and be a separation of such that three vertices , , and are in which are connected to and is an adjacent vertex to . Assume that and are two given vertices of . If let and ; if (or ) let and (or and ); if (or ) let or . If does not have a Hamiltonian path or satisfies the condition (F3), then does not have a Hamiltonian path between and .
Proof. By a similar way as used in the Lemma 3.5 we can prove this lemma; for more details see Figure 9.

(a)

(b)

(c)
Lemma 3.8. Let and or and be a separation of and and be two given vertices in . If or does not have a Hamiltonian path, then does not have a Hamiltonian path between and .
Proof. The proof is similar to the proof of Lemma 3.5, for more details see Figure 10.

(a)

(b)
From Corollary 3.3 and Lemmas 3.5, 3.6, 3.7, and 3.8, a Hamiltonian path problem is called acceptable if and are color-compatible and does not satisfy the condition (F3); is called acceptable if is acceptable and does not satisfy the condition (F3); is called acceptable if is acceptable and does not satisfy the condition (F3); is called acceptable if and are acceptable.
Theorem 3.9. Let be an -alphabet, -alphabet, -alphabet or -alphabet grid graph. If is Hamiltonian, then is acceptable.
Now, we show that all acceptable Hamiltonian path problems have solutions by introducing algorithms to find Hamiltonian paths (sufficient conditions). Our algorithms are based on a divide-and-conquer approach. In the dividing phase we use two operations stirp and split which are defined in the following.
Definition 3.10. A subgraph of an -alphabet, -alphabet, -alphabet or -alphabet grid graph A strips a Hamiltonian path problem , if all of the following four conditions hold: is even sized and: is a rectangular grid graph; where is a -alphabet grid graph , a -alphabet grid graph and an -alphabet grid graph ; is a -alphabet graph , a rectangular graph or three rectangular grid graphs ; where is a -alphabet grid graph. and is a separation of ;; is acceptable.
Definition 3.11. Let be a Hamiltonian path problem and be an edge of , where is an -alphabet, -alphabet, or -alphabet grid graph. Then we say splits if there exists a separation of into and such that and is acceptable, and is acceptable. (resp., ) into and or and resp., and such that and is acceptable (or and is acceptable), and is acceptable (or and is acceptable).In the following, we describe for each alphabet class how the solutions of the subgraphs are merged to construct a Hamiltonian path for the given input graph.
3.1. Hamiltonian Paths in -Alphabet Grid Graphs
Since an -alphabet graph may be partitioned into two rectangular grid graphs, then the possible cases for vertices and are as follows.
Case 1 (). Assume that has a Hamiltonian path by the algorithm of [12], where is a rectangular gird graph. Since is an even-sized rectangular gird graph, then it has Hamiltonian cycle by Lemma 2.2; see Figure 11(a). Therefore, a Hamiltonian path for can be obtained by merging and the Hamiltonian cycle of as shown in Figure 11(b).

(a)

(b)

(c)
Case 2 ( and ). In this case, we construct Hamiltonian paths in and between and , respectively; see Figure 12(a). Then a Hamiltonian path for can be obtained by connecting two vertices and as shown in Figure 12(b).

(a)

(b)

(c)
3.2. Hamiltonian Paths in -Alphabet Grid Graphs
Similar to -alphabet graphs since a -alphabet graph may be partitioned into a -alphabet graph and a rectangular grid graph , then the possible cases for vertices and are as follows.
Case 1 (). Assume that has a Hamiltonian path by the Algorithm 1, where is an -alphabet gird graph . Hence, a Hamiltonian path for can be obtained by merging and the Hamiltonian cycle of as shown in Figure 11(c).
Case 2 ( and ). A Hamiltonian path can be found as Figure 12(c).
3.3. Hamiltonian Paths in -Alphabet Grid Graphs
For -alphabet grid graphs, we consider the following cases.
Case 1 (, where is ). In this case, we construct a Hamiltonian path between and in by the Algorithm 1. Since is an even-sized rectangular grid graph , then it has a Hamiltonian cycle by Lemma 2.2.
Case 2 (, where is ). We construct a Hamiltonian path between and in by the algorithm in [12]. Since is an even-sized -alphabet grid graph , then it has a Hamiltonian cycle by Lemma 2.3.
In two cases, by combining the Hamiltonian cycle of and the Hamiltonian path of or , a Hamiltonian path between and for is obtained, see Figures 13(a) and 13(b).

(a)

(b)

(c)
Case 3 ( is odd sized and , where is ). In this case, and must be white. Since is even sized, then and are not color compatible in , see Figure 14(a). Therefore, we repartition into four rectangular grid graphs , , , and , such that and , , and are even sized, see Figure 14(b) where the dotted lines represent the strip. In this case, by combining the Hamiltonian cycles in , , and and the Hamiltonian path of , a Hamiltonian path between and for is obtained, see Figure 14(c).

(a)

(b)

(c)
Case 4 ( and or and ). A Hamiltonian path can be found as Figure 15.

(a)

(b)

(c)
3.4. Hamiltonian Paths in -Alphabet Grid Graphs
A Hamiltonian path for an -alphabet grid graph can be found using striping. So we strip such that , where is -alphabet or -alphabet . In this case, we construct a Hamiltonian path in (or by Algorithm 3 (or 4). Since is an even-sized rectangular grid graph (or , then it has a Hamiltonian cycle by Lemma 2.2. By combining the Hamiltonian cycle of and the Hamiltonian path of or , a Hamiltonian path between and for is obtained, see Figure 13(c).
Thus, we have the following lemmas.
Lemma 3.12. Let be an acceptable Hamiltonian path problem, and strips it, where is an -alphabet, -alphabet, -alphabet, or -alphabet grid graph. If has a Hamiltonian path between and , then has a Hamiltonian path between and .
Lemma 3.13. Let be an edge which splits . If and, where is-alphabet grid graph, and , where is -alphabet grid graph , and or and , where is -alphabet grid graph ,
have a Hamiltonian path between and , then also has a Hamiltonian path between and .
Since all the proofs presented in this section were constructive, they give us algorithms for finding a Hamiltonian path in -alphabet, -alphabet, -alphabet, and -alphabet grid graphs. The pseudo-codes of the algorithms are given in Algorithms 1, 2, 3, and 4. In these algorithms, HamiltonianCycle is the procedure that finds the Hamiltonian cycle of a strip based on Lemma 2.2 or Lemma 2.3, MergeStrip is a procedure that merges a path and a cycle(s), MergeCycles is a procedure that combine two Hamiltonian cycles using two parallel edges of and , MergeSplit is a procedure that connects two paths by simply adding an edge between their end vertices and , and R_HamiltonianPath is a procedure that finds a Hamiltonian path in a rectangular grid graph by the algorithm in [12]. The algorithm first checks if the input graph can be stripped, then strips by , and recursively finds a Hamiltonian path of . Otherwise, if can be split, then splits into and ( and or and ), and recursively finds Hamiltonian paths of and ( and or and ). Then these two Hamiltonian paths are merged into a single path, where is an -alphabet, -alphabet, -alphabet, or -alphabet grid graph.
From Theorem 3.9 and Lemmas 3.12 and 3.13 the following theorem holds.
Theorem 3.14. Let be an -alphabet, -alphabet, -alphabet, or -alphabet grid graph, and and be two distinct vertices of it. has Hamiltonian path if and only if is acceptable.
Theorem 3.14 provides necessary and sufficient conditions for the existence of Hamiltonian paths in -alphabet, -alphabet, -alphabet and -alphabet grid graphs.
Combining Theorem 3.14 and the Algorithms 1, 2, 3, and 4 we arrive at the main result.
Theorem 3.15. In -alphabet, -alphabet, -alphabet or -alphabet grid graphs, a Hamiltonian path between any two vertices and can be found in linear time.
Proof. The algorithms divide the problem into some rectangular grid graphs in . Then we solve the subproblems in linear time using the linear time algorithm in [12]. Then the results are merged in time using the method proposed in [12].
4. Conclusion and Future Work
In this paper, we presented linear time algorithms for finding a Hamiltonian path in -alphabet, -alphabet, -alphabet, and -alphabet grid graphs between any two given vertices. Since the Hamiltonian path problem is NP-complete in general grid graphs, it remains open if the problem is polynomially solvable in solid grid graphs.