-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgraph cycle
More file actions
97 lines (85 loc) · 1.78 KB
/
graph cycle
File metadata and controls
97 lines (85 loc) · 1.78 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// CPP program to find single cycle components
// in a graph.
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
// degree of all the vertices
int degree[N];
// to keep track of all the vertices covered
// till now
bool found[N];
// all the vertices in a particular
// connected component of the graph
vector<int> curr_graph;
// adjacency list
vector<int> adj_list[N];
// depth-first traversal to identify all the
// nodes in a particular connected graph
// component
void DFS(int v)
{
found[v] = true;
curr_graph.push_back(v);
for (int it : adj_list[v])
if (!found[it])
DFS(it);
}
// function to add an edge in the graph
void addEdge(vector<int> adj_list[N], int src,
int dest)
{
// for index decrement both src and dest.
src--, dest--;
adj_list[src].push_back(dest);
adj_list[dest].push_back(src);
degree[src]++;
degree[dest]++;
}
int countSingleCycles(int n, int m)
{
// count of cycle graph components
int count = 0;
for (int i = 0; i < n; ++i) {
if (!found[i]) {
curr_graph.clear();
DFS(i);
// traversing the nodes of the
// current graph component
int flag = 1;
for (int v : curr_graph) {
if (degree[v] == 2)
continue;
else {
flag = 0;
break;
}
}
if (flag == 1) {
count++;
}
}
}
return(count);
}
int main()
{
// n->number of vertices
// m->number of edges
int n = 15, m = 14;
addEdge(adj_list, 1, 10);
addEdge(adj_list, 1, 5);
addEdge(adj_list, 5, 10);
addEdge(adj_list, 2, 9);
addEdge(adj_list, 9, 15);
addEdge(adj_list, 2, 15);
addEdge(adj_list, 2, 12);
addEdge(adj_list, 12, 15);
addEdge(adj_list, 13, 8);
addEdge(adj_list, 6, 14);
addEdge(adj_list, 14, 3);
addEdge(adj_list, 3, 7);
addEdge(adj_list, 7, 11);
addEdge(adj_list, 11, 6);
cout << countSingleCycles(n, m);
return 0;
}