Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Hamiltonian_Cycle.cpp #2314

Merged
merged 1 commit into from
Mar 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions Hamiltonian_Cycle/Hamiltonian_Cycle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*

Hamiltonian Cycle - Hamiltonian Path in an undirected adjmat is a path
that visits each vertex exactly once.

A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian Path
such that there is an edge (in the adjmat) from the last vertex to the
first vertex of the Hamiltonian Path.

For more information
Watch "https://www.youtube.com/watch?v=dQr4wZCiJJ4"
This gives a clear explanation regarding the solution

*/

#include<iostream>

#define NODE 5
using namespace std;

int path[NODE];

void printcycle() {

//Prints the first Hamiltonian cycle found
cout<<"Hamiltonian Cycle: ";

for (int i = 0; i < NODE; i++)
cout << path[i] << " ";

}

bool isValid(int adjmat[NODE][NODE], int v, int k) {

if (adjmat [path[k-1]][v] == 0)
return false;

for (int i = 0; i < k; i++)
if (path[i] == v)
return false;

return true;
}

bool cyclepresent(int adjmat[NODE][NODE], int k) {

if (k == NODE) {
if (adjmat[path[k-1]][ path[0] ] == 1 )
return true;

else
return false;
}

for (int v = 1; v < NODE; v++) {
if (isValid(adjmat, v, k)) {
path[k] = v;

if (cyclepresent (adjmat, k+1) == true)
return true;

path[k] = -1;
}
}
return false;
}

bool hamilcycle(int adjmat[NODE][NODE]) {

for (int i = 0; i < NODE; i++)
path[i] = -1;

path[0] = 0;

if ( cyclepresent(adjmat, 1) == false ) {
cout << "Solution does not exist"<<endl;
return false;
}

printcycle();
return true;
}

int main() {


cout<<"Enter the connections between the nodes\n";
cout<<"Nodes will be marked from 0 to 4\n";
cout<<"Enter 0 0 to end the input process\n";

int x,y;
int adjmat[NODE][NODE];

x=1;
y=1;
for(int i=0;i<NODE;i++)
{
for(int j=0;j<NODE;j++)
{
adjmat[i][j]=0;
}
}
cin>>x>>y;
while(x!=0 || y!=0)
{
adjmat[x][y]=1;
adjmat[y][x]=1;
cin>>x>>y;
}

hamilcycle(adjmat);
}

/*

Sample Input

0 1
0 3
1 3
1 4
1 2
2 4
3 4
0 0

Sample Output

Hamiltonian Cycle: 0 1 2 4 3

*/