HybridHAM : A Novel Hybrid Heuristic for Finding Hamiltonian Cycle

Hamiltonian Cycle Problem is one of the most explored combinatorial problems. Being an NP-complete problem, heuristic approaches are found to bemore powerful than exponential time exact algorithms. This paper presents an efficient hybrid heuristic that sits in between the complex reliable approaches and simple faster approaches. The proposed algorithm is a combination of greedy, rotational transformation and unreachable vertex heuristics that works in three phases. In the first phase, an initial path is created by using greedy depth first search. This initial path is then extended to a Hamiltonian path in second phase by using rotational transformation and greedy depth first search. Third phase converts the Hamiltonian path into a Hamiltonian cycle by using rotational transformation. The proposed approach could find Hamiltonian cycles from a set of hard graphs collected from the literature, all the Hamiltonian instances (1000 to 5000 vertices) given in TSPLIB, and some instances of FHCP Challenge Set. Moreover, the algorithm has O(n) worst case time complexity. The performance of the algorithm has been compared with the state-of-the-art algorithms and it was found that HybridHAM outperforms others in terms of running time.


Introduction
The Hamiltonian Cycle Problem (HCP) is to identify a cycle in an undirected graph connecting all the vertices in the graph.It is considered as a subproblem of the most popular NP-complete problem, the Travelling Salesman Problem (TSP), where the problem is to find the minimum weighted Hamiltonian cycle.Hamiltonian cycles have many applications like reconstructing genome sequences, solving games like Icosian game, finding a knight's tour on a chessboard, and finding circular embeddings for regular graphs.There is no single efficient algorithm for this problem till date.The stateof-the-art algorithms are mainly classified into two: exponential time exhaustive search algorithms and polynomial time heuristic algorithms.While the first category guarantees giving solution, the latter does not.The latter gives solution in sufficiently less time in most of the cases compared to the first.The algorithms in the first category generally find some efficient pruning rules for reducing the search space and thus improving the running time, while those in the second category find some general thump rules for finding the solution in as many problem instances as possible, in less time.The objective of this research is to design a heuristic which is quicker than the established sophisticated heuristics, but which is more reliable than the very fast techniques.
Many theorems can be found in literature, giving the necessary and sufficient conditions [1][2][3][4] for Hamiltonian cycle.These conditions are used for checking whether the graph is Hamiltonian or not.A good study [5] on these theorems and algorithms for solving HCP is given by Vandegriend & Basil.Rubin & Frank [6] proposed an exhaustive search method for finding all Hamiltonian paths or cycles in a directed or undirected graph.Christophides [7] proposed a multipath algorithm that was again an intelligent search algorithm with exponential complexity.Christophides algorithm has been improved by Kocay & William [8] by proposing two operations for pruning the search space.Martello [9] proposed a backtrack search algorithm that uses low degree first heuristic for selecting the next vertex.Ejov et al [10] solved HCP by solving equations drawn from the adjacent matrix of the graph.Even though they could find long cycles in case of non-Hamiltonian graphs, they could not reduce the exponential time complexity.Posa's algorithm [11] is considered as the base algorithm for the heuristic algorithms for HCP.Posa's idea of rotational transformation and its variants formed the basis of almost all heuristic algorithms proposed later.Angluin and Valiant [12] proposed a much more complicated transformation for directed graph, as the rotational transformation is not suitable for directed graph.Bollobas et al. [13] proposed a Hamiltonian cycle algorithm called HAM that uses rotational transformation and cycle extension.Various versions of HAM algorithm like SparseHam [14] and HideHam [15] are also proposed for different kinds of graphs.Brunacci [16] proposed two algorithms DB2 and DB2A, which consider the HCP as a version of TSP and the nonedges as highly weighted edges.DB2A algorithm is a modification of DBA algorithm for directed graphs where directed graph is transformed into undirected graph.Many TSP heuristics like the famous Lin-Kernighan heuristic [17,18] use a technique called "k-opt" transformation [19,20], which is an exchange of k edges.Baniasadi et al. [21] proposed a "snakes and ladders" heuristic for solving HCP inspired from k-opt transformations.There are very few published HCP heuristics that sit in the "middle" area between sophisticated reliable heuristics and very simplistic (usually linear or quadratic time) approaches.
The proposed heuristic is a combination of greedy depth first search, unreachable vertex, and rotational transformation heuristics.The greedy depth first search reduces the running time considerably.The search is greedy since it always selects the unvisited low degree vertex for extending the path.While the unreachable vertex heuristic reduces the chances of reaching dead end, the rotational transformation helps to come out of the dead end condition.Thus the proposed method is faster than the complex exact algorithms and more reliable than the faster heuristics.

