-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPP-1.c
90 lines (75 loc) · 2.29 KB
/
PP-1.c
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Hamiltonian Path in an undirected graph 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 graph) from the last vertex to the first vertex of the Hamiltonian Path. Determine whether a given graph contains Hamiltonian Cycle or not. If it contains, then prints the path. Following are the input and output of the required function.
// Input:
// First the number of vertices in the graph V.
// Next
// A 2D array graph[V][V] where V is the number of vertices in graph and graph[V][V] is adjacency matrix representation of the graph. A value graph[i][j] is 1 if there is a direct edge from i to j, otherwise graph[i][j] is 0.
// Output:
// An array path[V] that should contain the Hamiltonian Path. path[i] should represent the ith vertex in the Hamiltonian Path. The code should also return false if there is no Hamiltonian Cycle in the graph.
// For example, a Hamiltonian Cycle in the following graph is {0, 1, 2, 4, 3, 0}.
// (0)-----(1)-----(2)
// | / \ |
// | / \ |
// | / \ |
// (3)-------------(4)
// Input:
// 5
// 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
// Ouput:
// The hamiltonian cycle is 0 1 2 4 3 0
#include<stdio.h>
int find(int n,int a[n][n],int map[],int ans[],int p,int v)
{
if(v==n)
{
if(a[p][0]==1)
{
ans[n]=0;
printf("The hamiltonian cycle is ");
for(int i=0;i<=n;i++)
printf("%d ",ans[i]);
return 1;
}
else
return 0;
}
else
{
for(int i=0;i<n;i++)
{
if(a[p][i]==1 && map[i]==0 )
{
ans[v]=i;
map[i]=1;
if(find(n,a,map,ans,i,v+1))
return 1;
else
{
map[i]=0;
ans[v]=-1;
}
}
}
}
return 0;
}
int main()
{
int n;
scanf("%d",&n);
int a[n][n],map[n],ans[n+1];
for(int i=0;i<n;i++)
map[i]=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
map[0]=1;
ans[0]=0;
if(!find(n,a,map,ans,0,1))
printf("The hamiltonian cycle does not exist");
}