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 L-alphabet, C-alphabet, F-alphabet, and E-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 G∞ 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 v of this graph, let vx and vy denote x and y coordinates of its corresponding point. A grid graph G is a finite vertex-induced subgraph of the two-dimensional integer grid. In a grid graph G, each vertex has degree of at most four. A rectangular grid graph R(m,n) (or R for short) is a grid graph whose vertex set is V(R)={υ∣1≤υx≤m,1≤υy≤n}. R(m,n) is called an n-rectangle. A solid grid graph is a grid graph without holes.
By [10], a vertex v is colored white if υx+υy is even, and is colored black otherwise. The size of R(m,n) is defined to be mn. R(m,n) is called odd sized if mn is odd, and is called even sized otherwise. Two different vertices υ and υ′ in R(m,n) are called color compatible if either both υ and υ′ are white and R(m,n) is odd sized, or υ and υ′ have different colors and R(m,n) 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 m,n≥3, an L-alphabet grid graph L(m,n) (or L for short), C-alphabet grid graph C(m,n) (or C for short), F-alphabet grid graph F(m,n) (or F for short), and E-alphabet grid graph E(m,n) (or E for short) are subgraphs of R(3m-2,5n-4). These alphabet grid graphs are shown in Figure 1, for m=4 and n=3. An alphabet grid graph is called odd sized if its corresponding rectangular graph is odd sized, and is called even sized otherwise.
(a) A rectangular grid graph R(10,11), (b) an L-alphabet grid graph L(4,3), (c) an C-alphabet grid graph C(4,3), (d) an F-alphabet grid graph F(4,3), (e) an E-alphabet grid graph E(4,3).
In the following by A(m,n) we mean a grid graph A(m,n). Let P(A(m,n),s,t) denote the problem of finding a Hamiltonian path between vertices s and t in grid graph A(m,n), and let (A(m,n),s,t) denote the grid graph A(m,n) with two specified distinct vertices s and t of it. Where A is a rectangular grid graph, L-alphabet, C-alphabet, F-alphabet, or E-alphabet grid graph. (A(m,n),s,t) is Hamiltonian if there is a Hamiltonian path between s and t in A(m,n). In this paper, without loss of generality we assume sx≤tx and m≥n. 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 s and t is a necessary condition for (R(m,n),s,t) to be Hamiltonian. Furthermore, Itai et al. [10] showed that if one of the following conditions hold, then (R(m,n),s,t) is not Hamiltonian:
R(m,n) is a 1-rectangle and either s or t is not a corner vertex (Figure 2(a)).
R(m,n) is a 2-rectangle and (s,t) is a nonboundary edge, that (s,t) is an edge and it is not on the outer face (Figure 2(b)).
R(m,n) is isomorphic to a 3-rectangle R′(m,n) such that s and t are mapped to s′ and t′ and all of the following three conditions hold:
m is even,
s′ is black, t′ is white,
sy′=2 and sx′<tx′ (Figure 2(c)) or sy′≠2 and sx′<tx′-1 (Figure 2(d)).
Rectangular grid graphs in which there is no Hamiltonian path between s and t.
A Hamiltonian path problem P(R(m,n),s,t) is called acceptable if s and t are color compatible and (R,s,t) does not satisfy any of conditions (F1), (F2), and (F3).
The following theorem has been proved in [10].
Theorem 2.1.
Let R(m,n) be a rectangular grid graph and s and t be two distinct vertices. Then (R(m,n),s,t) is Hamiltonian if and only if P(R(m,n),s,t) is acceptable.
Lemma 2.2 (see [12]).
R(m,n) has a Hamiltonian cycle if and only if it is even-sized and m,n>1.
Lemma 2.3 (see [14]).
Any L-alphabet grid graph L(m,n)(m,n≥3) has a Hamiltonian cycle if and only if mn is even.
Figure 3 shows a Hamiltonian cycle for an even-sized rectangular grid graph and L-alphabet graph L(m,n), 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 L-alphabet grid graph. This shows that for an even-sized rectangular graph R and L-alphabet graph L(m,n), we can always find a Hamiltonian cycle, such that it contains all boundary edges, except of exactly one side of R and two side of L which contains an even number of vertices.
A Hamiltonian cycle for the rectangular grid graph R(8,5) and L-alphabet grid graph L(4,3).
3. Necessary and Sufficient Conditions
In this section, we give necessary and sufficient conditions for the existence of a Hamiltonian path in L-alphabet, C-alphabet, F-alphabet, and E-alphabet grid graphs.
Definition 3.1.
A separation of
an L-alphabet grid graph L(m,n) is a partition of L into two disjoint rectangular grid graphs R1 and R2, that is, V(L)=V(R1)∪V(R2), and V(R1)∩V(R2)=∅,
an C-alphabet graph C(m,n) is a partition of C into a L-alphabet graph L(m,n) and a rectangular grid graph R(2m-2,n), that is, V(C)=V(L)∪V(R(2m-2,n)), and V(L)∩V(R(2m-2,n))=∅,
an F-alphabet grid graph F(m,n) is a partition of F into a L-alphabet grid graph L(m,n) and a rectangular grid graph R(2m-4,n) (or four rectangular grid graphs R1 to R4), that is, V(F)=V(L)∪V(R(2m-4,n)) and V(L)∩V(R(2m-4,n))=∅ (or V(F)=V(R1)∪V(R2)∪V(R3)∪V(R4) and V(R1)∩V(R2)∩V(R3)∩V(R4)=∅),
an E-alphabet grid graph E(m,n) is a partition of E into an F-alphabet grid graph F(m,n) and a rectangular grid graph R(2m-2,n) or a C-alphabet grid graph C(m,n) and a rectangular grid graph R(2m-4,n), that is, V(E)=V(F)∪V(R(2m-2,n)), and V(F)∩V(R(2m-2,n))=∅ or V(E)=V(C)∪V(R(2m-4,n)), and V(C)∩V(R(2m-4,n))=∅.
In the following, two nonincident edges e1 and e2 are parallel, if each end vertex of e1 is adjacent to some end vertex of e2.
Lemma 3.2.
Let A(m,n) be an L-alphabet, C-alphabet, F-alphabet, or E-alphabet grid graph and R be the smallest rectangular grid graph that includes A. If (A(m,n),s,t) is Hamiltonian, then (R,s,t) is also Hamiltonian.
Proof.
We break the proof into two cases.Case 1 (A(m,n) is an L-alphabet or C-alphabet grid graph).
Let P be a Hamiltonian path in L (or C) that is found by Algorithm 1 (or 2). Since R-L (or R-C) is an even-sized rectangular grid graph of (2m-2)×(4n-4) (or (2m-2)×(3n-4)), then by Lemma 2.2 it has a Hamiltonian cycle (i.e., we can find a Hamiltonian cycle of R-L (or R-C), such that it contains all edges of R-L (or R-C) that are parallel to some edge of P). Using two parallel edges of P and the Hamiltonian cycle of R-L (or R-C) 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 R.
Case 2.
A(m,n) is an F-alphabet or E-alphabet grid graph. Let P be a Hamiltonian path in F (or E) that is found by Algorithm 3 (or 4). We consider the following cases.
Subcase 2.1 (m,n>3).
Since R-F (or R-E) can be partitioned into three even-sized rectangular grid graphs of R(2m-4,n-2), R(2,2n-2), and R(2m-2,2n-2) (or R(2,3n-4),R(2m-4,n-2), and R(2m-4,n-2)), then they have Hamiltonian cycles by Lemma 2.2. Then combine Hamiltonian cycles on R(2m-4,n-2), R(2,2n-2) and R(2m-2,2n-2) (or R(2,3n-4),R(2m-4,n-2), and R(2m-4,n-2)) to be a large Hamiltonian cycle and then using two parallel edges of P and the Hamiltonian cycle of R-F (or R-E), we can obtain a Hamiltonian path for R.
Subcase 2.2 (n=3).
Let R-F be three rectangular grid graphs of R(2m-4,n-2), R(2,2n-2), and R(2m-2,2n-2). We consider the following two subcases.
Subcase2.2.1(m=3). Let two vertices v1,v2 be in R(2m-4,n-2). Using Algorithm 3 there exist two edges e1,e2 such that e1∈P or e2∈P is on the boundary of F(m,n) facing R(2m-4,n-2), see Figure 5(a). Hence by combining a Hamiltonian path P and Hamiltonian cycles in R(2,2n-2) and R(2m-2,2n-2) and (v1,v2), a Hamiltonian path between s and t is obtained, see Figures 5(b) and 5(c).
Subcase 2.2.2 (m>3). For m=4, let four vertices v1,v2,v3,v4 be in R(2m-4,n-2). Using Algorithm 3 there exist four edges e1,e2,e3,e4 such that e1,e2∈P, e1,e4∈P, e3,e4∈P or e2,e3∈P are on the boundary of F(m,n) facing R(2m-4,n-2) see Figure 6(a). Therefore, by merging (v1,v2) and (v3,v4) to these edges and Hamiltonian cycles in R-F we obtain a Hamiltonian path for R, see Figure 6(b). For other values of m, the proof is similar to that of m=4.
Similar to F-alphabet grid graphs, Hamiltonian paths can be found in E-alphabet grid graphs for n=3.
Algorithm 1: The Hamiltonian path algorithm for L-alphabet grid graphs.
procedure L_HamiltonianPath(L(m,n),s,t)
1: ifL can be stripped then
2: let S be a strip of L, where S is an even-sized rectangular grid graph
3: P←R_HamiltonianPath(L-S,s,t)
4: D← HamiltonianCycle(S)
5: return MergeStrip(P,D,s,t)
6: else
7: let L be split to Rp and Rq
8: P1←R_HamiltonianPath(Rp,s,p)
9: P2←R_HamiltonianPath(Rq,q,t)
10: return MergeSplit(P1,P2,p,q)
11: endif
Algorithm 2: The Hamiltonian path algorithm for C-alphabet grid graphs.
procedure C_HamiltonianPath(C(m,n),s,t)
1: ifC can be stripped then
2: let S be a strip of C, where S is an even-sized rectangular grid graph R(2m-2,n)
3: P←L_HamiltonianPath(C-S,s,t)
4: D← HamiltonianCycle(S)
5: return MergeStrip(P,D,s,t)
6: else
7: let C be split Rp and Lq
8: P1←R_HamiltonianPath(Rp,s,p)
9: P2←L_HamiltonianPath(Lq,q,t)
10: return MergeSplit(P1,P2,p,q)
11: endif
Algorithm 3: The Hamiltonian path algorithm for F-alphabet grid graphs.
procedure F_HamiltonianPath(F(m,n),s,t)
1: ifF can be stripped, where F(m,n) is not odd sized and s,t∉R(2m-4,n)then
2: let S be a strip of F
3: P←F-S_HamiltonianPath(F-S,s,t)
4: /*F-S is a L-alphabet grid graph L(m,n) or a rectangular grid graph R(2m-4,n)
*/
5: D← HamiltonianCycle(S)
6: return MergeStrip(P,D,s,t)
7: else
8: ifF can be stripped, where F(m,n) is odd sized and s,t∈R(2m-4,n)then
9: let S be a strip of F, where S is three rectangular grid graphs R2,R3 and R4
10: P←R_HamiltonianPath(R1,s,t)
11: c1← HamiltonianCycle(R3)
12: c2← HamiltonianCycle(R4)
13: C← MergeCycles(c1,c2)
14: D← HamiltonianCycle(R2)
15: return MergeStrip(P,D,C,s,t)
16: else
17: let F be split to Rp and Lq (or Lp and Rq)
18: P1←R_HamiltonianPath(Rp,s,p)
19: P2←L_HamiltonianPath(Lq,q,t)
20: return MergeSplit(P1,P2,p,q)
21: endif
22: endif
Algorithm 4: The Hamiltonian path algorithm for E-alphabet grid graphs.
procedure E_HamiltonianPath(E(m,n),s,t)
1: let S be a strip of E, where S is an even-sized rectangular grid graph R(2m-2,n) or
R(2m-4,n)
2: P←E-S_HamiltonianPath(E-S,s,t)
3: /*E-S is a F-alphabet grid graph F(m,n) or a C-alphabet grid graph C(m,n)*/
4: D← HamiltonianCycle(S)
5: return MergeStrip(P,D,s,t)
A Hamiltonian path in R(10,11).
A Hamiltonian path in R(7,11).
A Hamiltonian path in R(7,11).
Combining Lemma 3.2 and Theorem 2.1 the following corollary is trivial.
Corollary 3.3.
Let A(m,n) be an L-alphabet, C-alphabet, F-alphabet, or E-alphabet grid graph and R be the smallest rectangular grid graph that includes A. If (A(m,n),s,t) is Hamiltonian, then s and t must be color compatible in R.
Therefore, the color compatibility of s and t in R is a necessary condition for (L(m,n),s,t), (C(m,n),s,t), (F(m,n),s,t), and (E(m,n),s,t) 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 R(2m-2,n) and R(m,5n-4) be a separation of L(m,n) such that three vertices v, w, and u are in R(2m-2,n) which are connected to R(m,5n-4). Assume that s and t are two given vertices of L and s′=w and t′=t, if s∈R(2m-2,n) let s′=s. If tx>m+1 and (R(2m-2,n),s′,t′) satisfies condition (F3), then L(m,n) does not have any Hamiltonian path between s and t.
Proof.
Without loss of generality, let s and t be color compatible. Since m,n≥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 n=3. Assume that R(2m-2,n) satisfies condition (F3). We show that there is no Hamiltonian path in L(m,n) between s and t. Assume to the contrary that L(m,n) has a Hamiltonian path P. Since n=3 there are exactly three vertices v,w and u in R(2m-2,n) which are connected to R(m,5n-4), as shown in Figure 7(a). Then the following cases are possible. Case 1.
t∈R(2m-2,n) and s∉R(2m-2,n). The following subcases are possible for the Hamiltonian path P.Subcase 1.1.
The Hamiltonian path P of L(m,n) that starts from s may enter to R(2m-2,n) for the first time through one of the vertices v,w, or u, pass through all the vertices of R(2m-2,n) and end at t, see Figure 7(b). This case is not possible because we assumed that R(2m-2,n) satisfies (F3), in this case t′=t and s′=w.
Subcase 1.2.
The Hamiltonian path P of L(m,n) may enter to R(2m-2,n), 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 t. In this case, two subpaths of P which are in R(2m-2,n) are called P1 and P2, P1 from v to u (v to w or u to w) and P2 from w to t (u to t or v to t). This case is not also possible because the size of P1 is odd (even) and the size of P2 is even (odd), then |P1+P2| is odd while R(2m-2,n) is even, which is a contradiction.
Case 2 (s,t∈R(2m-2,n)).
The following cases may be considered. Subcase 2.1.
The Hamiltonian path P of L(m,n) which starts from s may pass through some vertices of R(2m-2,n), leave R(2m-2,n) at v (or u), then passes through all the vertices of R(m,5n-4) and reenter to R(2m-2,n) at w go to u (or v) and pass through all the remaining vertices of R(2m-2,n) and end at t. In this case by connecting v (or u) to w we obtain a Hamiltonian path from s to t in R(2m-2,n), which contradicts the assumption that R(2m-2,n) satisfies (F3).
Subcase 2.2.
The Hamiltonian path P of L(m,n) which starts from s may leave R(2m-2,n) at v (or u), then pass through all the vertices of R(m,5n-4) and reenter to R(2m-2,n) at u (or v) go to w and pass through all the remaining vertices of R(2m-2,n) and end at t. In this case, two parts of P reside in R(2m-2,n). The part P1 starts from s ends at v (or u), and the part P2 starts from u (or v) ends at t. The size of P1 is even and the size of P2 is odd while the size of R(2m-2,n) is even, which is a contradiction.
Subcase 2.3.
Another case that may arise is that the Hamiltonian path P of L(m,n) starts from s leaves R(2m-2,n) at w and reenters R(2m-2,n) at v (or u) and then goes to t. But in this case vertex u (or v) cannot be in P, which is a contradiction. Thus, the proof of Lemma 3.5 is completed.
A separation of L(4,3).
Lemma 3.6.
Let L(m,n) and R(2m-2,n) be a separation of C(m,n) such that three vertices v1, w1, and u1 are in R(2m-2,n) which are connected to L(m,n) and x∈L(m,n) is a adjacent vertex to w1. Assume that s and t are two given vertices of C. If t∈L(m,n) and s∈R(2m-2,n), let s=x and s′=w1, t′=s, respectively. If (L(m,n),s,t) does not have a Hamiltonian path or (R(2m-2,n),s′,t′) satisfies condition (F3), then C(m,n) does not have a Hamiltonian path between s and t.
Proof.
The proof is similar to the proof of Lemma 3.5, for more details see Figure 8.
C-alphabet grid graphs in which there is no Hamiltonian path between s and t.
Lemma 3.7.
Let L(m,n) and R(2m-4,n) be a separation of F(m,n) such that three vertices v1, w1, and u1 are in R(2m-4,n) which are connected to L(m,n) and x∈L(m,n) is an adjacent vertex to w1. Assume that s and t are two given vertices of F. If s,t∈R(2m-4,n) let s′=s and t′=t; if s(or t) ∈R(2m-4,n) let s′=w1 and t′=s(or s′=w1 and t′=t); if t(or s) ∈L(m,n) let s=x((t=s,s=x) or (t=x)). If (L(m,n),s,t) does not have a Hamiltonian path or (R(2m-4,n),s′,t′) satisfies the condition (F3), then F(m,n) does not have a Hamiltonian path between s and t.
Proof.
By a similar way as used in the Lemma 3.5 we can prove this lemma; for more details see Figure 9.
F-alphabet grid graphs in which there is no Hamiltonian path between s and t.
Lemma 3.8.
Let F(m,n) and R(2m-2,n) or C(m,n) and R(2m-4,n) be a separation of E(m,n) and s and t be two given vertices in E(m,n). If (F(m,n),s,t) or (C(m,n),s,t) does not have a Hamiltonian path, then E(m,n) does not have a Hamiltonian path between s and t.
Proof.
The proof is similar to the proof of Lemma 3.5, for more details see Figure 10.
E-alphabet grid graphs in which there is no Hamiltonian path between s and t.
From Corollary 3.3 and Lemmas 3.5, 3.6, 3.7, and 3.8, a Hamiltonian path problem P(L(m,n),s,t) is called acceptable if s and t are color-compatible and (R(2m-2,n),s′,t′) does not satisfy the condition (F3); P(C(m,n),s,t) is called acceptable if P(L(m,n),s,t) is acceptable and (R(2m-2,n),s′,t′) does not satisfy the condition (F3); P(F(m,n),s,t) is called acceptable if P(L(m,n),s,t) is acceptable and (R(2m-4,n),s′,t′) does not satisfy the condition (F3); P(E(m,n),s,t) is called acceptable if P(F(m,n),s,t) and (C(m,n),s,t) are acceptable.
Theorem 3.9.
Let A(m,n) be an L-alphabet, C-alphabet, F-alphabet or E-alphabet grid graph. If (A(m,n),s,t) is Hamiltonian, then P(A(m,n),s,t) 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 S of an L-alphabet, C-alphabet, F-alphabet or E-alphabet grid graph A strips a Hamiltonian path problem P(A(m,n),s,t), if all of the following four conditions hold:
S is even sized and:
S is a rectangular grid graph; where A(m,n) is a L-alphabet grid graph L(m,n), a C-alphabet grid graph C(m,n) and an E-alphabet grid graph E(m,n);
S is a L-alphabet graph L(m,n), a rectangular graph R(2m-4,n) or three rectangular grid graphs R1-R3; where A(m,n) is a F-alphabet grid graph.
S and A-S is a separation of A;
s,t∈A-S;
P(A-S,s,t) is acceptable.
Definition 3.11.
Let P(A(m,n),s,t) be a Hamiltonian path problem and (p,q) be an edge of A, where A(m,n) is an L-alphabet, C-alphabet, or F-alphabet grid graph. Then we say (p,q)splits (A(m,n),s,t) if there exists a separation of
L into Rp and Rq such that
s,p∈Rp and P(Rp,s,p) is acceptable,
q,t∈Rq and P(Rq,q,t) is acceptable.
F (resp., C) into Rp and Lq or Rq and Lp(resp., Lq and Rp) such that
s,p∈Rp and P(Rp,s,p) is acceptable (or s,p∈Lp and P(Lp,s,p) is acceptable),
q,t∈Lq and P(Lq,q,t) is acceptable (or q,t∈Rq and P(Rq,q,t) 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 L-Alphabet Grid Graphs L(m,n)
Since an L-alphabet graph L(m,n) may be partitioned into two rectangular grid graphs, then the possible cases for vertices s and t are as follows.
Case 1 (s,t∈L-S).
Assume that L-S has a Hamiltonian path P by the algorithm of [12], where L-S is a rectangular gird graph. Since S 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 (L(m,n),s,t) can be obtained by merging P and the Hamiltonian cycle of S as shown in Figure 11(b).
(a), (b) A strip of L(3,3), (c) a strip of C(3,3).
Case 2 (s∈Rp and t∈Rq).
In this case, we construct Hamiltonian paths in Rp and Rq between (s,p) and (q,t), respectively; see Figure 12(a). Then a Hamiltonian path for (L(m,n),s,t) can be obtained by connecting two vertices p and q as shown in Figure 12(b).
(a), (b) A split of L(3,3), (c) a split of C(3,3).
3.2. Hamiltonian Paths in C-Alphabet Grid Graphs C(m,n)
Similar to L-alphabet graphs since a C-alphabet graph C(m,n) may be partitioned into a L-alphabet graph L(m,n) and a rectangular grid graph R(2m-2,n), then the possible cases for vertices s and t are as follows.
Case 1 (s,t∈C-S).
Assume that C-S has a Hamiltonian path P by the Algorithm 1, where C-S is an L-alphabet gird graph L(m,n). Hence, a Hamiltonian path for (C(m,n),s,t) can be obtained by merging P and the Hamiltonian cycle of S as shown in Figure 11(c).
Case 2 (s∈Rp and q∈Lq).
A Hamiltonian path can be found as Figure 12(c).
3.3. Hamiltonian Paths in F-Alphabet Grid Graphs F(m,n)
For F-alphabet grid graphs, we consider the following cases.
Case 1 (s,t∈F-S, where F-S is L(m,n)).
In this case, we construct a Hamiltonian path between s and t in L(m,n) by the Algorithm 1. Since S is an even-sized rectangular grid graph R(2m-4,n), then it has a Hamiltonian cycle by Lemma 2.2.
Case 2 (s,t∈F-S, where F-S is R(2m-4,n)).
We construct a Hamiltonian path between s and t in R(2m-4,n) by the algorithm in [12]. Since S is an even-sized L-alphabet grid graph L(m,n), then it has a Hamiltonian cycle by Lemma 2.3.
In two cases, by combining the Hamiltonian cycle of S and the Hamiltonian path of L or R(2m-4,n), a Hamiltonian path between s and t for F(m,n) is obtained, see Figures 13(a) and 13(b).
(a), (b) A strip of F(4,3) and F(3,3), (c) a strip of E(3,3).
Case 3 (F(m,n) is odd sized and s,t∈F-S, where F-S is R(2m-4,n)).
In this case, s and t must be white. Since R(2m-4,n) is even sized, then s and t are not color compatible in R(2m-4,n), see Figure 14(a). Therefore, we repartition F(m,n) into four rectangular grid graphs R1, R2, R3, and R4, such that s,t∈V(R1) and R2, R3, and R4 are even sized, see Figure 14(b) where the dotted lines represent the strip. In this case, by combining the Hamiltonian cycles in R2, R3, and R4 and the Hamiltonian path of R1, a Hamiltonian path between s and t for (F(m,n),s,t) is obtained, see Figure 14(c).
A strip of F(3,3), when s,t∈R(2m-4,n).
Case 4 (s∈Rp and t∈Lq or s∈Lp and q∈Rq).
A Hamiltonian path can be found as Figure 15.
A split of F(3,3).
3.4. Hamiltonian Paths in E-Alphabet Grid Graphs E(m,n)
A Hamiltonian path for an E-alphabet grid graph E(m,n) can be found using striping. So we strip E(m,n) such that s,t∈E-S, where E-S is F-alphabet F(m,n) or C-alphabet C(m,n). In this case, we construct a Hamiltonian path in F(m,n) (or C(m,n) by Algorithm 3 (or 4). Since S is an even-sized rectangular grid graph R(2m-2,n) (or R(2m-4,n)), then it has a Hamiltonian cycle by Lemma 2.2. By combining the Hamiltonian cycle of S and the Hamiltonian path of F or C, a Hamiltonian path between s and t for E(m,n) is obtained, see Figure 13(c).
Thus, we have the following lemmas.
Lemma 3.12.
Let (A(m,n),s,t) be an acceptable Hamiltonian path problem, and S strips it, where A(m,n) is an L-alphabet, C-alphabet, F-alphabet, or E-alphabet grid graph. If A-S has a Hamiltonian path between s and t, then (A(m,n),s,t) has a Hamiltonian path between s and t.
Lemma 3.13.
Let (p,q) be an edge which splits P(A(m,n),s,t). If
(Lq,q,t) and (Rp,s,p), where A(m,n) is C-alphabet grid graph C(m,n),
(Rp,s,p) and (Lq,q,t) or (Rq,q,t) and (Lp,s,p), where A(m,n) is F-alphabet grid graph F(m,n),
have a Hamiltonian path between (s,p) and (q,t), then A(m,n) also has a Hamiltonian path between s and t.
Since all the proofs presented in this section were constructive, they give us algorithms for finding a Hamiltonian path in L-alphabet, C-alphabet, F-alphabet, and E-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 e1 and e2, MergeSplit is a procedure that connects two paths by simply adding an edge between their end vertices p and q, 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 A can be stripped, then strips A by S, and recursively finds a Hamiltonian path of A-S. Otherwise, if A can be split, then splits A into Rp and Rq (Lq and Rp or Rq and Lp), and recursively finds Hamiltonian paths of Rp and Rq (Lq and Rp or Rq and Lp). Then these two Hamiltonian paths are merged into a single path, where A(m,n) is an L-alphabet, C-alphabet, F-alphabet, or E-alphabet grid graph.
From Theorem 3.9 and Lemmas 3.12 and 3.13 the following theorem holds.
Theorem 3.14.
Let A(m,n) be an L-alphabet, C-alphabet, F-alphabet, or E-alphabet grid graph, and s and t be two distinct vertices of it. A(m,n) has Hamiltonian path if and only if P(A(m,n),s,t) is acceptable.
Theorem 3.14 provides necessary and sufficient conditions for the existence of Hamiltonian paths in L-alphabet, C-alphabet, F-alphabet and E-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 L-alphabet, C-alphabet, F-alphabet or E-alphabet grid graphs, a Hamiltonian path between any two vertices s and t can be found in linear time.
Proof.
The algorithms divide the problem into some rectangular grid graphs in O(1). Then we solve the subproblems in linear time using the linear time algorithm in [12]. Then the results are merged in time O(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 L-alphabet, C-alphabet, F-alphabet, and E-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.
DiestelR.20001732ndNew York, NY, USASpringerxiv+31310.1007/b1000331743598ZBL0974.46031GareyM. R.JohnsonD. S.1979San Francisco, Calif, USAW. H. Freeman and Co.x+338519066ZBL0411.68039DamaschkeP.Paths in interval graphs and circular arc graphs19931121–34964121375010.1016/0012-365X(93)90223-GZBL0777.05081DuL.A polynomial time algorithm for hamiltonian cycle (Path)1Proceedings of the International MultiConference of Engineers and Computer Scientists (IMECS '10)2010Hong Kong1719GouldR. J.Advances on the Hamiltonian problem—a survey2003191752197436810.1007/s00373-002-0492-xZBL1024.05057GurevichY.ShelahS.Expected computation time for Hamiltonian path problem198716348650288940410.1137/0216034ZBL0654.68083KaoS.-S.HsuL.-H.Spider web networks: a family of optimal, fault tolerant, Hamiltonian bipartite graphs20051601269282210019110.1016/j.amc.2003.06.005ZBL1057.05052RahmanM. S.KaykobadM.On Hamiltonian cycles and Hamiltonian paths20059413741212312110.1016/j.ipl.2004.12.002ZBL1182.68142LuccioF.MugniaC.Hamiltonian paths on a rectangular chessboardProceedings of the 16th Annual Allerton Conference1978161173ItaiA.PapadimitriouC. H.SzwarcfiterJ. L.Hamilton paths in grid graphs198211467668667766110.1137/0211056ZBL0506.05043ZamfirescuC.ZamfirescuT.Hamiltonian properties of grid graphs199254564570118682410.1137/0405046ZBL0770.05073ChenS. D.ShenH.ToporR.An efficient algorithm for constructing Hamiltonian paths in meshes20022891293130510.1016/S0167-8191(02)00133-31926025ZBL0999.68253LenhartW.UmansC.Hamiltonian cycles in solid grid graphsProceedings of the 38th Annual Symposium on Foundations of Computer Science (FOCS '97)1997496505SalmanA. N. M.2005University of TwenteIslamK.MeijerH.NunezY.RappaportD.XiaoH.Hamiltonian circuts in hexagonal grid graphsProceedings of the CCCG20072022GordonV. S.OrlovichY. L.WernerF.Hamiltonian properties of triangular grid graphs20083082461666188246490610.1016/j.disc.2007.11.040ZBL1158.05040NandiM.ParuiS.AdhikariA.The domination numbers of cylindrical grid graphs20112171048794889276327610.1016/j.amc.2010.11.019ZBL1223.05214Keshavarz-KohjerdiF.fatemeh.keshavarz.2003@gmail.comBagheriA.arbagheri@aut.ac.irAsgharian-SardroudA.asgharian@aut.ac.irA linear-time algorithm for the longest path problem in rectangular grid graphs2012160321021710.1016/j.dam.2011.08.010Keshavarz-KohjerdiF.BagheriA.An efficient parallel algorithm for the longest path problem in mesheshttp://arxiv.org/abs/1201.4459