Materials and Methods
. .Hamiltonian Cycle Problem.Hamiltonian cycle is a cycle connecting all the vertices in a given graph only once.A graph containing at least one Hamiltonian cycle is called Hamiltonian graph.This optimization problem can be formally defined as follows: Given a graph G=(E,V), where E is the set of edges of the graph, V is the set of vertices of the graph and |V| = n.
It is a hard problem that attracted both mathematicians and computer scientists.It is considered as one of the strong representatives of the NP-complete problem.
. .Greedy Depth First Search.The large search space of the HCP can be explored either breadth wise or depth wise.In depth first search, search is performed in depth wise manner starting from a given vertex till the point where the search cannot proceed further or a dead end is reached.The proposed heuristic uses greedy depth first search to create a Hamiltonian path.The path construction starts from the highest degree vertex because it increases the probability of returning to the starting vertex.Degree of a vertex is the number of edges connected to that vertex.The other vertices of the path are selected greedily by selecting the smallest degree neighbour among the unvisited neighbours.The lowest degree vertices are added first into the path since they may become unreachable with the selection of its neighbours.For example, if the degree of a vertex is two then both the edges of that vertex must be present in the Hamiltonian path/cycle and these edges are added to the path first.The vertices that are part of the path constructed so far are considered as "visited."Since in Hamiltonian cycle a vertex appears only once, only the unvisited neighbours of the current vertex need to be considered.In order to guide the greedy depth first search further to longer paths, an unreachable vertex heuristic is proposed.According to this heuristic, before adding a vertex to the path, an unreachable condition is checked for each of its neighbours.This heuristic is explained in Section 2.3.
. .Unreachable Vertex Heuristic.This research proposes a new heuristic, namely, unreachable vertex heuristic, to reduce the chance of reaching a dead end while constructing the Hamiltonian path.
Definition .Let P be the partial path and x be the end vertex on partial path.Select the next vertex y∈Adj(x) in the path such that the number of unvisited adjacent vertices of all Adj(y) vertices is greater than one.
A vertex is said to be unreachable if all its neighbours are already a part of the path constructed so far.In this case, there is no way to reach the vertex and it becomes unreachable.In this proposed heuristic, if the selection of a vertex is making any of its unvisited neighbours unreachable then that vertex will not be added to path.
For example, consider the graph shown in Figure 1.
Let the partial path identified so far be A→C→D.In the given graph Adj(D) = {B, E}.Let the next vertex selected be E. Adj(E) = {B, G, F}.Number of unvisited neighbours of B, G and F are 1, 2 and 2 respectively.According to the unreachable vertex heuristic, E will not be selected as next vertex in the path since the number of unvisited neighbours of B is one.
. .Rotational Transformation.Rotational transformation [11] and its variations are found to be powerful heuristics for finding Hamiltonian cycle.It is used to change a path to get a new end vertex as shown in Figure 2. In the figure, e is the end on which rotational transformation is applied to get a new end vertex.The procedure of rotational transformation is summarized below.
(1) Find a vertex adjacent to e in the input graph, in path P. Let it be vertex b.
( In the proposed algorithm, rotational transformation is used for two purposes. (1) To come out of the dead end during the construction of Hamiltonian path.
(2) To convert the Hamiltonian path into Hamiltonian cycle.
In the first case highest degree end of the path is selected for rotational transformation since it increases the probability of getting a new end.Similarly for converting path into cycle, the lowest degree end vertex of the path is selected for rotational transformation since it increases the probability of returning to the higher degree end to make the cycle.

Proposed Hybrid Heuristic
. .HybridHAM Algorithm.The proposed HybridHAM algorithm works in three steps: . . .Initial Path Creation.The path creation starts from the highest degree vertex and then adds the smallest degree vertices greedily in order to construct an initial path as long as possible.The selection of highest degree initial vertex and then the smallest degree vertices is meant for constructing a longer initial path and is given in Table 1.The algorithm checks the unreachable condition before adding a vertex in the path.This is to reduce the probability of reaching a dead end during the path construction.If there are more than one highest degree vertices then repeat this procedure of creating initial path by selecting each one of them as the starting vertex and select the path with maximum number of vertices as the initial path.By following this procedure, we may get an initial path which is Hamiltonian or near Hamiltonian.
. . .Conversion of Initial Path into Hamiltonian Path.If the initial path created is not Hamiltonian (less number of vertices than the total number of vertices), then select the highest degree end of the initial path for rotational transformation.This is to increase the probability for getting a new end vertex for extending the path further to create a Hamiltonian path.Extend the path from the new end vertex by following the greedy procedure in Section 3.1.1.The rotational transformation and greedy path extension are continued until we are getting a Hamiltonian path.At any time during this process, rotational transformation could not be applied means that either the graph is not having Hamiltonian path or the algorithm fails to identify the Hamiltonian path.

. . . Conversion of Hamiltonian Path into Hamiltonian Cycle.
If there is an edge connecting the two ends of the Hamiltonian path in the graph then add that edge to the path to get the Hamiltonian cycle.Otherwise, apply rotational transformation to the smallest degree vertex repeatedly until getting a new end vertex which can be connected to the other end vertex to form the Hamiltonian cycle.At any time during this process, rotational transformation could not be applied, means that either the graph is not having Hamiltonian cycle or the algorithm fails to identify the Hamiltonian cycle.
The vertex selection criteria used in various steps are summarized in Table 1.

. . . Initial Path Identified in
Step 0 1 2 3 4 5 6 7 8 12 13 14 22 21 20 19 18 15 Here the length of the initial path is 18, which is less than the total number of vertices in the graph.Since the identified path is not Hamiltonian, go to step 2.Here the length of the path is 24 and hence it is Hamiltonian path.Since there is no edge in Figure 3, connecting the end vertices of the path (i.e., edge connecting vertex 1 and vertex 11), go to step 3 and apply rotational transformation to find the new end vertices that are connected in the graph in Figure 3 Table 1: Vertex selection criteria.

S tep Reason
Selection of Highest degree initial vertex.
Step 1 It increases the probability of getting a path to reach back to this vertex to form the cycle.
Greedy selection of smallest degree vertices during path construction.
Step 1 Giving preference to smaller degree vertices increases the probability of longer paths.For example, if the degree of a vertex adjacent to the current end vertex of the path is two, then it should be included first in the path, as it is the only possible position of the vertex in the Hamiltonian path.
Selection of the highest degree end of the initial path for rotational transformation Step 2 It increases the probability for getting a new end vertex for extending the path further to create a Hamiltonian path Selection of the smallest degree end vertex for rotational transformation Step 3 This is done to keep the highest degree end vertex, since the probability of getting an edge connecting a higher degree vertex is more compared to a smaller degree vertex. . .Pseudocode.The input to the algorithm is the adjacency matrix representation of the graph and the output is the set of vertices corresponding to the Hamiltonian cycle.Let  be the number of vertices in the graph.The proposed algorithm contains three phases and is outlined as follows: From the input adjacency matrix, create two arrays   and   of vertices sorted in the increasing and decreasing order of their degrees, respectively.

//Phase 1 //Create an initial path
(1) Start from one of the highest degree vertex (first vertex in the array   ).Let it be V  .
(2) Add it to the initial path   .
(3) Repeat (a) Select the next smallest degree neighbour of V  (from the array   ).Let it be V  .
(b) If the selection of V  is not making any of its unvisited neighbours unreachable then (i) add it to the initial path   (ii) make V  as V  Until V  becomes a dead end.

//End
(a) Select the smallest degree end of the path  ℎ for rotational transformation (b) Reverse  ℎ if the first vertex in  ℎ is having degree higher than the last vertex to make the smallest degree vertex as the end vertex of the path.Let it be V  .
(c) Apply rotational transformation to  ℎ using V  to get a new path.(d) If rotational transformation could not be applied then either the graph is not having Hamiltonian Cycle or the algorithm fails to identify the Hamiltonian path and so exit.
Until there is an edge connecting the first and last vertices of the path  ℎ .( 9 Greedy depth first search goes through the adjacency matrix once in the worst case and hence its complexity is O(n 2 ).This search is repeated for each of the highest degree vertex.The worst case is all the  vertices are of the same degree.

Experiments
The performance evaluation experiments of the proposed algorithm are conducted in a system with 4GB RAM and Intel Core i5 processor.The algorithm is implemented in MATLAB 13RA.The experiments were carried out on a set of graphs collected from the literature as well as from the TSPLIB.
. .Sample Hamiltonian Cycle Graphs.The Hamiltonian cycles returned by the proposed algorithm on the collected set of sample graphs are shown in Figure 5.
The running time of the algorithm to solve these sample instances is given in Table 2.This dataset is specifically designed to resist the heuristic approaches.HybridHAM is tested on 250 instances of the FHCP Challenge Set.Out of these 250 instances, the proposed heuristic could find 75 Hamiltonian paths and 6 Hamiltonian cycles.The complete results are shown in Table 4.The graphs are too sparse so that the rotation transformation could not convert the Hamiltonian path into Hamiltonian cycle in Phase 3 of the algorithm.Phase 1 and Phase 2 of the algorithm perform comparatively well with the highly difficult instances of the Challenge Set and hence the Hamiltonian path.It is also found that the proposed highest degree and smallest degree vertex selection criteria suit more graphs having vertices of different degrees.However, the algorithm could find Hamiltonian cycle from medium sparse graphs in very less time (e.g., Graphs 72, 79, 84, 90, etc. in Table 4).Even the winners [26] of the challenge could not solve the complete set and they used different algorithms for different types of graphs as their objective was to find solutions rather than developing algorithms.
It would be worth pointing out that the difficult instances of FHCP challenge dataset are very rare and difficult to construct, let alone encounter "naturally." Also even discovering a Hamiltonian path is a difficult (NPcomplete) problem and the proposed heuristic succeeded in doing so for many instances of the FHCP Challenge Set.

Conclusion
This paper proposes a hybrid heuristic for finding Hamiltonian cycle from undirected graphs.The proposed threestage algorithm uses a combination of three heuristics: greedy depth first search, unreachable vertex, and rotational transformation.From the various experiments conducted on different graphs of variable sizes and complexity, it is found that the highest degree heuristic used for selecting the initial vertex for the creation of longest initial path as well as Hamiltonian path and the smallest degree heuristic used for the conversion of Hamiltonian path to Hamiltonian cycle are the reasons for getting the solution in a single run.The experimental evaluation also shows that the proposed heuristic is much faster and succeeds in obtaining good results in the majority of cases.It should be worth noting that the increase in speed comes without sacrificing the heuristic's reliability on "easy" instances of various sizes (including the quite large TSPLIB instances) and that it still performs reasonably well on more difficult instances.
) Create a new path P' , (a) by connecting b to e (b) by reversing the path from c to e

( 1 )
Create an initial path (2) Convert the initial path to Hamiltonian path.(3) Convert the Hamiltonian path to Hamiltonian cycle.

( 5 )//End of Phase 2 ( 7 )
Repeat(a) Select the highest degree end of the path   for rotational transformation (b) Reverse   if the first vertex in   is having degree higher than the last vertex to make the highest degree vertex as the end vertex of the path.Let it be V  .(c) Apply rotational transformation to   using V  to get a new path.(d) If rotational transformation could not be applied then either the graph is not having Hamiltonian path or the algorithm fails to identify the Hamiltonian path and so exit.(e) Extend this new path by using the greedy depth first search as in Phase 1. Until |  | = .(6) Now   is the Hamiltonian Path  ℎ .Assign  ℎ =   .If there is an edge connecting the first and last vertices of  ℎ in the graph then  ℎ is the Hamiltonian cycle and return  ℎ

1 )
) Now  ℎ is the Hamiltonian cycle and return  ℎ .//End of Phase 3 }// End of HybridHAM algorithm . .Worst Case Complexity (Creation of sorted arrays Therefore, T(n) = O(n 3 ) (3) Phase Complexity of rotational transformation at the most is O(n) Complexity of greedy depth first search is O(n 2 ) Total of these two = O(n 2 ) These two operations are repeated at most  times.Therefore, T(n) = O(n 3 ) (4) Phase 3 Complexity of rotational transformation at the most is O(n) This operation is repeated at most  times.Therefore, T(n) = O(n 2 ) Total worst case complexity of HybridHam, T(n) = O(n 2 ) + O(n 3 )+ O(n 3 )+ O(n 2 ) =O( 3 ).

Table 2 :
Running time on sample instances.

Table 3 :
Running time on TSPLIB instances.

of vertices Running time in sec.
[25]proposed algorithm could solve all these problem instances in a few seconds.The running time of the proposed algorithm is compared with that of HCP Solver [ ], Concorde TSP Solver [ ], and the latest Snake and Ladder Heuristic [ ]. Table3gives the time taken by these algorithms to solve each of the HCP instances.. .FHCP Challenge Set.The FHCP Challenge Set[25]is a collection of 1001 Hamiltonian Cycle Problem instances.