Elsevier

Discrete Optimization

Volume 35, February 2020, 100554
Discrete Optimization

Linear-time algorithms for finding Hamiltonian and longest (s,t)-paths in C-shaped grid graphs

https://doi.org/10.1016/j.disopt.2019.100554Get rights and content

Abstract

The longest and Hamiltonian path problems are well-known NP-hard problems in graph theory. Despite many applications of these problems, they are still open for many classes of graphs, including solid grid graphs and grid graphs with some holes. We consider the longest and Hamiltonian (s,t)-path problems in C-shaped grid graphs. A (s,t)-path is a path between two given vertices s and t of the graph. A C-shaped grid graph is a rectangular grid graph such that a rectangular grid subgraph is removed from it to make a C-liked shape. In this paper, we first give the necessary conditions for the existence of Hamiltonian cycles and Hamiltonian (s,t)-paths in such graphs. Then by given a linear-time algorithm for finding Hamiltonian cycles and Hamiltonian (s,t)-paths, we show that these necessary conditions are also sufficient. Finally, we give a linear-time algorithm for finding the longest (s,t)-path in these graphs.

Introduction

The longest path problem, i.e. the problem of finding a simple path with the maximum number of vertices, is one of the most important problems in graph theory. The well-known NP-complete Hamiltonian path problem, i.e. deciding whether there is a simple path that visits each vertex of the graph exactly once, is a special case of the longest path problem. Despite the many applications of these problems, they are still open for many classes of graphs, including solid grid graphs and grid graphs with some holes [1], [2].

For the longest path problem, only few polynomial-time algorithms are known for special classes of graphs. This problem for trees began with the work of Dijkstra around 1960, and was followed by others [3], [4], [5]. In the area of approximation algorithms, it has been shown that the problem is not in APX, i.e. there is no polynomial-time constant factor approximation algorithm for the problem unless P = NP [6]. Also, it has been shown that finding a path of length nnϵ is not possible in polynomial-time unless P = NP [7]. To our knowledge, the best-known approximation algorithm for the problem has the ratio of O(n(loglognlogn)2) [8]. For the background and some known results about approximation algorithms, we refer the reader to [9]. In this paper, we focus on grid graphs.

The two-dimensional integer grid G is an infinite undirected graph in which vertices are all points of the plane with integer coordinates and two vertices are connected by an edge if and only if the Euclidean distance between them is equal to 1. A grid graph is a finite vertex-induced subgraph of the two-dimensional integer grid (see Fig. 1(a)). A solid grid graph is a grid graph without holes. A rectangular grid graph is a grid graph bounded by an axis-parallel rectangle (see Fig. 1(b)). A C-shaped or L-shaped grid graph is a rectangular grid graph such that a rectangular grid subgraph is removed from it to make a C-like or L-like shape (see Fig. 1(c) and (d)). Despite the many applications of the problem, it is still open for many classes of graphs (see [10], [11] for more details), including solid grid graphs and grid graphs with some holes [1], [2].

There are many applications for the Hamiltonian path in grid graphs including the picturesque maze generation problem [12], pathogen biology [13], and the off-line exploration problem in robotic motion planning [14]. In the picturesque maze generation problem, a rectangular black-and-white raster image is given, and a random maze should be generated in which the resulted path fills up the black pixels. This problem is equal to finding a Hamiltonian path in the subgraph induced by the vertices corresponding to the black pixels. Modeling of animals movement can help to understand the dynamics of pathogen transmission. Srinivasa Rao et al. [13] investigated the movement of domestic chicken in association with the transmission of its protozoan parasite, Eimeria, through physical, rather than airborne, contact. They suggested a configuration for maximum possible distance of walking in straight and non-overlapping paths on square grid graphs. A maximum possible walk between two cells on square grid graphs can be considered as a longest path between these two cells. In the off-line exploration problem, a mobile robot should visit every cell in a cellular room and return to the starting point. The number of cells that are visited multiple times by the robot should be minimum. Exploring the room without any multiple visited cell corresponds to finding a Hamiltonian cycle in a grid graph.

Itai et al. [1] have shown that the Hamiltonian path problem for general grid graphs, with or without specified endpoints, is NP-complete. The problem for rectangular grid graphs, however, is in P requiring only linear time. Later, Chen et al. [15] improved the algorithm of [1] and presented a parallel algorithm for the problem in mesh architecture. Lenhart and Umans [2] have presented a polynomial-time algorithm for finding Hamiltonian cycles in solid grid graphs. Salman [16] determined classes of alphabet grid graphs that have Hamiltonian cycle. Alphabet grid graphs are a special kind of grid graphs that their shapes are similar to alphabet characters. In [17], the authors proposed a linear-time algorithm for the Hamiltonian path problem for some small classes of grid graphs, namely L-alphabet, C-alphabet, E-alphabet, and F-alphabet grid graphs. L-alphabet and C-alphabet grid graphs considered in [17] is a special case of L-shaped and C-shaped grid graphs.

Recently, Hamiltonian path(cycle) and longest path problems in grid graphs have received much attention. Zhang and Liu [18] gave an approximation algorithm for the longest path problem in grid graphs; their algorithm runs in quadratic time. Keshavarz-Kohjerdi et al. [19], [20] studied the longest path problem for rectangular grid graphs, their algorithm is based on divide and conquer technique and runs in linear time. More recently, Hung et al. [21] showed that the Hamiltonian cycle and path problems in supergrid graphs are NP-complete. Also, they showed that linear-convex supergrid graphs always contain Hamiltonian cycles [22]. In addition, there is a linear-time algorithm for the longest path problem between two given vertices for L-shaped grid graphs proposed by Keshavarz-Kohjerdi and Bagheri [23], [24].

