-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathncyclesofgraph.cpp
More file actions
59 lines (57 loc) · 1.32 KB
/
ncyclesofgraph.cpp
File metadata and controls
59 lines (57 loc) · 1.32 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
/*
* C++ Program to Find Number of Cycles in a Graph
*/
#include<iostream>
#define SIZE 20
using namespace std;
bool map[SIZE][SIZE], F;
long long f[1 << SIZE][SIZE], res = 0;
/*
* Main: count the number of cycles in a graph
*/
int main()
{
int n , m, i, j, k, l, x, y;
cout<<"Enter number of vertices: ";
cin>>n;
cout<<"Enter number of edges: ";
cin>>m;
for (i = 0; i < m; i++)
{
cout<<"Enter source vertex of an edge: ";
cin>>x;
cout<<"Enter destination vertex of an edge: ";
cin>>y;
x--;
y--;
if (x > y)
swap(x, y);
map[x][y] = map[y][x] = 1;
f[(1 << x) + (1 << y)][y] = 1;
}
for (i = 7; i < (1 << n); i++)
{
F = 1;
for (j = 0; j < n; j++)
{
if (i & (1 << j) && f[i][j] == 0)
{
if (F)
{
F = 0;
k = j;
continue;
}
for (l = k + 1; l < n; l++)
{
if (i & (1 << l) && map[j][l])
f[i][j] += f[i - (1 << j)][l];
}
if (map[k][j])
res += f[i][j];
}
}
}
cout<<"Number of Cycles: "<<res/2<<endl;
return 0;
}