-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathHamilton Cycle.c
More file actions
78 lines (73 loc) · 1.55 KB
/
Hamilton Cycle.c
File metadata and controls
78 lines (73 loc) · 1.55 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
74
75
76
77
78
#include <stdio.h>
#define MAX 25
int x[MAX];
void Next_Vertex(int G[MAX][MAX], int n, int k)
{
int j;
while (1)
{
x[k] = (x[k] + 1) % (n + 1);
if (x[k] == 0)
return;
if (G[x[k - 1]][x[k]] != 0)
{
for (j = 1; j <= k - 1; j++)
{
if (x[j] == x[k])
break;
}
if (j == k)
{
if ((k < n) || ((k == n) && (G[x[n]][x[1]] != 0)))
return;
}
}
}
}
void H_Cycle(int G[][MAX], int n, int k)
{
int i;
while (1)
{
Next_Vertex(G, n, k);
if (x[k] == 0)
return;
if (k == n)
{
printf("\n");
for (i = 1; i <= n; i++)
printf(" %d", x[i]);
printf(" %d", x[1]);
}
else
H_Cycle(G, n, k + 1);
}
}
int main()
{
int i, j, v1, v2, Edges, n, G[MAX][MAX];
printf("\n\t Program for Hamiltonian Cycle ");
printf("\n Enter the number of vertices of graph: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
G[i][j] = 0;
x[i] = 0;
}
}
printf("\n Enter the total number of edges: ");
scanf("%d", &Edges);
for (i = 1; i <= Edges; i++)
{
printf("\n Enter the edge: ");
scanf("%d%d", &v1, &v2);
G[v1][v2] = 1;
G[v2][v1] = 1;
}
x[1] = 1;
printf("\n Hamiltonian cycle ...\n");
H_Cycle(G, n, 2);
return 0;
}