# Hamilton Cycle Detection in C++

In this article, we will discuss Hamilton Cycle Detection in C++ with several examples.

## What is Hamiltonian Cycle?

Hamiltonian Cycle or Circuit G is a cycle that travels around each vertex exactly once before returning to the first vertex. A graph is referred to as Hamiltonian if it has a Hamiltonian cycle; otherwise, it is non-Hamiltonian.

There is currently no effective solution available to address the well-known NP-complete problem of finding a Hamiltonian Cycle in a graph for all known forms of graphs. It can be solved for small or specialized types of graphs.

The Hamiltonian Cycle problem has real-world applications in a number of disciplines, including computer science, network design, and logistics.

## What do you mean by Hamiltonian Path?

In a graph G, a Hamiltonian Path is a path that visits each vertex precisely once and is not required to go back to the starting vertex. Finding a Hamiltonian Path in a general graph is NP-complete and can be difficult, much like the Hamiltonian Cycle issue. Although, it is frequently a simpler job than locating a Hamiltonian Cycle.

Applications for Hamiltonian Paths include circuit design, graph theory research, and determining the best paths in transportation networks.

### Backtracking:

Backtracking is an algorithmic method for solving problems based on recursion that involves creating each and every solution incrementally and eliminating any solutions that fail to satisfy the restrictions of the problem at any moment.

Let's review the definition of a Hamiltonian cycle.

### Hamiltonian cycle:

In order to return to the starting vertex from any source vertex, we must visit each of the vertices exactly once.

Let's examine the problem now.

### Problem Statement:

Let us take an undirected graph with N nodes. This graph is shown as an adjacency matrix with dimensions N x N. It is our task to print every conceivable Hamiltonian cycle.

### Example:

This is how the provided graph is shown:

The given graph is represented by an adjacency matrix.

{ 0, 1, 1, 0, 0, 1 }

{ 1, 0, 1, 0, 1, 1 }

{ 1, 1, 0, 1, 0, 0 }

{ 0, 0, 1, 0, 1, 0 }

{ 0, 1, 0, 1, 0, 1 }

{ 1, 1, 0, 0, 1, 0 }

Output:

0 1 2 3 4 5 0

0 1 5 4 3 2 0

0 2 3 4 1 5 0

0 2 3 4 5 1 0

0 5 1 4 3 2 0

0 5 4 3 2 1 0

We can see that by going over every vertex in the graph exactly once, we can get to our source (in this case, 0), by traversing the graph in the form 0->1->2->3->4->5->0. As shown in our report, we may repeat the same process using several pathways.

### Working mechanism:

The next step is to take the following method in order to print every Hamiltonian cycle in an undirected graph.

1. In order to determine whether the vertex being considered at the moment is not already part of the path and is not next to the preceding vertex, we must first build a function.
2. The vertex can be safely counted if the first two conditions are met.
3. After that, the function to locate all Hamiltonian cycles will be defined. There will be a path from the last vertex to the first vertex and all of the graph's vertices will be present.
4. The source vertex will be a part of our path, and the full path will be printed. After printing the path, the source vertex must be removed.
5. To find a new path for the upcoming vertices, repeat the aforementioned stages.
6. We will print all conceivable Hamiltonian cycles. We will mention that no hamiltonian cycles are feasible if the aforementioned conditions are not met.

To Print All Hamiltonian Cycles in an Undirected Graph, Use This Pseudocode:

procedure isSafe(int v, int graph[][6], vector<int> path, int pos):

procedure FindHamCycle(int graph[][6], int pos, vector<int> path, bool visited[], int N):

procedure hamCycle(int graph[][6], int N):

### Program:

Output:

```Hamiltonian Cycle: 0 1 2 3 4 5 0
```

### Complexity Evaluation

Time Complexity: O(N!).

N! iterations of the matrix are performed, where N is the number of nodes in the network.

Space complexity: O(N).

The space needed to print the paths will occupy N nodes because there are N nodes in the graph.