Hamiltonian Cycles Problem

Distribute Education like Computer Geek

Introduction

.

A Hamiltonian cycle is a special type of cycle in a graph that visits every vertex exactly once and returns to the starting vertex.

Unlike a Eulerian cycle, which visits every edge, a Hamiltonian cycle focuses on visiting every vertex. The challenge of finding such a cycle in a graph is known as the Hamiltonian cycle problem.

.

Early Concepts and Origins

Sir William Rowan Hamilton (1805–1865) – The Hamiltonian cycle is named after the Irish mathematician Sir William Rowan Hamilton, who introduced the concept in the 19th century.

Hamilton devised a mathematical game known as the “Icosian Game” where he sought to find a cycle that passes through each vertex of a dodecahedron (a polyhedron with twelve faces) exactly once before returning to the starting point. His work laid the foundation for the Hamiltonian cycle concept in graph theory.

The Hamiltonian 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.

.

Example – The graph below is a Hamiltonian Graph.

Hamiltonian Cycle Example 1

The vertex A, B, C, D, E, F, A is a Hamiltonian cycle.

This problem is particularly interesting because it belongs to the class of NP-complete problems, meaning it is difficult to solve efficiently for large graphs, because there is an exponential time taking problem.

One of the common approaches to solving this problem is through the backtracking algorithm. Backtracking systematically explores all possible paths in the graph and eliminates those that do not lead to a valid Hamiltonian cycle.

.

Example – Find all possible combinations of Hamiltonian Cycles in the below graph.

Hamiltonian Graphs

.

Graph (A) 

All possible solutions of Hamiltonian Cycles include only one cycle in the above graph, A, B, C, D, E, A.

However, B, C, D, E, A, B is essentially the same as this Hamiltonian cycle, so it does not count as a second Hamiltonian cycle. 

.

Graph (B)

This graph is a pentagon with an inner star-shaped structure Hamiltonian Cycle1 – A, B, C, D, E, A

Hamiltonian Cycle 2 – A, D, C, B, E, A

.

Graph (C)

This graph has two triangles connected by a rectangle. The vertex C is called the articulation point of the graph. These types of graphs do not contain Hamiltonian Cycle.

.

Graph (D)

This is a bipartite graph, and it contains a Hamiltonian Cycle A, B, C, D, E, F, A.

Graph (E)

In this graph, there is a vertex E which is called the pendant vertex, and these pendant graphs do not contain Hamiltonian Cycle.

.

Graph (F)

The graph has no Hamiltonian Cycles.

.

Algorithm of Finding All Hamiltonian Cycles

Initialization

  • Create an empty list to store all Hamiltonian cycles.
  • Create a path array to store the current path being explored.
  • Initialize all vertices as unvisited.

Recursive Function – findCycles(v, path)

  • If all vertices are visited and there is an edge from the last vertex in the path back to the first vertex, then
    • Add the first vertex to the end of the path (to complete the cycle).
    • Save this path as a Hamiltonian cycle.
    • Remove the first vertex from the end of the path (to continue searching for other cycles).
  • If all vertices are visited but there is no edge from the last vertex back to the first, then backtrack (return).
  • For each unvisited adjacent vertex u of v
    • Mark u as visited.
    • Add u to the path.
    • Recur with findCycles(u, path).
    • Backtrack by removing u from the path and marking it as unvisited.

Start the Algorithm

  • Start from any vertex (say vertex 0) and call findCycles(0, path).
  • Return the list of Hamiltonian cycles.

.

Example – The Hamiltonian graph for backtrack is

Hamiltonian Graph with backtrack

.

Step 1 – Create a path array to store the current path being explored. Initialize all vertices as unvisited (0).

Hamiltonian Cycle – {0, 0, 0, 0, 0}

.

Step 2 – Start at vertex A as the stating vertex.

Hamiltonian Cycle – {A, 0, 0, 0, 0}

.

Hamiltonian Graph with backtrack root A

.

Step 3 – In adjacency list, for the current vertex A, look at all the connected vertices (where 1 is present). Here, vertices B, D, and E are connected to A because the matrix has 1 in those positions.

Hamiltonian Cycle – {A, 0, 0, 0, 0}

Hamiltonian Graph with backtrack B, D & E

.

Step 4 – Next, we take B connected vertices because B is in extreme left.

Hamiltonian Cycle – {A, B, 0, 0, 0}

Hamiltonian Graph with backtrack C & D

.

Step 5 – Next, we take C connected vertices because C is in the extreme left.

This goes on and on …

 

The full backtracking tree is –

Hamiltonian Graphs with backtracking

Hamiltonian Cycle 1 – A, B, C, D, E, A

Hamiltonian Cycle 2 – A, E, D, C, B, A

The second cycle is just reverse of the first cycle. If the graph is directed graph, then the reverse case will not apply.

.

Source Code for Hamiltonian Cycle Problem

Time complexity

The time complexity for finding all Hamiltonian cycles in an undirected graph using backtracking is generally discussed in the context of the worst case is O(V!), where V is the number of vertices in the graph.

In the best case, time complexity is O(V) if the graph is extremely sparse or if no Hamiltonian cycle is found early in the search.

