
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Check Whether a Hamiltonian Cycle or Path Exists in a Given Graph
A Hamiltonian cycle is a Hamiltonian Path such that there is an edge (in graph) from the last vertex to the first vertex of the Hamiltonian Path. It is in an undirected graph is a path that visits each vertex of the graph exactly once.
Functions and purposes:
Begin 1.function isSafe() is used to check for whether it is adjacent to the previously added vertex and already not added. 2. function hamiltonianCycle() solves the hamiltonian problem. 3. function hamCycle() uses hamiltonianCycle() to solve the hamiltonian problem. It returns false if there is no Hamiltonian Cycle possible, otherwise return true and prints the path. End
Example
#include <iostream> #include <cstdio> #include <cstdlib> #define N 5 using namespace std; void displaytheSolution(int path[]); bool isSafe(int n, bool g[N][N], int path[], int pos) { if (g [path[pos-1]][n] == 0) return false; for (int i = 0; i < pos; i++) if (path[i] == n) return false; return true; } bool hamiltonianCycle(bool g[N][N], int path[], int pos) { //If all vertices are included in Hamiltonian Cycle if (pos == N) { if (g[ path[pos-1] ][ path[0] ] == 1) return true; else return false; } for (int n = 1; n < N; n++) { if (isSafe(n, g, path, pos)) //Check if this vertex can be added to Hamiltonian Cycle { path[pos] = n; //recur to construct rest of the path if (hamiltonianCycle (g, path, pos+1) == true) return true; path[pos] = -1; //remove vertex if it doesn’t lead to the solution } } return false; } bool hamCycle(bool g[N][N]) { int *path = new int[N]; for (int i = 0; i < N; i++) path[i] = -1; //put vertex 0 as the first vertex in the path. If there is a Hamiltonian Cycle, then the path can be started from any point //of the cycle as the graph is undirected path[0] = 0; if (hamiltonianCycle(g, path, 1) == false) { cout<<"\nCycle does not exist"<<endl; return false; } displaytheSolution(path); return true; } void displaytheSolution(int p[]) { cout<<"Cycle Exists:"; cout<<" Following is one Hamiltonian Cycle \n"<<endl; for (int i = 0; i < N; i++) cout<<p[i]<<" "; cout<< p[0]<<endl; } int main() { bool g[N][N] = { {0, 1, 0, 1, 1}, {0, 0, 1, 1, 0}, {0, 1, 0, 1, 1}, {1, 1, 1, 0, 1}, {0, 1, 1, 0, 0}, }; hamCycle(g); return 0; }
Output
Cycle Exists: Following is one Hamiltonian Cycle 0 4 1 2 3 0
- Related Articles
- C++ Program to Check if a Given Graph must Contain Hamiltonian Cycle or Not\n
- C++ Program to Find Hamiltonian Cycle in an UnWeighted Graph
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Cycle
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Path
- C++ Program to Find Whether a Path Exists Between 2 Given Nodes
- C++ Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
- C# Program to check whether a directory exists or not
- Program to check whether odd length cycle is in a graph or not in Python
- C++ Program to Check Whether an Undirected Graph Contains a Eulerian Path
- Java Program to check whether a file exists or not
- C++ Program to Check Cycle in a Graph using Topological Sort
- C++ Program to Check Whether a Graph is Strongly Connected or Not
- Program to check whether given graph is bipartite or not in Python
- C# program to check whether a given string is Heterogram or not
- Hamiltonian Cycle

Advertisements