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 [38].

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 𝑉 ( 𝑅 ) = { 𝜐 1 𝜐 𝑥 𝑚 , 1 𝜐 𝑦 𝑛 } . 𝑅 ( 𝑚 , 𝑛 ) 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 𝑚 , 𝑛 3 , 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 𝑅 ( 3 𝑚 2 , 5 𝑛 4 ) . These alphabet grid graphs are shown in Figure 1, for 𝑚 = 4 and 𝑛 = 3 . An alphabet grid graph is called odd sized if its corresponding rectangular graph is odd sized, and is called even sized otherwise.

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 ( 1 , 1 ) 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:

( F 1 ) 𝑅 ( 𝑚 , 𝑛 ) is a 1-rectangle and either 𝑠 or 𝑡 is not a corner vertex (Figure 2(a)). ( F 2 ) 𝑅 ( 𝑚 , 𝑛 ) is a 2-rectangle and ( 𝑠 , 𝑡 ) is a nonboundary edge, that ( 𝑠 , 𝑡 ) is an edge and it is not on the outer face (Figure 2(b)). ( F 3 ) 𝑅 ( 𝑚 , 𝑛 ) is isomorphic to a 3-rectangle 𝑅 ( 𝑚 , 𝑛 ) such that 𝑠 and 𝑡 are mapped to 𝑠 and 𝑡 and all of the following three conditions hold: ( 1 ) 𝑚 is even, ( 2 ) 𝑠 is black, 𝑡 is white, ( 3 ) 𝑠 𝑦 = 2 and 𝑠 𝑥 < 𝑡 𝑥 (Figure 2(c)) or 𝑠 𝑦 2 and 𝑠 𝑥 < 𝑡 𝑥 1 (Figure 2(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 𝑚 , 𝑛 > 1 .

Lemma 2.3 (see [14]). Any 𝐿 -alphabet grid graph 𝐿 ( 𝑚 , 𝑛 ) ( 𝑚 , 𝑛 3 ) 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.

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 ( i ) an 𝐿 -alphabet grid graph 𝐿 ( 𝑚 , 𝑛 ) is a partition of 𝐿 into two disjoint rectangular grid graphs 𝑅 1 and 𝑅 2 , that is, 𝑉 ( 𝐿 ) = 𝑉 ( 𝑅 1 ) 𝑉 ( 𝑅 2 ) , and 𝑉 ( 𝑅 1 ) 𝑉 ( 𝑅 2 ) = , ( i i ) an 𝐶 -alphabet graph 𝐶 ( 𝑚 , 𝑛 ) is a partition of 𝐶 into a 𝐿 -alphabet graph 𝐿 ( 𝑚 , 𝑛 ) and a rectangular grid graph 𝑅 ( 2 𝑚 2 , 𝑛 ) , that is, 𝑉 ( 𝐶 ) = 𝑉 ( 𝐿 ) 𝑉 ( 𝑅 ( 2 𝑚 2 , 𝑛 ) ) , and 𝑉 ( 𝐿 ) 𝑉 ( 𝑅 ( 2 𝑚 2 , 𝑛 ) ) = , ( i i i ) an 𝐹 -alphabet grid graph 𝐹 ( 𝑚 , 𝑛 ) is a partition of 𝐹 into a 𝐿 -alphabet grid graph 𝐿 ( 𝑚 , 𝑛 ) and a rectangular grid graph 𝑅 ( 2 𝑚 4 , 𝑛 ) (or four rectangular grid graphs 𝑅 1 to 𝑅 4 ) , that is, 𝑉 ( 𝐹 ) = 𝑉 ( 𝐿 ) 𝑉 ( 𝑅 ( 2 𝑚 4 , 𝑛 ) ) and 𝑉 ( 𝐿 ) 𝑉 ( 𝑅 ( 2 𝑚 4 , 𝑛 ) ) = (or 𝑉 ( 𝐹 ) = 𝑉 ( 𝑅 1 ) 𝑉 ( 𝑅 2 ) 𝑉 ( 𝑅 3 ) 𝑉 ( 𝑅 4 ) and 𝑉 ( 𝑅 1 ) 𝑉 ( 𝑅 2 ) 𝑉 ( 𝑅 3 ) 𝑉 ( 𝑅 4 ) = ) , ( i v ) an 𝐸 -alphabet grid graph 𝐸 ( 𝑚 , 𝑛 ) is a partition of 𝐸 into an 𝐹 -alphabet grid graph 𝐹 ( 𝑚 , 𝑛 ) and a rectangular grid graph 𝑅 ( 2 𝑚 2 , 𝑛 ) or a 𝐶 -alphabet grid graph 𝐶 ( 𝑚 , 𝑛 ) and a rectangular grid graph 𝑅 ( 2 𝑚 4 , 𝑛 ) , that is, 𝑉 ( 𝐸 ) = 𝑉 ( 𝐹 ) 𝑉 ( 𝑅 ( 2 𝑚 2 , 𝑛 ) ) , and 𝑉 ( 𝐹 ) 𝑉 ( 𝑅 ( 2 𝑚 2 , 𝑛 ) ) = or 𝑉 ( 𝐸 ) = 𝑉 ( 𝐶 ) 𝑉 ( 𝑅 ( 2 𝑚 4 , 𝑛 ) ) , and 𝑉 ( 𝐶 ) 𝑉 ( 𝑅 ( 2 𝑚 4 , 𝑛 ) ) = .

In the following, two nonincident edges 𝑒 1 and 𝑒 2 are parallel, if each end vertex of 𝑒 1 is adjacent to some end vertex of 𝑒 2 .

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 ( 2 𝑚 2 ) × ( 4 𝑛 4 ) (or ( 2 𝑚 2 ) × ( 3 𝑛 4 ) ), 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 ( 𝑚 , 𝑛 > 3 ). Since 𝑅 𝐹 (or 𝑅 𝐸 ) can be partitioned into three even-sized rectangular grid graphs of 𝑅 ( 2 𝑚 4 , 𝑛 2 ) , 𝑅 ( 2 , 2 𝑛 2 ) , and 𝑅 ( 2 𝑚 2 , 2 𝑛 2 ) (or 𝑅 ( 2 , 3 𝑛 4 ) , 𝑅 ( 2 𝑚 4 , 𝑛 2 ) , and 𝑅 ( 2 𝑚 4 , 𝑛 2 ) ), then they have Hamiltonian cycles by Lemma 2.2. Then combine Hamiltonian cycles on 𝑅 ( 2 𝑚 4 , 𝑛 2 ) , 𝑅 ( 2 , 2 𝑛 2 ) and 𝑅 ( 2 𝑚 2 , 2 𝑛 2 ) (or 𝑅 ( 2 , 3 𝑛 4 ) , 𝑅 ( 2 𝑚 4 , 𝑛 2 ) , and 𝑅 ( 2 𝑚 4 , 𝑛 2 ) ) 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 ( 𝑛 = 3 ). Let 𝑅 𝐹 be three rectangular grid graphs of 𝑅 ( 2 𝑚 4 , 𝑛 2 ) , 𝑅 ( 2 , 2 𝑛 2 ) , and 𝑅 ( 2 𝑚 2 , 2 𝑛 2 ) . We consider the following two subcases.

Subcase   2 . 2 . 1    ( 𝑚 = 3 ) . Let two vertices 𝑣 1 , 𝑣 2 be in 𝑅 ( 2 𝑚 4 , 𝑛 2 ) . Using Algorithm 3 there exist two edges 𝑒 1 , 𝑒 2 such that 𝑒 1 𝑃 or 𝑒 2 𝑃 is on the boundary of 𝐹 ( 𝑚 , 𝑛 ) facing 𝑅 ( 2 𝑚 4 , 𝑛 2 ) , see Figure 5(a). Hence by combining a Hamiltonian path 𝑃 and Hamiltonian cycles in 𝑅 ( 2 , 2 𝑛 2 ) and 𝑅 ( 2 𝑚 2 , 2 𝑛 2 ) and ( 𝑣 1 , 𝑣 2 ) , a Hamiltonian path between 𝑠 and 𝑡 is obtained, see Figures 5(b) and 5(c).
Subcase 2.2.2 ( 𝑚 > 3 ) . For 𝑚 = 4 , let four vertices 𝑣 1 , 𝑣 2 , 𝑣 3 , 𝑣 4 be in 𝑅 ( 2 𝑚 4 , 𝑛 2 ) . Using Algorithm 3 there exist four edges 𝑒 1 , 𝑒 2 , 𝑒 3 , 𝑒 4 such that 𝑒 1 , 𝑒 2 𝑃 , 𝑒 1 , 𝑒 4 𝑃 , 𝑒 3 , 𝑒 4 𝑃 or 𝑒 2 , 𝑒 3 𝑃 are on the boundary of 𝐹 ( 𝑚 , 𝑛 ) facing 𝑅 ( 2 𝑚 4 , 𝑛 2 ) see Figure 6(a). Therefore, by merging ( 𝑣 1 , 𝑣 2 ) and ( 𝑣 3 , 𝑣 4 ) 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 𝑚 = 4 .
Similar to 𝐹 -alphabet grid graphs, Hamiltonian paths can be found in 𝐸 -alphabet grid graphs for 𝑛 = 3 .

procedure 𝐿 _HamiltonianPath ( 𝐿 ( 𝑚 , 𝑛 ) , 𝑠 , 𝑡 )
1:  if   𝐿 can be stripped then
2:   let 𝑆 be a strip of 𝐿 , where 𝑆 is an even-sized rectangular grid graph
3:    𝑃 𝑅 _HamiltonianPath ( 𝐿 𝑆 , 𝑠 , 𝑡 )
4:    𝐷 HamiltonianCycle ( 𝑆 )
5:   return MergeStrip ( 𝑃 , 𝐷 , 𝑠 , 𝑡 )
6:  else
7:   let 𝐿 be split to 𝑅 𝑝 and 𝑅 𝑞
8:    𝑃 1 𝑅 _HamiltonianPath ( 𝑅 𝑝 , 𝑠 , 𝑝 )
9:    𝑃 2 𝑅 _HamiltonianPath ( 𝑅 𝑞 , 𝑞 , 𝑡 )
10:   return MergeSplit ( 𝑃 1 , 𝑃 2 , 𝑝 , 𝑞 )
11:  end  if

  procedure 𝐶 _HamiltonianPath ( 𝐶 ( 𝑚 , 𝑛 ) , 𝑠 , 𝑡 )
1:  if   𝐶 can be stripped then
2:   let 𝑆 be a strip of 𝐶 , where 𝑆 is an even-sized rectangular grid graph 𝑅 ( 2 𝑚 2 , 𝑛 )
3:    𝑃 𝐿 _HamiltonianPath ( 𝐶 𝑆 , 𝑠 , 𝑡 )
4:    𝐷 HamiltonianCycle ( 𝑆 )
5:   return MergeStrip ( 𝑃 , 𝐷 , 𝑠 , 𝑡 )
6:  else
7:   let 𝐶 be split 𝑅 𝑝 and 𝐿 𝑞
8:    𝑃 1 𝑅 _HamiltonianPath ( 𝑅 𝑝 , 𝑠 , 𝑝 )
9:    𝑃 2 𝐿 _HamiltonianPath ( 𝐿 𝑞 , 𝑞 , 𝑡 )
10:  return MergeSplit ( 𝑃 1 , 𝑃 2 , 𝑝 , 𝑞 )
11:  end  if

  procedure 𝐹 _HamiltonianPath ( 𝐹 ( 𝑚 , 𝑛 ) , 𝑠 , 𝑡 )
1:  if   𝐹 can be stripped, where 𝐹 ( 𝑚 , 𝑛 ) is not odd sized and 𝑠 , 𝑡 𝑅 ( 2 𝑚 4 , 𝑛 )   then
2:   let 𝑆 be a strip of 𝐹
3:    𝑃 𝐹 𝑆 _HamiltonianPath ( 𝐹 𝑆 , 𝑠 , 𝑡 )
4:   /* 𝐹 𝑆 is a 𝐿 -alphabet grid graph 𝐿 ( 𝑚 , 𝑛 ) or a rectangular grid graph 𝑅 ( 2 𝑚 4 , 𝑛 )
    */
5:    𝐷 HamiltonianCycle ( 𝑆 )
6:   return MergeStrip ( 𝑃 , 𝐷 , 𝑠 , 𝑡 )
7:  else
8:   if   𝐹 can be stripped, where 𝐹 ( 𝑚 , 𝑛 ) is odd sized and 𝑠 , 𝑡 𝑅 ( 2 𝑚 4 , 𝑛 )   then
9:    let 𝑆 be a strip of 𝐹 , where 𝑆 is three rectangular grid graphs 𝑅 2 , 𝑅 3 and 𝑅 4
10:      𝑃 𝑅 _HamiltonianPath ( 𝑅 1 , 𝑠 , 𝑡 )
11:      𝑐 1 HamiltonianCycle ( 𝑅 3 )
12:      𝑐 2 HamiltonianCycle ( 𝑅 4 )
13:      𝐶 MergeCycles ( 𝑐 1 , 𝑐 2 )
14:      𝐷 HamiltonianCycle ( 𝑅 2 )
15:     return MergeStrip ( 𝑃 , 𝐷 , 𝐶 , 𝑠 , 𝑡 )
16:   else
17:  let 𝐹 be split to 𝑅 𝑝 and 𝐿 𝑞 (or 𝐿 𝑝 and 𝑅 𝑞 )
18:      𝑃 1 𝑅 _HamiltonianPath ( 𝑅 𝑝 , 𝑠 , 𝑝 )
19:      𝑃 2 𝐿 _HamiltonianPath ( 𝐿 𝑞 , 𝑞 , 𝑡 )
20:     return MergeSplit ( 𝑃 1 , 𝑃 2 , 𝑝 , 𝑞 )
21:   end  if
22:  end  if

  procedure 𝐸 _HamiltonianPath ( 𝐸 ( 𝑚 , 𝑛 ) , 𝑠 , 𝑡 )
1:  let 𝑆 be a strip of 𝐸 , where 𝑆 is an even-sized rectangular grid graph 𝑅 ( 2 𝑚 2 , 𝑛 ) or
    𝑅 ( 2 𝑚 4 , 𝑛 )
2:   𝑃 𝐸 𝑆 _HamiltonianPath ( 𝐸 𝑆 , 𝑠 , 𝑡 )
3:  /* 𝐸 𝑆 is a 𝐹 -alphabet grid graph 𝐹 ( 𝑚 , 𝑛 ) or a 𝐶 -alphabet grid graph 𝐶 ( 𝑚 , 𝑛 ) */
4:   𝐷 HamiltonianCycle ( 𝑆 )
5:  return MergeStrip ( 𝑃 , 𝐷 , 𝑠 , 𝑡 )

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 𝑅 ( 2 𝑚 2 , 𝑛 ) and 𝑅 ( 𝑚 , 5 𝑛 4 ) be a separation of 𝐿 ( 𝑚 , 𝑛 ) such that three vertices 𝑣 , 𝑤 , and 𝑢 are in 𝑅 ( 2 𝑚 2 , 𝑛 ) which are connected to 𝑅 ( 𝑚 , 5 𝑛 4 ) . Assume that 𝑠 and 𝑡 are two given vertices of 𝐿 and 𝑠 = 𝑤 and 𝑡 = 𝑡 , if 𝑠 𝑅 ( 2 𝑚 2 , 𝑛 ) let 𝑠 = 𝑠 . If 𝑡 𝑥 > 𝑚 + 1 and ( 𝑅 ( 2 𝑚 2 , 𝑛 ) , 𝑠 , 𝑡 ) satisfies condition (F3), then 𝐿 ( 𝑚 , 𝑛 ) does not have any Hamiltonian path between 𝑠 and 𝑡 .

Proof. Without loss of generality, let 𝑠 and 𝑡 be color compatible. Since 𝑚 , 𝑛 3 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 𝑛 = 3 . Assume that 𝑅 ( 2 𝑚 2 , 𝑛 ) satisfies condition (F3). We show that there is no Hamiltonian path in 𝐿 ( 𝑚 , 𝑛 ) between 𝑠 and 𝑡 . Assume to the contrary that 𝐿 ( 𝑚 , 𝑛 ) has a Hamiltonian path 𝑃 . Since 𝑛 = 3 there are exactly three vertices 𝑣 , 𝑤 and 𝑢 in 𝑅 ( 2 𝑚 2 , 𝑛 ) which are connected to 𝑅 ( 𝑚 , 5 𝑛 4 ) , as shown in Figure 7(a). Then the following cases are possible. Case 1. 𝑡 𝑅 ( 2 𝑚 2 , 𝑛 ) and 𝑠 𝑅 ( 2 𝑚 2 , 𝑛 ) . The following subcases are possible for the Hamiltonian path 𝑃 .Subcase 1.1. The Hamiltonian path 𝑃 of 𝐿 ( 𝑚 , 𝑛 ) that starts from 𝑠 may enter to 𝑅 ( 2 𝑚 2 , 𝑛 ) for the first time through one of the vertices 𝑣 , 𝑤 , or 𝑢 , pass through all the vertices of 𝑅 ( 2 𝑚 2 , 𝑛 ) and end at 𝑡 , see Figure 7(b). This case is not possible because we assumed that 𝑅 ( 2 𝑚 2 , 𝑛 ) satisfies (F3), in this case 𝑡 = 𝑡 and 𝑠 = 𝑤 .Subcase 1.2. The Hamiltonian path 𝑃 of 𝐿 ( 𝑚 , 𝑛 ) may enter to 𝑅 ( 2 𝑚 2 , 𝑛 ) , 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 𝑅 ( 2 𝑚 2 , 𝑛 ) are called 𝑃 1 and 𝑃 2 , 𝑃 1 from 𝑣 to 𝑢 ( 𝑣 to 𝑤 or 𝑢 to 𝑤 ) and 𝑃 2 from 𝑤 to 𝑡 ( 𝑢 to 𝑡 or 𝑣 to 𝑡 ). This case is not also possible because the size of 𝑃 1 is odd (even) and the size of 𝑃 2 is even (odd), then | 𝑃 1 + 𝑃 2 | is odd while 𝑅 ( 2 𝑚 2 , 𝑛 ) is even, which is a contradiction.Case 2 ( 𝑠 , 𝑡 𝑅 ( 2 𝑚 2 , 𝑛 ) ). The following cases may be considered. Subcase 2.1. The Hamiltonian path 𝑃 of 𝐿 ( 𝑚 , 𝑛 ) which starts from 𝑠 may pass through some vertices of 𝑅 ( 2 𝑚 2 , 𝑛 ) , leave 𝑅 ( 2 𝑚 2 , 𝑛 ) at 𝑣 (or 𝑢 ), then passes through all the vertices of 𝑅 ( 𝑚 , 5 𝑛 4 ) and reenter to 𝑅 ( 2 𝑚 2 , 𝑛 ) at 𝑤 go to 𝑢 (or 𝑣 ) and pass through all the remaining vertices of 𝑅 ( 2 𝑚 2 , 𝑛 ) and end at 𝑡 . In this case by connecting 𝑣 (or 𝑢 ) to 𝑤 we obtain a Hamiltonian path from 𝑠 to 𝑡 in 𝑅 ( 2 𝑚 2 , 𝑛 ) , which contradicts the assumption that 𝑅 ( 2 𝑚 2 , 𝑛 ) satisfies ( 𝐹 3 ) .Subcase 2.2. The Hamiltonian path 𝑃 of 𝐿 ( 𝑚 , 𝑛 ) which starts from 𝑠 may leave 𝑅 ( 2 𝑚 2 , 𝑛 ) at 𝑣 (or 𝑢 ), then pass through all the vertices of 𝑅 ( 𝑚 , 5 𝑛 4 ) and reenter to 𝑅 ( 2 𝑚 2 , 𝑛 ) at 𝑢 (or 𝑣 ) go to 𝑤 and pass through all the remaining vertices of 𝑅 ( 2 𝑚 2 , 𝑛 ) and end at 𝑡 . In this case, two parts of 𝑃 reside in 𝑅 ( 2 𝑚 2 , 𝑛 ) . The part 𝑃 1 starts from 𝑠 ends at 𝑣 (or 𝑢 ), and the part 𝑃 2 starts from 𝑢 (or 𝑣 ) ends at 𝑡 . The size of 𝑃 1 is even and the size of 𝑃 2 is odd while the size of 𝑅 ( 2 𝑚 2 , 𝑛 ) is even, which is a contradiction.Subcase 2.3. Another case that may arise is that the Hamiltonian path 𝑃 of 𝐿 ( 𝑚 , 𝑛 ) starts from 𝑠 leaves 𝑅 ( 2 𝑚 2 , 𝑛 ) at 𝑤 and reenters 𝑅 ( 2 𝑚 2 , 𝑛 ) 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.

Lemma 3.6. Let 𝐿 ( 𝑚 , 𝑛 ) and 𝑅 ( 2 𝑚 2 , 𝑛 ) be a separation of 𝐶 ( 𝑚 , 𝑛 ) such that three vertices 𝑣 1 , 𝑤 1 , and 𝑢 1 are in 𝑅 ( 2 𝑚 2 , 𝑛 ) which are connected to 𝐿 ( 𝑚 , 𝑛 ) and 𝑥 𝐿 ( 𝑚 , 𝑛 ) is a adjacent vertex to 𝑤 1 . Assume that 𝑠 and 𝑡 are two given vertices of 𝐶 . If 𝑡 𝐿 ( 𝑚 , 𝑛 ) and 𝑠 𝑅 ( 2 𝑚 2 , 𝑛 ) , let 𝑠 = 𝑥 and 𝑠 = 𝑤 1 , 𝑡 = 𝑠 , respectively. If ( 𝐿 ( 𝑚 , 𝑛 ) , 𝑠 , 𝑡 ) does not have a Hamiltonian path or ( 𝑅 ( 2 𝑚 2 , 𝑛 ) , 𝑠 , 𝑡 ) 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.

Lemma 3.7. Let 𝐿 ( 𝑚 , 𝑛 ) and 𝑅 ( 2 𝑚 4 , 𝑛 ) be a separation of 𝐹 ( 𝑚 , 𝑛 ) such that three vertices 𝑣 1 , 𝑤 1 , and 𝑢 1 are in 𝑅 ( 2 𝑚 4 , 𝑛 ) which are connected to 𝐿 ( 𝑚 , 𝑛 ) and 𝑥 𝐿 ( 𝑚 , 𝑛 ) is an adjacent vertex to 𝑤 1 . Assume that 𝑠 and 𝑡 are two given vertices of 𝐹 . If 𝑠 , 𝑡 𝑅 ( 2 𝑚 4 , 𝑛 ) let 𝑠 = 𝑠 and 𝑡 = 𝑡 ; if 𝑠 (or 𝑡 ) 𝑅 ( 2 𝑚 4 , 𝑛 ) let 𝑠 = 𝑤 1 and 𝑡 = 𝑠 (or 𝑠 = 𝑤 1 and 𝑡 = 𝑡 ); if 𝑡 (or 𝑠 ) 𝐿 ( 𝑚 , 𝑛 ) let 𝑠 = 𝑥 ( ( 𝑡 = 𝑠 , 𝑠 = 𝑥 ) or ( 𝑡 = 𝑥 ) ) . If ( 𝐿 ( 𝑚 , 𝑛 ) , 𝑠 , 𝑡 ) does not have a Hamiltonian path or ( 𝑅 ( 2 𝑚 4 , 𝑛 ) , 𝑠 , 𝑡 ) 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.

Lemma 3.8. Let 𝐹 ( 𝑚 , 𝑛 ) and 𝑅 ( 2 𝑚 2 , 𝑛 ) or 𝐶 ( 𝑚 , 𝑛 ) and 𝑅 ( 2 𝑚 4 , 𝑛 ) 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.

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 ( 𝑅 ( 2 𝑚 2 , 𝑛 ) , 𝑠 , 𝑡 ) does not satisfy the condition (F3); 𝑃 ( 𝐶 ( 𝑚 , 𝑛 ) , 𝑠 , 𝑡 ) is called acceptable if 𝑃 ( 𝐿 ( 𝑚 , 𝑛 ) , 𝑠 , 𝑡 ) is acceptable and ( 𝑅 ( 2 𝑚 2 , 𝑛 ) , 𝑠 , 𝑡 ) does not satisfy the condition (F3); 𝑃 ( 𝐹 ( 𝑚 , 𝑛 ) , 𝑠 , 𝑡 ) is called acceptable if 𝑃 ( 𝐿 ( 𝑚 , 𝑛 ) , 𝑠 , 𝑡 ) is acceptable and ( 𝑅 ( 2 𝑚 4 , 𝑛 ) , 𝑠 , 𝑡 ) 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: ( 1 ) 𝑆 is even sized and: ( i ) 𝑆 is a rectangular grid graph; where 𝐴 ( 𝑚 , 𝑛 ) is a 𝐿 -alphabet grid graph 𝐿 ( 𝑚 , 𝑛 ) , a 𝐶 -alphabet grid graph 𝐶 ( 𝑚 , 𝑛 ) and an 𝐸 -alphabet grid graph 𝐸 ( 𝑚 , 𝑛 ) ; ( i i ) 𝑆 is a 𝐿 -alphabet graph 𝐿 ( 𝑚 , 𝑛 ) , a rectangular graph 𝑅 ( 2 𝑚 4 , 𝑛 ) or three rectangular grid graphs 𝑅 1 𝑅 3 ; where 𝐴 ( 𝑚 , 𝑛 ) is a 𝐹 -alphabet grid graph. ( 2 ) 𝑆 and 𝐴 𝑆 is a separation of 𝐴 ; ( 3 ) 𝑠 , 𝑡 𝐴 𝑆 ; ( 4 ) 𝑃 ( 𝐴 𝑆 , 𝑠 , 𝑡 ) 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 ( 1 ) 𝐿 into 𝑅 𝑝 and 𝑅 𝑞 such that ( i ) 𝑠 , 𝑝 𝑅 𝑝 and 𝑃 ( 𝑅 𝑝 , 𝑠 , 𝑝 ) is acceptable, ( i i ) 𝑞 , 𝑡 𝑅 𝑞 and 𝑃 ( 𝑅 𝑞 , 𝑞 , 𝑡 ) is acceptable. ( 2 ) 𝐹 (resp., 𝐶 ) into 𝑅 𝑝 and 𝐿 𝑞 or 𝑅 𝑞 and 𝐿 𝑝 ( resp., 𝐿 𝑞 and 𝑅 𝑝 ) such that ( i ) 𝑠 , 𝑝 𝑅 𝑝 and 𝑃 ( 𝑅 𝑝 , 𝑠 , 𝑝 ) is acceptable (or 𝑠 , 𝑝 𝐿 𝑝 and 𝑃 ( 𝐿 𝑝 , 𝑠 , 𝑝 ) is acceptable), ( i i ) 𝑞 , 𝑡 𝐿 𝑞 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).

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).

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 𝑅 ( 2 𝑚 2 , 𝑛 ) , 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 𝑅 ( 2 𝑚 4 , 𝑛 ) , then it has a Hamiltonian cycle by Lemma 2.2.

