-
Notifications
You must be signed in to change notification settings - Fork 1
/
3 cycle.cpp
96 lines (77 loc) · 2.23 KB
/
3 cycle.cpp
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
/*
3 Cycle
Given a graph with N vertices (numbered from 0 to N-1) and M undirected edges, then count the distinct 3-cycles in the graph.
A 3-cycle PQR is a cycle in which (P,Q), (Q,R) and (R,P) are connected by an edge.
Input Format :
The first line of input contains two space separated integers, that denotes the value of N and M.
Each of the following M lines contain two integers, that denote the vertices which have an undirected edge between them.
Let us denote the two vertices with the symbol u and v.
Output Format :
Print the count the number of 3-cycles in the given graph
Constraints :
0 <= N <= 100
0 <= M <= (N*(N-1))/2
0 <= u <= N - 1
0 <= v <= N - 1
Time Limit: 1 sec
Sample Input 1:
3 3
0 1
1 2
2 0
Sample Output 1:
1
*/
#include<iostream>
#include<vector>
using namespace std;
void countCycles(vector<vector<bool>>& graph, vector<bool>& visited, int current, int start, int length, int& count) {
visited[current] = true;
// If a path of length 2 is found
if(length == 0) {
visited[current] = false;
if(graph[current][start]) {
count++;
return;
} else {
return;
}
}
for(int i = 0; i < graph.size(); i++) {
if(!visited[i] and graph[current][i]) {
countCycles(graph, visited, i, start, length - 1, count);
}
}
visited[current] = false;
}
int main() {
// Write your code here
int v, e;
cin >> v >> e;
vector<vector<bool>> graph(v, vector<bool>(v, false));
// corner case
if(e == 0) {
cout << 0 << endl;
return 0;
}
for(int i = 0; i < e; i++) {
int first, second;
cin >> first >> second;
graph[first][second] = true;
graph[second][first] = true;
}
vector<bool> visited(v, false);
int count = 0;
for(int i = 0; i < v; i++) {
if(!visited[i]) {
// For a 3 cycle from i we nee to find a path to vertex i of length 2
countCycles(graph, visited, i, i, 2, count);
visited[i] = true;
}
}
// Since every path will be counted twice so we divide by 2
cout << count / 2 << endl;
return 0;
}
// Time Complexity : O(V^2)
// Auxillary Space : O(V^2)