Skip to content

Commit 6abcab0

Browse files
committed
Add code for Hamiltonian Cycle in C++.
1 parent 0cd1de2 commit 6abcab0

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/* C++ program for solution of Hamiltonian
2+
Cycle problem using backtracking */
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
// Number of vertices in the graph
8+
#define V 5
9+
10+
void printSolution(int path[]);
11+
12+
/* A utility function to check if
13+
the vertex v can be added at index 'pos'
14+
in the Hamiltonian Cycle constructed
15+
so far (stored in 'path[]') */
16+
bool isSafe(int v, bool graph[V][V],
17+
int path[], int pos)
18+
{
19+
/* Check if this vertex is an adjacent
20+
vertex of the previously added vertex. */
21+
if (graph [path[pos - 1]][ v ] == 0)
22+
return false;
23+
24+
/* Check if the vertex has already been included.
25+
This step can be optimized by creating
26+
an array of size V */
27+
for (int i = 0; i < pos; i++)
28+
if (path[i] == v)
29+
return false;
30+
31+
return true;
32+
}
33+
34+
/* A recursive utility function
35+
to solve hamiltonian cycle problem */
36+
bool hamCycleUtil(bool graph[V][V],
37+
int path[], int pos)
38+
{
39+
/* base case: If all vertices are
40+
included in Hamiltonian Cycle */
41+
if (pos == V)
42+
{
43+
// And if there is an edge from the
44+
// last included vertex to the first vertex
45+
if (graph[path[pos - 1]][path[0]] == 1)
46+
return true;
47+
else
48+
return false;
49+
}
50+
51+
// Try different vertices as a next candidate
52+
// in Hamiltonian Cycle. We don't try for 0 as
53+
// we included 0 as starting point in hamCycle()
54+
for (int v = 1; v < V; v++)
55+
{
56+
/* Check if this vertex can be added
57+
// to Hamiltonian Cycle */
58+
if (isSafe(v, graph, path, pos))
59+
{
60+
path[pos] = v;
61+
62+
/* recur to construct rest of the path */
63+
if (hamCycleUtil (graph, path, pos + 1) == true)
64+
return true;
65+
66+
/* If adding vertex v doesn't lead to a solution,
67+
then remove it */
68+
path[pos] = -1;
69+
}
70+
}
71+
72+
/* If no vertex can be added to
73+
Hamiltonian Cycle constructed so far,
74+
then return false */
75+
return false;
76+
}
77+
78+
/* This function solves the Hamiltonian Cycle problem
79+
using Backtracking. It mainly uses hamCycleUtil() to
80+
solve the problem. It returns false if there is no
81+
Hamiltonian Cycle possible, otherwise return true
82+
and prints the path. Please note that there may be
83+
more than one solutions, this function prints one
84+
of the feasible solutions. */
85+
bool hamCycle(bool graph[V][V])
86+
{
87+
int *path = new int[V];
88+
for (int i = 0; i < V; i++)
89+
path[i] = -1;
90+
91+
/* Let us put vertex 0 as the first vertex in the path.
92+
If there is a Hamiltonian Cycle, then the path can be
93+
started from any point of the cycle as the graph is undirected */
94+
path[0] = 0;
95+
if (hamCycleUtil(graph, path, 1) == false )
96+
{
97+
cout << "\nSolution does not exist";
98+
return false;
99+
}
100+
101+
printSolution(path);
102+
return true;
103+
}
104+
105+
/* A utility function to print solution */
106+
void printSolution(int path[])
107+
{
108+
cout << "Solution Exists:"
109+
" Following is one Hamiltonian Cycle \n";
110+
for (int i = 0; i < V; i++)
111+
cout << path[i] << " ";
112+
113+
// Let us print the first vertex again
114+
// to show the complete cycle
115+
cout << path[0] << " ";
116+
cout << endl;
117+
}
118+
119+
// Driver Code
120+
int main()
121+
{
122+
/* Let us create the following graph
123+
(0)--(1)--(2)
124+
| / \ |
125+
| / \ |
126+
| / \ |
127+
(3)-------(4) */
128+
bool graph1[V][V] = {{0, 1, 0, 1, 0},
129+
{1, 0, 1, 1, 1},
130+
{0, 1, 0, 0, 1},
131+
{1, 1, 0, 0, 1},
132+
{0, 1, 1, 1, 0}};
133+
134+
// Print the solution
135+
hamCycle(graph1);
136+
137+
/* Let us create the following graph
138+
(0)--(1)--(2)
139+
| / \ |
140+
| / \ |
141+
| / \ |
142+
(3) (4) */
143+
bool graph2[V][V] = {{0, 1, 0, 1, 0},
144+
{1, 0, 1, 1, 1},
145+
{0, 1, 0, 0, 1},
146+
{1, 1, 0, 0, 0},
147+
{0, 1, 1, 0, 0}};
148+
149+
// Print the solution
150+
hamCycle(graph2);
151+
152+
return 0;
153+
}
154+
155+
// This code is taken from geeksforgeeks and was contributed by rathbhupendra

0 commit comments

Comments
 (0)