Introduction
.
The Hamiltonian Path or Cycle Problem was formalized as a computational problem during the rise of computer science in 1960s – 1970s.
This period saw the development of algorithms and computational complexity theory. The problem was identified as NP-complete, meaning it is computationally hard to solve efficiently for large graphs.
Also, in the 1960s, algorithms for finding Hamiltonian cycles using backtracking were developed. These algorithms systematically explore all possible paths to find a cycle that visits every vertex exactly once.
According to Michael Garey and David S. Johnson’s book Computers and Intractability: A Guide to the Theory of NP-Completeness and Richard Karp’s list of 21 NP-complete problems both the Hamiltonian path problem and the Hamiltonian cycle problem are NP-complete.
.
Problem Statement
Hamiltonian Path
- Given an undirected or directed graph G, a Hamiltonian path is a sequence of edges that visits each vertex exactly once.
- To check if a Hamiltonian path exists, you need to examine every possible path within the graph and see if one path covers every vertex exactly once.
- Hamiltonian Cycle
- In this problem, we must determine if there’s a Hamiltonian cycle in G, a closed path that starts and ends at the same vertex while visiting each vertex exactly once.
- It’s like a Hamiltonian path, except it requires the path to loop back to the starting point.
.
NP-Completeness
Both the Hamiltonian Path and Cycle problems are NP-complete
- NP – A problem is in NP if, given a solution, it can be verified in polynomial time.
- NP-Complete – A problem is NP-complete if it’s in NP and every other NP problem can be transformed into it in polynomial time.
.
Algorithm for the Hamiltonian Cycle Problem
Due to the NP-completeness, we do not have a polynomial-time algorithm to find a Hamiltonian cycle in a general graph. However, here is a common backtracking approach to tackle the problem.
- Choose a starting vertex (let’s call it u).
- Add u to the path and mark it as visited.
- For each adjacent vertex v of the current vertex, check if v has already been visited in the current path.
- If it has not been visited, add v to the path and recursively attempt to extend the path from v.
- If the current path length equals the number of vertices and there’s an edge from the last vertex back to the starting vertex, we’ve found a Hamiltonian cycle.
- Backtrack if no further extension is possible from the current path.
If we reach a dead end (i.e., we cannot complete a cycle), we backtrack to explore other potential paths.
.
Time Complexity
The time complexity of solving Hamiltonian Path and Cycle problems using backtracking is O(n!). n! tries all possible permutations of vertices, it has factorial time complexity. Because of this exponential growth in time, this approach is impractical for large graphs.
Suppose this is the graph where we have to find out the Hamiltonian path or cycle. We have the algorithm to find out the Hamiltonian cycle or path, but it will take milleniums to generate the correct output.
Roughly, this graph has 75 vertices. The time complexity it has is O(n!), and 75! is 1.0314 × 10206. Thus, this problem is np-complete.
.
Reduction of Hamiltonian Path Problem to Hamiltonian Cycle Problem and vice versa.
Hamiltonian Path to Hamiltonian Cycle | Introduce a new vertex and link it to every other vertex in the graph. |
Hamiltonian Cycle to Hamiltonian Path | Delete one of the edges in the Hamiltonian cycle. |
In both conversions, the modified graph helps transform the nature of the Hamiltonian path or cycle problem while maintaining the graph’s structure, allowing one problem to be solved using a solution to the other. This conversion is in polynomial times and hence if cycle is NP-Complete then path is NP-Complete and vice versa.
.
.
Reduction of 3-SAT to Hamiltonian Cycle Problem
The 3-SAT problem is the first problem that can be proved by scientist, an NP-Complete problem. Reduction of the 3-SAT problem to the Hamiltonian cycle problem will prove that the Hamiltonian cycle problem is also an NP-Complete problem.
To reduce 3-SAT to Hamiltonian Cycle, we need to create a graph where finding a Hamiltonian cycle would be equivalent to finding a satisfying assignment for the 3-SAT formula.
- For each variable xi in the 3-SAT formula, create two nodes representing the literal xi (true) and ¬xi (false).
- For each clause, create a pair of vertices representing the literal xi (true) and ¬xi (false). Also, create edges between xi to ¬xi and ¬xi to
- Add two special vertices, Start and End, connected to the variables to form a cycle.
- Connect the Start vertex to the first x1 vertex and the last ¬x1 vertex back to Start. Connect the first xn vertex to the End vertex, and its last ¬xn vertex, to the End
- Finally, create an edge from End back to Start to complete the cycle.
- For each clause
- If a literal xi is present in a clause, add an edge from xi to the clause vertex and from the clause vertex back to ¬xi.
- If the clause contains ¬xi, connect ¬xi to the clause vertex and back from the clause vertex to xi.
- Verify the Hamiltonian Cycle
- The constructed graph will contain a Hamiltonian cycle if and only if there exists a satisfying assignment for the original 3-SAT formula.
- Check if the cycle formed in the graph includes all vertices without repeating any, indicating that the graph structure aligns with the required assignments.
.
Example
F = (¬x1 V x2 V ¬x3) Λ (x1 V ¬x2 V x3)
Step 1 – For each variable xi in the 3-SAT formula, create two nodes representing the literal xi (true) and ¬xi (false).
Step 2 – For two clauses, create two pair of nodes representing the literal xi (true) and ¬xi (false).
Step 3 – Add two vertices Start and End. Connect the two vertices as shown.
Step 4 – For each clause, C1 = (¬x1 V x2 V ¬x3) and C2 = (x1 V ¬x2 V x3), if a literal xi is present in a clause, add an edge from xi to the clause vertex and from the clause vertex back to ¬xi and vice-versa.
Here ¬x1 is present in the clause C1, so add an edge from ¬x1 to C1 and from C1 to x1. Likewise create the all edges.
Step 4 – Verify the Hamiltonian Path or Cycle.
The Hamiltonian cycle exist in 3-SAT problem. Now to verify the solution Hamiltonian cycle into 3-SAT problem, we must look at the graph and find x1, x2 and x3.
So, x1 = true because the x1 line has left to right edges.
Also, x2 = true because the x2 line has left to right edges
Then, x3 = false because the x3 line has right to left edges.
F = (¬x1 V x2 V ¬x3) Λ (x1 V ¬x2 V x3)
F = (False V True V True) Λ (True V False V False)
F = True Λ True
F = True
Hence, 3-SAT Problem is true. it means that solving Hamiltonian Path Problem or Cycle Problem would allow us to solve 3-SAT as well.
Travelling Salesman Problem is also NP-Complete.
The Traveling Salesman Problem (TSP) is not explicitly listed among Richard Karp’s original 21 NP-complete problems in his 1972 paper. However, the TSP Decision Problem is closely related to the Hamiltonian Circuit problems listed in Karp’s work:
- Directed Hamilton Circuit (now known as Directed Hamiltonian Cycle)
- Undirected Hamilton Circuit (now known as Undirected Hamiltonian Cycle)
These Hamiltonian problems form the foundation for showing the NP-completeness of TSP’s decision version. The TSP’s optimization version is NP-Hard.
Test Yourself
Q1- What are the steps to transform a Hamiltonian Path problem into a Hamiltonian Cycle problem?
To convert a Hamiltonian Path problem to a Hamiltonian Cycle, introduce a new vertex and link it to every other vertex in the graph. This added vertex allows the path to close into a cycle by connecting each endpoint of the path to it.
Q2- What is the primary challenge in solving the Hamiltonian Cycle problem in large graphs?
The primary challenge lies in the exponential number of possible cycles due to factorial time complexity O(n!), which makes the problem computationally infeasible for large graphs.
Q3- What is the purpose of creating pairs of vertices for each variable in the reduction from 3-SAT to Hamiltonian Path/Cycle?
In the reduction, each variable in the 3-SAT formula is represented by a pair of vertices: one for the variable itself (e.g., x) and one for its negation (e.g., ¬x). These pairs ensure that only one of the two (either x or ¬x) can be included in a Hamiltonian Path or Cycle. This structure effectively represents the binary choice of assigning either “true” or “false” to each variable in the 3-SAT formula, thus preserving the logic of variable assignments in the graph representation.
Q4- Why are additional “clause gadgets” necessary in the 3-SAT to Hamiltonian Path/Cycle reduction?
Clause gadgets are necessary to enforce the logical OR operation for each clause in the 3-SAT formula. They ensure that at least one literal in each clause is selected (or true) to satisfy the clause. In the graph, these gadgets connect to variable vertices corresponding to literals in the clause. This forces the Hamiltonian Path/Cycle to traverse the gadget in a way that mimics the satisfaction of each clause in the formula, thus guaranteeing that all clauses are satisfied if a Hamiltonian Path/Cycle exists.
Q5- How does the reduction from 3-SAT to Hamiltonian Path/Cycle demonstrate that the Hamiltonian Path problem is NP-complete?
The reduction shows that any instance of the NP-complete 3-SAT problem can be transformed into an equivalent instance of the Hamiltonian Path/Cycle problem. This means that solving the Hamiltonian Path problem could solve any problem in NP. Since the reduction is done in polynomial time, it proves that the Hamiltonian Path problem is at least as hard as 3-SAT, thus confirming its NP-completeness.
Q6- In the context of 3-SAT to Hamiltonian Cycle reduction, what role does the “Start” and “End” vertex play?
The “Start” and “End” vertices serve as endpoints for the Hamiltonian Path. They help ensure that the path covers all vertices in the correct order, mimicking the constraints of the 3-SAT formula. By connecting these vertices appropriately with variable and clause gadgets, we construct a path that must satisfy all clauses to reach the “End” vertex, ensuring that any valid Hamiltonian Path corresponds to a satisfying assignment for the 3-SAT instance.
Q7- Why do we add edges between each literal and its negation in the reduction?
Edges between each literal and its negation enforce the constraint that only one of them can be included in the Hamiltonian Path/Cycle.
This structure represents the exclusive choice of assigning either “true” or “false” to each variable in the 3-SAT formula. If both were included, it would violate the path requirement of visiting each vertex exactly once, thereby preserving logical consistency in the reduction.
Q8- What does it mean if we find a Hamiltonian Cycle in the graph constructed from a 3-SAT formula?
If a Hamiltonian Cycle exists in the graph, it implies that there is a way to traverse all vertices (representing literals and clauses) such that each clause is satisfied by at least one literal. This corresponds to a satisfying assignment of the variables in the 3-SAT formula, where each clause is “true” due to at least one literal being true, thus satisfying the entire formula.
Q9- What is the main approach used to find Hamiltonian cycles?
Greedy algorithms
Divide and Conquer
Backtracking
Dynamic Programming
Ans – (3)
Explanation – Backtracking is commonly used, as it explores possible cycles recursively and systematically.
Q10- What is the purpose of adding a new vertex when reducing a Hamiltonian Path to a Hamiltonian Cycle?
To complete the path as a cycle
To simplify the problem
To reduce computation
To connect to the end vertex
Ans – (1)
Explanation – Adding a new vertex linked to all others enables the path to loop back and form a cycle.
Q11- What kind of graph always has a Hamiltonian Path?
Tree
Complete graph
Bipartite graph
Disconnected graph
Ans – (2)
Explanation – Complete graphs have a Hamiltonian Path (and Cycle) as there is an edge between every pair of vertices, allowing traversal without constraints.
Q12- Which of the following problems is not directly reducible to the Hamiltonian Path problem?
3-SAT problem
Traveling Salesman Problem
Eulerian Path problem
Knapsack problem
Ans – (3)
Explanation – The Eulerian Path problem, focusing on edges rather than vertices, is fundamentally different and not reducible to Hamiltonian Path.