forked from rituburman/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhamiltonian.cpp
More file actions
73 lines (66 loc) · 1.25 KB
/
hamiltonian.cpp
File metadata and controls
73 lines (66 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
#define v 5
using namespace std;
void solution(int path[v])
{
cout<<"solution exists and the cycle is : ";
for(int i=0;i<v;i++)
{
cout<<path[i]<<" ";
}
cout<<path[0]<<endl;
}
bool isSafe(int vi, bool graph[v][v], int path[], int pos)
{
if (graph [path[pos-1]][vi] == 0)
return false;
for (int i = 0; i < pos; i++)
if (path[i] == vi)
return false;
return true;
}
bool hamcycle(bool graph[v][v],int path[v],int pos)
{
if(pos==v)
{
if (graph[ path[pos-1] ][ path[0] ] == 1)
return true;
else
return false;
}
for (int vi = 1; vi < v; vi++)
{
if (isSafe(vi, graph, path, pos))
{
path[pos] = vi;
if (hamcycle(graph, path, pos+1) == true)
return true;
path[pos] = -1;
}
}
return false;
}
bool ham(bool graph[v][v])
{
int *path=new int[v];
for(int i=0;i<v;i++)
{
path[i]=0;
}
path[0]=0;
if(hamcycle(graph,path,1)==false)
{
cout<<"Solution does not exist"<<endl;
return false;
}
solution(path);
return true;
}
int main()
{
bool graph[v][v]={{0,1,0,1,0},{1,0,1,1,1},
{0,1,0,0,1},{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0},};
ham(graph);
return 0;
}