This repository was archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathCycleDetection.cpp
More file actions
139 lines (106 loc) · 3.3 KB
/
CycleDetection.cpp
File metadata and controls
139 lines (106 loc) · 3.3 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
vector<pair<int, int>>* getEdgeSet(unordered_map<int, vector<int>*> &graph, int &edgeCount) {
// NUMBER OF VERTICES
int V = graph.size();
vector<pair<int, int>> *v = new vector<pair<int, int>>();
for(int i=0; i<V; i++) {
int n = graph[i] -> size();
for(int j=0; j<n; j++) {
int curr = graph[i]->at(j);
if(curr >= i) {
v->push_back(make_pair(i, curr));
}
}
}
edgeCount = v->size();
return v;
}
int find(int *parent, int p) {
int currParent = p;
while(parent[currParent] != -1) {
currParent = parent[currParent];
}
return currParent;
}
void unionn(int *parent, int p1, int p2) {
parent[p2] = p1;
}
bool detectCycle(unordered_map<int, vector<int>*> &graph) {
// NUMBER OF VERTICES
int V = graph.size();
// NUMBER OF EDGES
int edgeCount = 0;
// FUNCTION TO GET THE EDGE SET FROM AN ADJACENT LIST
vector<pair<int, int>> *edges = getEdgeSet(graph, edgeCount);
// UTILITY PARENT SET FOR CYCLE DETECTION, INITIALISED WITH -1
int *parent = new int[V];
for(int i=0; i<V; i++) {
parent[i] = -1;
}
for(int i=0; i<edgeCount; i++) {
// FIND THE PARENT OF BOTH VERTICES OF EDGE
int p1 = find(parent, edges->at(i).first);
int p2 = find(parent, edges->at(i).second);
// IF P1 AND P2 ARE SAME THEN BOTH VERTICES LIE IN SAME SET
// INCLUDING THIS EDGE RESULTS IN A CYCLE
if(p1 == p2)
return true;
// ELSE IF P1 NOT EQUAL TO P2, THEN BOTH VERTICES ARE IN DIFFERENT SETS
// INCLUDE BOTH IN SAME SET
unionn(parent, p1, p2);
}
// NO CYCLE, RETURN FALSE
return false;
}
int main() {
/*
0
/ | \
/ | \
1 2 3
| | \
4___5___6 7
*/
// INITIAL INPUT FOR NUMBER OF VERTICES AND EDGES
int V = 8, E = 8;
// GRAPH AS ADJACENCY LIST
unordered_map<int, vector<int> *> graph;
for(int i=0; i<V; i++) {
graph[i] = new vector<int>();
}
// INPUT FOR EDGES
int **edges = new int*[E];
for(int i=0; i<E; i++) {
edges[i] = new int[2];
}
edges[0][0] = 0;edges[0][1] = 1;
edges[1][0] = 0;edges[1][1] = 2;
edges[2][0] = 0;edges[2][1] = 3;
edges[3][0] = 1;edges[3][1] = 4;
edges[4][0] = 4;edges[4][1] = 5;
edges[5][0] = 2;edges[5][1] = 5;
edges[6][0] = 5;edges[6][1] = 6;
edges[7][0] = 7;edges[7][1] = 3;
// INPUT FOR NO CYCLE IN GRAPH, MAKE E=7 ABOVE ON LINE 93
// edges[0][0] = 0;edges[0][1] = 1;
// edges[1][0] = 7;edges[1][1] = 3;
// edges[2][0] = 0;edges[2][1] = 3;
// edges[3][0] = 1;edges[3][1] = 4;
// edges[4][0] = 4;edges[4][1] = 5;
// edges[5][0] = 2;edges[5][1] = 5;
// edges[6][0] = 5;edges[6][1] = 6;
//FORMING THE ADJACENCY LIST
for(int i=0; i<E; i++) {
graph[edges[i][1]] -> push_back(edges[i][0]);
graph[edges[i][0]] -> push_back(edges[i][1]);
}
// IF CYCLE EXISTS RETURNS TRUE
bool isCycle = detectCycle(graph);
if(isCycle)
cout << "There is a cycle in the graph\n";
else
cout << "No cycle in the graph\n";
}