Efficient solution for finding Hamilton cycles in undirected graphs
 Wadee Alhalabi^{1}Email author,
 Omar Kitanneh^{2},
 Amira Alharbi^{2},
 Zain Balfakih^{2} and
 Akila Sarirete^{2}
Received: 8 January 2016
Accepted: 4 July 2016
Published: 28 July 2016
Abstract
The Hamilton cycle problem is closely related to a series of famous problems and puzzles (traveling salesman problem, Icosian game) and, due to the fact that it is NPcomplete, it was extensively studied with different algorithms to solve it. The most efficient algorithm is not known. In this paper, a necessary condition for an arbitrary undirected graph to have Hamilton cycle is proposed. Based on this condition, a mathematical solution for this problem is developed and several proofs and an algorithmic approach are introduced. The algorithm is successfully implemented on many Hamiltonian and nonHamiltonian graphs. This provides a new effective approach to solve a problem that is fundamental in graph theory and can influence the manner in which the existing applications are used and improved.
Keywords
Undirected graphs Hamiltonian cycle Necessary condition Set partitionIntroduction and preliminaries
The Hamiltonian cycle (HC) problem has many applications such as time scheduling, the choice of travel routes and network topology (Bollobas et al. 1987; Akhmedov and Winter 2014). Therefore, resolving the HC is an important problem in graph theory and computer science as well (Pak and Radoičić 2009). It is known to be in the class of NPcomplete problems and consequently, determining if a graph is Hamiltonian, using the current algorithms, if it has a high time complexity. The difficulty of finding HC increases exponentially with the problem size. Also, there is an algorithm for solving the HC problem with polynomial expected running time (Bollobas et al. 1987).
A Hamiltonian path is a path in an undirected graph that visits each vertex exactly once. A Hamiltonian cycle is the cycle that visits each vertex once. A Hamiltonian graph is a graph that has a Hamiltonian cycle (Hertel 2004).
Due to their similarities, the problem of an HC is usually compared with Euler’s problem, but solving them is very different. There exists a very elegant, necessary and sufficient condition for a graph to have Euler Cycles. Also, literature presents many solutions that generate efficient algorithms for finding Euler Cycles. Unfortunately, there is a lack of solutions for the Hamilton problem. In addition to the author’ knowledge, there is no research indicating a necessary and sufficient condition for a graph to have a HC. Some of the theorems provide finding sufficient conditions for a graph to be Hamiltonian [such as Ore’s (1961) and Dirac’s (1952) theorems]. Nevertheless, the conditions in those theorems are complicated, and they are not applicable in many situations.
In this context, this paper introduces necessary conditions for a graph to have HC. This condition leads to build an efficient algorithm to find HC. The proposed algorithm depends on a numerical term which we call surplus edges.
Definition 1
If G(V, E) is an undirected graph and v ∊ V is a vertex in G, then we define the surplus of v, denoted by sur(v), to be sur(v) = deg (v) − 2. The surplus of the vertex v represents the number of edges that are incident to v and that must be removed so that the degree of v is 2.
In this paper, another new term used is: “nfactor graphs”. It is an extension to the family of Hamiltonian graphs.
Definition 2
An undirected graph G(V, E) is nFactor graph if, and only if there exists, a positive integer n and G _{1}(V _{1}, E _{1}), G _{2}(V _{2}, E _{2}),…, G _{ n }(V _{ n }, E _{ n }) cycles and subgraphs of G with {V _{1}, V _{2},…, V _{ n }} is a partition for V. Moreover, the set {G _{1}, G _{2},…, G _{ n }} is called a factorized representation of G and the G _{ i }’s are called the factors of the representation. The n is the number of factors.
The remainder of this paper is as follows. In “Literature review” section, a short literature review related to the problem of HC is done. “Deletion and nfactor graphs” section tackles the problem of deletion and nFactor Graphs, the basis of the algorithm proposed being detailed step by step. In “Results and discussion” section, the results of different case studies (examples) were given. The last section concludes the paper.
Literature review
HC plays an important role in many areas including graph theory, algorithm design, and computational complexity. It has been studied widely in the past 100 years (Ibarra 2009). Many sufficient conditions for Hamiltonicity have been discovered from which two of the most notable are Dirac’s sufficient condition (Dirac 1952) and Ore’s theorems (Ore 1961).
Dirac’s theorem
A simple graph with n vertices in which each vertex has degree at least \(\left\lceil {n/2} \right\rceil\) has a Hamiltonian cycle.
Ore’s theorem
A simple graph with n vertices in which the sum of the degrees of any two nonadjacent vertices is greater than or equal to n has a Hamiltonian cycle.
An algorithm for finding a HC in a proper interval graph in O(m + n) time is presented by Ibarra (2009) where m is the number of edges and n is the number of vertices in the graph. The algorithm is simpler and shorter than the previous versions. An interval graph is a graph where each vertex can be assigned an interval on the real line so that two vertices are adjacent if, and only if, their assigned intervals intersect; such an assignment is an interval representation. A proper interval graph is an interval graph with an interval representation where no interval is properly contained in another.
In another work, Nivasch (2003) tested two algorithms to determine their performance for practical problems. The two algorithms used are Ham and SemiHam. Ham was introduced by Bollobas et al. (1987), and it is heuristic polynomialtime algorithm for finding Hamiltonian cycles in random graphs with high probability.
In order to improve the Hamiltonian Cycle function of the Combinatorica, Csehi and Toth (2011) proposed an alternative solution for finding HC by testing if a HC exists. This is performed by checking the biconnectivity of the graph and other conditions such as those described in the theorems of Dirac (1952), Ore (1960), Posa (1962) and Lawler et al. (1985).
A similar approach for finding HC in directed graphs is proposed by Christofides et al. (2012). It uses several known criteria for the existence of HC and provides algorithmic proof for, and oriented analogue of, Dirac theorem and an approximate version of Chvatal theorem.
Other researchers focused on specific structures. Liu and Wang (2014) demonstrated that, in a hypercube with a set of faulty edges, there is a fault free HC if two conditions are satisfied: the degree of every vertex is at least two and there are no pairs of adjacent vertices (with degree two) in a 4 cycle. In a similar study, Hao et al. (2014) derived that in a balanced hypercube, for any fault free edge exists a fault free HC containing it, and having 2n2 faulty edges.
Deletion and nfactor graphs
In order to find a HC, the edges that form it must be chosen so that any extra edges are avoided. In other words, the edges that do not belong to this cycle must be deleted. Definition 3 below introduces the primary way of deleting such edges.
Definition 3
A deletion of a graph G is a random way of removing edges that are not incident to vertices with zero surpluses until we cannot delete more edges. The edges that are deleted are called surplus edges, and the deletion process is denoted by D.
Definition 4
In a deletion for a graph G (V,E), an alternate edge path from vertex u to vertex v is defined to be a path in the graph G which is labeled in the deletion by X,O,X,… Or O,X,O,X,… In this case, “X” labels the removed edges, while “O” labels the nonremoved edges.