Case 2 ( 𝑠 , 𝑡 𝐹 𝑆 , where 𝐹 𝑆 is 𝑅 ( 2 𝑚 4 , 𝑛 ) ). We construct a Hamiltonian path between 𝑠 and 𝑡 in 𝑅 ( 2 𝑚 4 , 𝑛 ) 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 𝑅 ( 2 𝑚 4 , 𝑛 ) , a Hamiltonian path between 𝑠 and 𝑡 for 𝐹 ( 𝑚 , 𝑛 ) is obtained, see Figures 13(a) and 13(b).

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

Case 4 ( 𝑠 𝑅 𝑝 and 𝑡 𝐿 𝑞 or 𝑠 𝐿 𝑝 and 𝑞 𝑅 𝑞 ). A Hamiltonian path can be found as Figure 15.

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 𝑅 ( 2 𝑚 2 , 𝑛 ) (or 𝑅 ( 2 𝑚 4 , 𝑛 ) ) , 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 ( i ) ( 𝑅 𝑝 , 𝑠 , 𝑝 ) and ( 𝑅 𝑞 , 𝑞 , 𝑡 ) , where 𝐴 ( 𝑚 , 𝑛 ) is 𝐿 -alphabet grid graph 𝐿 ( 𝑚 , 𝑛 ) , ( i i ) ( 𝐿 𝑞 , 𝑞 , 𝑡 ) and ( 𝑅 𝑝 , 𝑠 , 𝑝 ) , where 𝐴 ( 𝑚 , 𝑛 ) is 𝐶 -alphabet grid graph 𝐶 ( 𝑚 , 𝑛 ) , ( i i i ) ( 𝑅 𝑝 , 𝑠 , 𝑝 ) 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 𝑒 1 and 𝑒 2 , 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 𝑂 ( 1 ) . Then we solve the subproblems in linear time using the linear time algorithm in [12]. Then the results are merged in time 𝑂 ( 1 ) 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.