Unit-length embedding of cycles and paths on grid graphs
Although there are very algorithms for embedding graphs on unbounded grids, only few results on embedding or drawing graphs on restricted grids has been published. In this work, we consider the problem of embedding paths and cycles on grid graphs. We give the necessary and sufficient conditions for the existence of cycles of given length and paths of given length between two given vertices in -vertex rectangular grid graphs and introduce two algorithms with running times O and O for finding respectively such cycles and paths. Also, we extend our results to 3D grids. Our method for finding cycle of length in rectangular grid graphs also introduces a linear-time algorithm for finding cycles of a given length in hamiltonian solid grid graphs.
Keywords:Grid graphs, Rectangular grid graphs, 3D grids, Hamiltonian path, Hamiltonian cycle, Longest Path.
In the area of graph drawing, a lot of research has been done on drawing or embedding various classes of graphs on grids. Most of the research thus far, has considered unbounded grids, and in some cases tried to minimize the area used for drawing the given graph, and only a few results deal with drawing or embedding graphs on restricted grids. Felsner et al.  considered the problem of drawing graphs on -tracks, i.e. grids. They characterized the 2-track drawable trees and devised a linear-time algorithm for constructing such drawings. Cornelsen et al.  proposed a polynomial-time algorithm for planar drawing of an arbitrary graph on two parallel lines. Their results show that, only outer planar graphs may have such a drawing. Suderman studied the various types of drawings of a tree on grids . Using the concept of pathwidth, he worked out the optimal lower and upper bounds on the minimum value of for a tree in order to be drawable on a grid.
In this paper, we consider unit-length drawing of cycles and paths in bounded grids, i.e. in grid graphs. Grid graphs are finite vertex-induced subsets of infinite integer grids and they are widely used in various areas of computer science with applications in VLSI layout design (e.g. [8, 10]), robotics and path planning (e.g. [1, 2]), computer graphics, overlay networks and parallel processing.
The problem of finding paths and cycles of a given length is the general case of the problems of finding Hamiltonian and longest paths and cycles. These well-known problems are NP-hard for both general graphs [4, 7] and grid graphs . However, there are polynomial-time algorithms for these problems on some subclasses of grid graphs. For example, Umans et al.  proposed a polynomial-time algorithm for finding Hamiltonian cycles in solid grid graphs. Solid grid graphs are a subset of grid graphs which have no holes, i.e. any unbounded face in these graphs has unit area, but the polynomiality of Hamiltonian path, longest path and longest cycle problems are still open problems for this class of graphs. Recently, Zhang and Liu  proposed a -approximation algorithm for the longest path problem in grid graphs that possess square free 2-factors. A square free 2-factors is a 2-regular spanning subgraph in which any component has more than 4 vertices. In  salman defined 26 classes of grid graphs called alphabet graphs and determined which of these classes of graphs has Hamiltonian cycles.
Itai et al.  proposed a linear-time algorithm for finding a Hamiltonian path between two given vertices of a rectangular grid graph. Rectangular grid graphs are grid graphs with the vertex set of all integer-coordinate points inside an axis-parallel rectangle. For this problem, Chen et al.  introduced a parallel algorithm that can find a Hamiltonian path between two given vertices of a rectangular grid graph in constant time, if there is a processor for each vertex of the graph. Keshavarz et al.  considered the longest path problem in rectangular grid graphs and introduced a linear-time algorithm for finding a longest path between two given vertices in a rectangular grid graph.
The problems of Hamiltonian path, Hamiltonian cycle, longest path and longest cycle are widely studied in the case of grid graphs. Yet, we are not aware of any results on finding paths and cycles of a given length in these graphs. We present a linear-time algorithm for constructing a cycle of a given length in rectangular and Hamiltonian solid grid graphs, and a polynomial-time algorithm for finding a path of a given length between two specific vertices of a rectangular grid graph.
The remainder of this paper is organized as follows. Section 2, contains some necessary concepts and definitions. In Sections 3 and 4, the necessary and sufficient conditions are given for the existence of respectively cycles of length and paths of length between two given vertices in a rectangular grid graph. Although the proofs given in Sections 3 and 4 are constructive and implicitly give algorithms for finding desired cycles or paths, since these algorithms are not efficient, Section 5 introduces two efficient algorithms for these problems. In Section 6, we extend our results to 3D grids and Section 7 concludes the paper.
The two-dimensional integer grid is an infinite graph with the vertex set of all the points of the Euclidean plane that have integer coordinates. In this graph, there is an edge between any two vertices of unit distance. A grid graph is a finite vertex-induced subgraph of . For a vertex of , we use and to denote the and coordinates of its corresponding point in the Euclidean plane. Furthermore, we color white if is even; otherwise, we color it black. Grid graphs are 2-colorable, because there is no edge between white and black vertices in these graphs. For a grid graph , its size is considered to be the number of its vertices. Similarly for a path or cycle , its length is equal to the number of its vertices.
In a 2-colorable graph any cycle or path alternates between black and white vertices, so we have the following lemma.
In a grid graph, any cycle and any path between two different-colored vertices has even length, and any path between two same-colored vertices has odd length.
Grid graph is solid if is connected, where denotes the graph obtained from by removing vertices of together with their incident edges. A rectangular grid graph, (or for short), is a solid grid graph whose vertex set is . Similarly, 3D grid graph can be defined by the vertex set .
In the following, denotes the given rectangular grid graph in which we would like to find path between two vertices and , or cycle of the given length . In the figures, the vertices are illustrated by bullets, the edges of or by solid lines and the other edges of by dotted lines. The paths are assumed to be directed from to and the cycles are assumed to be directed in clockwise direction. Based on the direction of the edges, we have four type of edges, i.e. up, down, left and right edges.
Our main idea for finding paths or cycles of length is to start from a longest path or cycle and decrease its length to by contracting some parts of it called caves. A cave in a path or cycle is a minimal sub path of or such that its first and last edges are in opposite directions (see Figure 1(a)). We use letters and to refer to the start and end vertex of cave respectively. All the edges and vertices between and (i.e. the edges and the vertices of in the shortest path between and excluding and ) are considered to be inside the cave. A cave is called contractible if none of the vertices inside it are in or . Figure 1(a) shows part of a rectangular grid graph containing a non-contractible cave between two vertices and . Contracting a contractible cave is done by replacing the cave with the shortest path between and as shown in Figure 1(b). The cave of cycle is convex cave if the edges and vertices inside also lie inside ; otherwise, it is a concave cave.
3 Cycles of length
In this section, we give the necessary and sufficient conditions for the existence of cycles of length in . First, we find the length of the longest cycles of a grid graph . Then using the concept of contractible convex caves we show that for any even that is not larger than the length of the longest cycles of , there is a cycle of length in . The following lemma gives the length of the longest cycles of , and shows how we can find such cycles.
The length of the longest cycles of a rectangular grid graph is when is even, and otherwise.
Any cycle in a solid grid graph can be considered a simple orthogonal polygon. The vertices of this orthogonal polygon are those vertices of in which the direction of the edges of change. An orthogonal ear in an orthogonal polygon is four consecutive vertices and such that the line segment connecting to lies completely inside the polygon. Two orthogonal ears are non-overlapping, if the insides of the two quadrilaterals defined by these ears are disjoint.
Let be a cycle in a solid grid graph , and be one of its edges. Then contains a contractible convex cave not containing .
Without loss of generality we assume that the orthogonal polygon corresponding to has more than four vertices, otherwise the lemma is trivial. We know that every orthogonal polygon with at least five vertices has at least two non-overlapping orthogonal ears . Any orthogonal ear, in the orthogonal polygon, corresponds to a contractible convex cave in . Thus, should contain at least two contractible convex caves, and one of these two caves does not contain the edge . ∎
In a rectangular grid graph there is a cycle of length if and only if , is even and .
Clearly if or there is no cycle in . Because is 2-colorable any cycle in should have even length. Furthermore, based on Lemma 3.2 any cycle in has a contractible cave such that its contraction reduces the length of the cycle by two. Therefore starting from the longest cycle of (obtained by Lemma 3.1) and by repeatedly finding and contracting such caves we can construct a cycle of length for any even satisfying . Note that there is no cycle of length less than four in . ∎
Lemma 3.2 has more general consequences. For example, this lemma shows that in a Hamiltonian solid grid graph with vertices there is a cycle of length for any even satisfying .
4 Paths of length
In this section, we use a similar approach for giving the necessary and sufficient conditions for the existence of paths of length between two given vertices in a rectangular grid graph. We show that any path between and that is not a shortest path has a contractible cave. Thus, starting from a longest path between and we can construct a path of any desired length.
A path from to is monotone if it does not contain two edges in opposite directions i.e. it does not contain both up and down or both left and right edges.
In a rectangular grid graph, a path between any two vertices and is shortest if and only if it is monotone.
Without loss of generality assume that and . Clearly, any path between and must contain at least up edges and at least right edges. Furthermore, any monotone path between and should contain only up and right edges, so it can not contain more than up edges and more than right edges. Therefore, any monotone path between and is a shortest path. On the other hand, any shortest path has only up edges and right edges so is monotone. ∎
In the following, we are going to show that any non-monotone path in has at least one contractible cave. Clearly any non-monotone path contains at least one cave because it contains at least two edges in opposite directions. Let be a non-contractible cave and and be its end vertices. Suppose that edges inside incident to or are not in (e.g. and in Figure 3), because in this case we can easily find a contractible cave with three edges and contract it. Then should be blocked by or (i.e. or are inside ) or by some smaller caves. A cave is blocked by cave if all the edges of are inside except its first and last edges. Figure 3 shows a cave blocked by and two smaller caves. Note that, edges like and should be in because each vertex of , except and , should have two incident edges in .
Any non-monotone path in a rectangular grid graph contains at least one contractible cave.
Let be a non-monotone path between vertices and in a rectangular grid graph . Then it should contain at least one cave. Let be the nearest cave to (i.e. when we start from and go along , is the first cave that we encounter). We assume that edges of inside and incident to the end vertices of are not in , otherwise it would be easy to find a contractible cave with three edges. Hence should be blocked by or or some other smaller caves. We can search the blocking caves of recursively to find a contractible cave. Since the length of blocking caves of a cave is smaller than its length, caves cannot block each other in a cyclic order. So we should finally find a contractible cave except in the case that there is a sequence of caves where is blocked only by and is blocked only by (see Figure 4). Note that neither nor its directly or indirectly blocking caves can be blocked by , because we assumed that is the nearest cave to .
In this case, let be the vertex of adjacent to . The sub path of between and together with edge makes cycle . If is inside (Figure 5(b)) then clearly connecting to any vertex of with only one edge of can not result in a cycle containing in its inside. Therefore, without loss of generality we can assume that is not inside (Figure 5(a)), otherwise we can change the direction of the path and exchange the roles of and . Using Lemma 3.2, we can find a contractible cave in not containing edge . This completes our proof. ∎
Let and be two vertices of a rectangular grid graph . There is a path of length between and in if and only if and , where and are respectively lengths of the shortest and the longest paths between and in .
Clearly there is no path of any length shorter than or longer than between and . Based on Lemma 2.1 either all of the paths between and have even lengths or all of them have odd lengths, so and are necessary conditions for the existence of a path of length . For the sufficiency of these conditions note that there is a linear-time algorithm for finding a longest path between and in . Furthermore if a path between and is not the shortest one, based on Lemma 4.1 it is non-monotone so by the Lemma 4.2 it has at least one contractible cave. By contracting this cave we can reduce its length by two. Thus starting from the longest path, and repeatedly finding and contracting a contractible cave we can finally construct a paths of length , if satisfies the mentioned conditions. ∎
5 The algorithms
We demonstrated the necessary and sufficient conditions for the existence of cycles or paths of length in a rectangular grid graph. The proof of the Theorem 3.1 is constructive and provides an algorithm for finding a cycle of length in a solid grid graph . However, it requires O time to execute. Because a longest cycle of can be found in O time and its length is O. Similarly, the constructive proof of the Theorem 4.1 provides an algorithm for finding a path of length between two vertices of a rectangular graph which requires O time to execute. In this section we will present two efficient algorithms for finding such cycles and paths.
First, we show that reducing the length of a cycle in a solid grid graph to the desired length can be done in time O. The procedure ShrinkCycle in the algorithm 1 reduces the length of by an even integer , without removing a given edge from , in O time. In this algorithm, refers to the sub path of starting from going in the clockwise direction around and ending at , where and are two vertices of . The following lemma proves the correctness and time complexity of Algorithm 1.
Let be a cycle in a solid grid graph , and be a given edge of . Then we can find a cycle of length containing in O time, if it exists.
We prove the lemma by showing that the procedure ShrinkCycle of Algorithm 1 works correctly and runs in time O, when its preconditions is satisfied (i.e. its parameter is a cycle in a solid grid graph and is an even integer and ). By calling the ShrinkCycle with parameters , and we can construct the desired cycle . In this procedure, if then should be zero, so we can assume that . Based on the Lemma 3.2, there is at least one contractible convex cave not containing on . Hence, in line 3, always can be found. If is contractible, then its contraction reduces the length of by two, and if it is not contractible, its general situation is illustrated in Figure 6. In this case, let be the vertex of inside closest to and be the adjacent vertex of in different from . If , then line 12 of the procedure, by removing from and adding edge to it, decreases its length by . Otherwise, let be the cycle formed by sub path nad edge . The procedure recursively reduces the length of cycle by at line 16. Clearly, at this line is an edge of and so the preconditions of the procedure ShrinkCycle are satisfied. By induction on the length of , we can assume that the recursive call at line 16 works correctly. Because, and when (), the procedure trivially works correctly. Therefore, Algorithm 1 works correctly.
The while statement of line 2 of the procedure can iterate at most O times (including the iterations of the recursive calls), because in each iteration either or decreases. Storing the track of the scanned part of , we can find all the consecutive convex caves of a cycle in one scan. In other words, let be the last scanned vertex of and let be the ordered list of vertices of in which direction of the edges change, together with and . For each consecutive pair of vertices of this list, and , let be the set of vertices of lying to the right of the line segment connecting and and adjacent to some vertices of . Also let be the set of edges of whose end vertices are in . We can construct and maintain these lists while scanning (starting from ) and contracting some of its caves. Using vertices and the sets and one easily can check that if there is a cave ending at , and whether it is contractible or not in time O. Hence, the total time spent at lines 3 and 4 of the procedure (including the recursive calls) is O.
The total time spent at line 5 can not exceed O, because contracting a cave requires O time and when is contracted, the number of vertices of lying inside is decreased by , and it is never increased again. Other lines of this procedure can be implemented in time O. Therefore, overall running time of the algorithm is O. ∎
The size of can be arbitrarily larger than . So applying the Algorithm 1 to a longest cycle of may require more than O time. However, instead of , we can use a rectangular subgraph of having O size. For example, for a rectangular grid graph with and , let be the induced rectangular subgraph of with vertex set , where:
Clearly, has a cycle of length and is at most .
In a rectangular grid graph , a cycle of length can be found in O time, if it exists.
First we find a rectangular subgraph of of size O having more than vertices. Then by Lemma 5.1 we can construct a cycle of length from the longest cycle of in O time. ∎
Algorithm 1 also can be used to shrink cycles in solid grid graphs. For example, Figure 8(b) shows a cycle of length 58 on a solid grid graph which is obtained from its Hamiltonian path (Figure 8(a)) by applying the procedure ShrinkCycle of Algorithm 1. In the remaining of this section, we will show that a path of length between two vertices of a rectangular grid graph can be found in O time.
In a rectangular grid graph , having a path between two vertices and , we can find a path of the given length between and in O time, if it exists.
By Lemma 4.2, while is not a shortest path it contains at least a contractible cave. Such a cave can be found in O time, and we can contract at most O caves. Therefore, if a path of length between and exists, it can be found in O time. ∎
One may think it is easy to reduce the time complexity of lemma 5.2 to O, similar to Lemma 5.1. But note that, while shrinking a cycle, the algorithm contracts only convex caves, so it sweeps the interior of the cycle only once. However, there is no convex caves on paths, and contracting caves may cause some parts of the graph to be swept multiple times.
In a rectangular grid graph , a path of length between two given vertices and can be found in O time, if it exists.
We show how to construct a path between and such that and O. Then, by using and applying Lemma 5.2 we can construct a path of length in O time. If there is a rectangular subgraph of including and such that and O, then we let be a longest path between and in (see Figure 7(a)). Otherwise let be a rectangular subgraph of including such that and O and let be a closest vertex of to distinct from . Then, there is a longest path between and in and a shortest path between and in such that these two paths have no edge in common. Let be the concatenation of these two paths (see Figure 7(b)). Because the length of a longest path between two vertices of a rectangular grid graph is at least , in both cases and . This completes our proof. ∎
See Figures 8(a) and (b) for examples.
6 Extending the results to 3D grids
The problems of embedding length cycles and length paths between two given vertices and in a 3D grid graph can be solved using the introduced algorithms for solving these problems in rectangular grid grpaphs.
To this aim, consider a spanning subgraph of illustrated in Figure 9(b). This subgraph is isomorphic to the rectangular grid graph using the bijective mapping defined by the following:
Since, 3D grid graphs are 2-colorable as rectangular grid graphs, the stated conditions for the existance of the desired cycles and paths in are true also for . Similarly, the upper bounds proposed in  for the length of longest cycle and length of longest path between two given vertices in is true also for . Thus, any longest cycle in indicates a longest cycle in and any longest path between nad in indicates a longest path between and in . Moreover, length of shortest cycles in both and is four. Thus, the mapping of any cycle of length in using is a cycle of length in . However, the same argument is not true about the paths of length , because the length of the shortest paths between and in may be less than the length of the shortests path between and in .
For finding a path of length between the vertices and in , without loss of generality, let and let be vertices of such that , and for . Also, without loss of generality, we can assume that is distinct from , because as otherwise we can swap the roles of and cordinates in the defination of bijection .
Let be the first vertex among such that the length of the shortest path between and in is not more than . Such a exists if is not less than the length of the shortest path between and , because the length of the shortest path between and in is equal to the length of the shortest path between and in minus . Now, if be a path of length between and in (found by the algorithm proposed in Section 5) the concatenation of the mapping of using and the sequence of vertices results a path of length between and in . Note that, the mapping of using will not contain any of vertices , as otherwise the is not the first vertex among with the desired property which is a contradiction.
In this paper we first give the necessary and sufficient conditions for the existence of cycles and paths of a given length , between two given vertices in a rectangular grid graph. Using these conditions one can verify in O time whether such a path or cycle exits. We next introduced an algorithm for finding such cycles in optimal time O, and an algorithm for finding such paths in O time. However, the problems of finding paths and cycles of a given length is still open for solid grid graphs.
-  E. M. Arkin, M. A. Bender, E. D. Demaine, S. P. Fekete, J. S. B. Mitchell, and S. Sethia. Optimal covering tours with turn costs. In Proceedings of the twelfth annual ACM-SIAM symposium on Discrete algorithms, pages 138–147. Society for Industrial and Applied Mathematics, 2001.
-  E. M. Arkin, S. P. Fekete, and J. S. B. Mitchell. Approximation algorithms for lawn mowing and milling. Computational Geometry, 17(1-2):25–50, 2000.
-  S. Cornelsen and T.S.D. Wagner. Drawing graphs on two and three lines. Journal of Graph Algorithms and Applications, 8(2):161–177, 2004.
-  R. Diestel. Graph Theory, volume 173 of Graduate Texts in Mathematics. Springer-Verlag, Heidelberg, fourth edition, 2010.
-  S. Dong Chen, H. Shen, and R. Topor. An efficient algorithm for constructing Hamiltonian paths in meshes. Parallel Computing, 28(9):1293–1305, 2002.
-  S. Felsner, G. Liotta, and S. Wismath. Straight-line drawings on restricted integer grids in two and three dimensions. Journal of Graph Algorithms and Applications, 7(4):363–398, 2003.
-  M.R. Garey and D.S. Johnson. Computers and Intractability: A Guide to the Theory of NP-completeness. WH Freeman & Co. New York, NY, USA, 1979.
-  J. Hu and S. S. Sapatnekar. A survey on multi-net global routing for integrated circuits. Integration, the VLSI Journal, 31(1):1–49, 2001.
-  A. Itai, C. H. Papadimitriou, and J. L. Szwarcfiter. Hamiltonian paths in grid graphs. SIAM Journal on Computing, 11(4):676–686, 1982.
-  R. Kastner, E. Bozorgzadeh, and M. Sarrafzadeh. Predictable routing. In Proceedings of the 2000 IEEE/ACM international conference on Computer-aided design, pages 110–114. IEEE Press, 2000.
-  F. Kehsavarz Kohjerdi, A. Bagheri, and A. Asgharian Sardroud. A linear-time algorithm for the longest path problem in rectangular grid graphs. Discrete Applied Mathematics, 160(3):210–217, 2012.
-  J. O’Rourke. Art gallery theorems and algorithms, volume 4. Oxford University Press Oxford, 1987.
-  A. N. M. Salman, H. Broersma, and E. T. Baskoro. Spanning 2-connected subgraphs in alphabet graphs, special classes of grid graphs. Journal of Automata, Languages and Combinatorics, 8(4):675–681, 2003.
-  M. Suderman. Pathwidth and layered drawings of trees. International Journal of Computational Geometry and Applications, 14(3):203–225, 2004.
-  C. Umans and W. Lenhart. Hamiltonian cycles in solid grid graphs. In Proceedings of 38th Annual Symposium on Foundations of Computer Science, pages 496–505. IEEE, 1997.
-  W. Zhang and Y. Liu. Approximating the longest paths in grid graphs. Theoretical Computer Science, 412(39):5340–5350, 2011.