If the number of edges in an alternate edge path is odd, then the path is called an “odd alternate edge path”.

If the odd alternate edge path starts with O (and so ends with O because its length is odd), then it is called a “Destroyer”.

If the number of edges in an alternate edge path is even, then the path is called an “even alternate edge path”.

If the even alternate edge path is a cycle from the vertex to itself, then it is called a “Connector”.
Definition 5
The inverse of an alternate edge path: XOXO… ntimes is the same path but with reflecting the X’s and the O’s, that is: OXOX… ntimes.
Definition 6
In a deletion D for a graph G (V,E), the set of vertices remained with surplus >0 (degree > 2) is called the Remaining Set induced by D, it is denoted by S_{D}.
The following example illustrates the deletion process.
Example 1
Consider the following graph G.
Now, no more edges can be removed because each edge is incident with at least one vertex with degree two (zero surplus).

Initially, a random deletion was applied so that the degree of every vertex is 2 (as discussed in Definition 3).

The path f,g,l,m,h is an even alternate edge path with labeling XOXO,

The path b,g,f,k is a destroyer with labeling P: OXO, according to Definition 4.

According to Definition 5, the inverse of the destroyer b,g,f,k is b,g,f,k with labeling P^{−1}: XOX.

The remaining set of our deletion is S _{ D } = {b,d,k,o}.
The following theorem provides a necessary and sufficient condition for a graph to be an nfactor graph.
Theorem 1
A graphG(V, E) is an nfactor graph if and only, if in any deletion D with a remaining set SD the following condition is satisfied: “∀u ∈ S _{ D }∃v ∈ S _{ D } such that there exists a destroyer from u to v”.
Proof
(←) If S _{ D } = φ, then there is nothing to prove. If not, that is, if S _{ D } ≠ φ, then by given ∀u ∈ SD∃v ∈ S _{ D } and a destroyer, say P: u, O, × 1, X, x2, O,…, X, x _{ n }, O, v, where x _{ i }, are the vertices between u and v through which the destroyer P passes and i = 1,2,…n. By reflecting the destroyer P, its inverse is P ^{−1}: u, X, x _{1}, O, x _{2}, X,…, O, x _{ n }, X, v. This process reduces with one the degree of u and v without affecting the degrees of any x _{ i }, and this is because, simultaneously, one edge is added to each vertex x _{ i } and one edge is removed from each vertex x _{ i }. Finally, if this process is repeated for the other vertices in SD, then some edges can be deleted so that the degree of each vertex in the resulting graph is 2. As a result, it is S _{ D } = ϕ. This new graph is a factorized representation for graph G.
Example 2
Figure 3, it can be observed that \(C_{1}\): (g,h,m,l,g), \(C_{2}\): (q,l,g,h,m,n,s,r,q), \(C_{3}\): (m,n,s,r,m),\(C_{4}\): (h,i,n,m,h), \(C_{5}\): (c,d,i,h,c) and \(C_{6}\): (i,j,o,n,i) are connectors, and there could be other connectors.
Reflecting any connector in an nfactor graph will transform it into an mfactor graph, where m is not necessarily to be equal to n. Therefore, the objective is to find the set of reflections that lead to the 1factor graph, which is the Hamilton cycle of the original graph.
The concept behind reflecting a connector in a factorized representation is just adding k _{ i } and removing k _{ i } edges from each vertex v _{ i }. This means that reflecting connectors preserves the degree of each vertex but change the status of the graph from a representation to another.
Therefore, if in a factorized representation of a graph there exists m different connectors, then to determine whether the graph is Hamiltonian or not, at most 2 ^{ m } situations of the connectors must be tested. Knowing that each connector C has two situations C and C ^{−1 }, each situation is called a switch.
For a certain graph, HC doesn’t have to be unique, and this is equivalent to the affirmation that the connectors that produce HC do not have to be unique. Distinctively to the Euler’s cycle (in which the determination of the direction of moving to pass each edge exactly one time is required), in the case of the Hamilton problem, the right edges that lead to pass each vertex once must be selected.
The previous arguments suggest that the idea of factorized representation, and the idea of connectors, will lead together to find the desired Hamilton cycle. Consequently, based on Theorem 1, starting with some nfactor representation (if possible, otherwise the graph is not Hamiltonian), the target is to make n = 1. In other words, the scope is to join the n components by using some of the connectors.
Based on these aspects, a set of illustrative cases are presented below.
Example 4
In Example 5 it can be observed that the factorized representation of the Petersen graph is {G_{1}, G_{2}} where G_{1} is the red plot, and G_{2} is the blue plot. The components of the representation cannot be joined because reflecting any of the connectors will transfer the representation into another representation of two components; hence the Petersen graph is not Hamiltonian, although it is nfactor graph. In order for the reader to have a better understanding of this aspect, we will reflect all connectors.
Results and discussion
In order to evaluate the proposed algorithms, their implementation was performed. The IDE used was Eclipse with JRE 7 and 1.6.0_37 JDK in combination with JGraph and JGraphT (open source java class libraries for the interface design) (http://www.jgraph.com, http://www.jgrapht.org). The specifications of the machine used in the experiments are 2 GB of RAM and 2.2 Ghz CPU speed with iOS version 10.8 as an operating system.
Conclusion
As the existing solutions relate to the HC problem, this paper proposed a precise mathematical procedure for determining HC. Based on this procedure, an algorithm has been proposed and successfully implemented. The simulations results indicated that the algorithm works as expected. The graphs were visualized using a Java Applet with the aid of the JGraph library, where the steps of the algorithm are shown. Several graphs have been tested, and in all the cases, the algorithm provided successful results.
One of the possibilities of algorithm improvement include adding more constraints to the destroyer and connector functions, thus further reducing the search time. Also, computing the complexity of the algorithm and testing the algorithm on a variety of extensive graphs, needs to be implemented for future improvement.
Declarations
Authors’ contributions
WA: Wrote the algorithm. OK: Introduced the mathematical problem, and proposed the mathematical solution. ZB: Follow up with the student in the implementing the code in Java. AS: Wrote the paper. AA: The student who implemented the algorithm in Java. All authors read and approved the final manuscript.
Competing interests
The authors declare that they have no competing interests.
Open AccessThis article is distributed under the terms of the Creative Commons Attribution 4.0 International License (http://creativecommons.org/licenses/by/4.0/), which permits unrestricted use, distribution, and reproduction in any medium, provided you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons license, and indicate if changes were made.
Authors’ Affiliations
References
 Akhmedov A, Winter M (2014) Chordal and timbral morphologies using Hamiltonian cycles. J Math Music 8(1):1–24View ArticleGoogle Scholar
 Bollobas B, Fenner TI, Frieze AM (1987) An algorithm for finding Hamilton paths and cycles in random graphs. Combinatorica 7(4):327–341View ArticleGoogle Scholar
 Christofides D, Keevash P, Kohn D, Osthus D (2012) Finding Hamilton cycles in robustly expanding digraphs. J Graph Algorithms Appl 16(2):335–358View ArticleGoogle Scholar
 Csehi C, Toth J (2011) Search for Hamiltonian cycles. Math J 13:1–16Google Scholar
 Dirac GA (1952) Some theorems on abstract graphs. Proc Lond Math Soc 3(1):69–81View ArticleGoogle Scholar
 Hao RX, Zhang R, Feng YQ, Zhou JX (2014) Hamiltonian cycle embedding for fault tolerance in balanced hypercubes. Appl Math Comput 244:447–456Google Scholar
 Hertel A (2004) Hamiltonian cycles in sparse graphs. Master of Science Thesis, University of TorontoGoogle Scholar
 Ibarra L (2009) A simple algorithm to find Hamiltonian cycles in proper interval graphs. Inf Process Lett 109(18):1105–1108View ArticleGoogle Scholar
 Lawler EL, Lenstra JK, Rinnooy Kan AHG, Shmoys DB (1985) The traveling salesman problem, chap 11. John Wiley, Chichester, UK, pp 403–429Google Scholar
 Liu JJ, Wang YL (2014) Hamiltonian cycles in hypercubes with faulty edges. Inf Sci 256:225–233View ArticleGoogle Scholar
 Nivasch G (2003) Experimental results on Hamiltoniancyclefinding algorithms. Technical Report, Weizman Institute, Israel, May 2003Google Scholar
 Ore O (1960) Note on Hamilton circuits, vol 67, no 1. American Mathematical Monthly, Mathematical Association of America, p 55Google Scholar
 Ore O (1961) Arc coverings of graphs. Annali di Matematica Pura ed Applicata 55(1):315–321View ArticleGoogle Scholar
 Pak I, Radoičić R (2009) Hamiltonian paths in Cayley graphs. Discrete Math 309(17):5501–5508View ArticleGoogle Scholar
 Posa L (1962) A theorem concerning Hamilton lines. Magyar Tud Akad Mat Kutat Int K 7:225–226Google Scholar
 http://www.jgraph.com. Last accessed 20 February 2015
 http://www.jgrapht.org. Last accessed 20 February 2015