In this paper, we consider the longest and the Hamiltonian path problems between two given vertices s and t in C-shaped grid graphs. The motivation to consider these problems comes from the problem of embedding a guest graph in a given host graph [25]. In this problem, first we should recognize if there is enough room in the host graph for the guest graph. Consider a simple form of the problem, where the guest graph is a path and the host graph is a solid grid graph or a grid graph with some holes. To solve this problem actually the longest path (or the Hamiltonian path) problem should be solved, which as mentioned before is a open problem. In solving this open problem, we face into another version of the problem in which the first and last vertices of the path are fixed which are called (s,t)-path. To be more clear, in the solid grid graph given in Fig. 2(a), finding the longest and Hamiltonian path in the graph requires finding the longest and Hamiltonian (s,t)-path in the C-shaped grid subgraph of the original graph (see Fig. 2(b)).

In this paper, we consider the longest and Hamiltonian (s,t)-path in C-shaped grid graphs, which are a special case of rectangular grid graphs with a rectangular hole. This can be considered among the first attempts to solve the mentioned open problems. The Hamiltonian, but not the longest (s,t)-path, of rectangular grid graphs with a rectangular hole has been considered in [26], [27].

The main idea of our approach is to partition the given graph into some parts, then show that if there is the Hamiltonian (s,t)-path in the original graph by examining these parts. If the Hamiltonian (s,t)-path does not exist, then using this partitioning the longest path of the graph is constructed by merging the longest paths of these parts. Although the idea of partitioning the original graph may seem to be a simple idea, but there are many issues that should be considered. In other words, the exact form of the partitioning is important and should be done precisely. If the partitioning is done in a wrong way, then the Hamiltonian or the longest path may not be obtained by combining the longest paths of the parts of the partitioning.

The organization of the paper is as follows. In Section 2, the definitions and the previous results are given. The necessary and sufficient conditions for the Hamiltonian cycle and (s,t)-path of C-shaped grid graphs are given in Section 3. The upper bounds on the length of longest paths are given in Section 4. In this Section, we also show how to obtain a longest (s,t)-path for C-shaped grid graphs. Conclusion is given in Section 5.

Section snippets

Preliminaries

In this section, we provide some definitions and results which we need in the paper. Some of the definitions are previously defined in [1], [15], [19], [20], [23], [28].

The two-dimensional integer grid G is an infinite undirected graph in which vertices are all points of the plane with integer coordinates and two vertices are connected by an edge if and only if the Euclidean distance between them is equal to 1. For a vertex v of this graph, let vx and vy denote x and y coordinates of its

The necessary and sufficient conditions for the Hamiltonian cycle and Hamiltonian (s,t)-path of C-shaped grid graphs

In this section, we are going to obtain necessary and sufficient conditions for the existence of a Hamiltonian cycle and a Hamiltonian (s,t)-path in C-shaped grid graph C(m,n;k,l;d,c). First, we will verify the Hamiltonicity of C-shaped grid graphs. If condition (FC) holds, then C(m,n;k,l;d,c) is not Hamiltonian, where (FC) is defined as follows:

    (FC)

    (C(m,n;k,l;d,c) is odd-sized), (C(m,n;k,l;d,c) is even-sized, n=odd, d=odd>1, c=odd>1, and nl=2), or (at least one of the d, c, and nl equals 1).

Theorem 3.1

C

The longest (s,t)-path algorithm

From Theorem 3.8, it follows that if (C(m,n;k,l;d,c),s,t) satisfies one of the conditions (F1), (F3), and (F10)–(F13) or (C(m,n;k,l;d,c),s,t) is not color-compatible, then HP(C(m,n;k,l;d,c),s,t) does not exist. So in this section, for these cases we give upper bounds on the lengths of longest paths between s and t. Recall that we only consider the cases (1) sx,txd+k and (2) sxd and tx>d+k. Notice that the isomorphic cases are omitted.

In the following, we use Lˆ(G,s,t) to show the length of

Conclusion

We gave necessary conditions for the existence of a Hamiltonian path in C-shaped grid graphs between any two given vertices. Then we showed that these necessary conditions are also sufficient by giving a linear-time algorithm to compute the Hamiltonian path. Then we give a linear-time algorithm for computing the longest paths of C-shaped grid graphs. C-shaped grid graphs are a special kind of solid grid graphs and also a special kind of grid graphs with some holes. So, solving the Hamiltonian

Acknowledgments

The authors would like to thank the referees for many useful comments and suggestions which have improved the presentation of the paper.

References (28)

  • ItaiA. et al.

    Hamiltonian paths in grid graphs

    SIAM J. Comput.

    (1982)
  • LenhartW. et al.

    Hamiltonian cycles in solid grid graphs

  • MertziosG.B. et al.

    The longest path problem is polynomial on cocomparability graphs

    Algorithmica

    (2013)
  • UeharaR. et al.

    On computing longest paths in small graph classes

    Internat. J. Found Comput. Sci.

    (2007)
  • Cited by (10)

    View all citing articles on Scopus
    View full text