In average case, the algorithm might be able to prune some branches of the search tree early, especially in sparser graphs where fewer Hamiltonian cycles exist.

However, the average-case complexity is still dominated by the factorial growth due to the large number of possible vertex orderings that must be checked. Therefore, it is often considered to be O(V!) as well.

.

Space Complexity

Since the algorithm uses backtracking, the recursion depth is equal to the number of vertices V. Therefore, the space required for the recursion stack is O(V).

The algorithm needs to store the current path being explored, which requires O(V) space.

An additional array is used to keep track of which vertices have been visited, which also requires O(V) space.

Therefore, the space complexity for finding all Hamiltonian cycles using backtracking is O(V).

.

Applications of the Hamiltonian Cycle problem
  1. Network Design – In designing computer networks, finding a Hamiltonian cycle can help in ensuring that every node (router or switch) is visited exactly once, optimizing the efficiency of data transmission.
  2. Tourism and Travel – For planning tours or travel routes where each city or location needs to be visited exactly once and return to the starting point, Hamiltonian cycles can help find the optimal route.
  3. Circuit Design – In electronic circuit design, Hamiltonian cycles can be used to layout circuits so that each component is connected in an efficient manner, minimizing the total length of wiring needed.
  4. Robot Path Planning – For robots that need to cover a grid or map visiting each location exactly once and return to the starting point, finding a Hamiltonian cycle can assist in planning the robot’s path.
  5. Puzzles and Games – Many puzzles and games, like certain variations of the Travelling Salesman Problem (TSP) or Hamiltonian path games, use Hamiltonian cycles to create challenging and interesting scenarios.

Test Yourself

Q1- Explain what a Hamiltonian cycle is and how it differs from a Eulerian cycle.

A Hamiltonian cycle is a cycle in a graph that visits every vertex exactly once and returns to the starting vertex.

Unlike a Eulerian cycle, which covers every edge of the graph exactly once, a Hamiltonian cycle focuses on visiting each vertex exactly once.

Q2- Describe the backtracking approach to finding Hamiltonian cycles.

The backtracking approach systematically explores all possible paths in the graph.

It starts at a vertex, adds adjacent vertices to the path one by one, and backtracks if a vertex cannot be added without violating the Hamiltonian cycle conditions.

Q3- Why is the Hamiltonian cycle problem classified as NP-complete?

The Hamiltonian cycle problem is NP-complete because there is no known polynomial-time algorithm to solve it, and verifying a solution is feasible in polynomial time.

It requires exponential time to explore all possible vertex orderings for large graphs.

Q4- Can a graph with all vertices having odd degrees contain a Hamiltonian cycle?

Not necessarily. The degree of vertices is not the sole determinant of the existence of a Hamiltonian cycle. The cycle depends on whether a path can be formed that visits each vertex exactly once.

Hamiltonian Graph with odd degree
Hamiltonian Graph with odd degree
Q5- If a Hamiltonian cycle exists in a graph, does it imply that the graph is complete?

No, a Hamiltonian cycle can exist in non-complete graphs.

A complete graph has all possible edges, but a Hamiltonian cycle only requires a path covering all vertices exactly once.

Q6- Can removing an edge from a graph destroy a Hamiltonian cycle?

Yes, if the removed edge is part of the Hamiltonian cycle, it can destroy the cycle by breaking the path that connects all vertices.

Q7- Is a Hamiltonian path also a Hamiltonian cycle?

No, a Hamiltonian path visits all vertices exactly once but does not return to the starting vertex. Only when it returns to the starting vertex does it become a Hamiltonian cycle.

Q8- What is the time complexity of finding all Hamiltonian cycles using backtracking?
  1. O(V)
  2. O(V^2)
  3. O(V!)
  4. O(2^V)

Ans – (3)

Explanation – The factorial time complexity arises due to the need to explore all possible vertex orderings.

Q9- Which of the following graphs does NOT have a Hamiltonian cycle?
  1. Complete graph
  2. Graph with a pendant vertex
  3. Graph with a Hamiltonian path
  4. Cycle graph

Ans – (2)

Explanation – A graph with a pendant vertex cannot have a Hamiltonian cycle because it breaks the necessary cycle structure.

Q10- In a Hamiltonian cycle problem, if a graph has 5 vertices, how many vertices does the path array have?
  1. 4
  2. 5
  3. 6
  4. 10

Ans – (2)

Explanation – The path array stores the vertices in the Hamiltonian cycle, so it has the same number of entries as there are vertices in the graph.

Q11- What is the minimum number of vertices required for a graph to have a Hamiltonian cycle?
  1. 1
  2. 2
  3. 3
  4. 4

Ans – (3)

ExplanationA minimum of three vertices is needed to form a cycle.

Q12- In the Hamiltonian cycle problem, what happens when all vertices are visited but there is no edge from the last vertex to the first?
  1. The cycle is complete.
  2. The algorithm backtracks.
  3. A new vertex is added.
  4. The cycle is printed.

Ans – (2)

Explanation – Backtracking occurs because the cycle cannot be completed without connecting to the starting vertex.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha

Â