-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycle-detection.cpp
87 lines (76 loc) · 2.31 KB
/
cycle-detection.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
// Program to detect whether a graph contains a cycle. The vertices of the graph are
// placed in disjoint sets. As each edge is iterated through, the subset of the two
// vertices is determined. If the subsets are the same, then a cycle is found.
// Otherwise, the algorithm will join the two subsets together and repeat the process
// until each edge is iterated through or a cycle is found.
#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <utility>
#include <vector>
// Edge list implementation of unweighted, undirected graph
class Graph
{
private:
std::vector<std::pair<int, int>> edgeList;
std::unordered_set<int> uniqueVertices; // Keep track of the number of unique vertices
public:
void insertEdge(int from, int to);
bool isCycle();
int findSet(std::vector<int>& parent, int i);
void unionSet(std::vector<int>& parent, int x, int y);
};
void Graph::insertEdge(int from, int to)
{
edgeList.push_back(std::make_pair(from, to));
uniqueVertices.insert(from);
uniqueVertices.insert(to);
}
bool Graph::isCycle()
{
// Initialize parent vector as disjoint sets
std::vector<int> parent(uniqueVertices.size(), -1);
// Iterate through all graph edges
for (auto i : edgeList)
{
// Find the subset of both vertices of an edge
int x = findSet(parent, i.first);
int y = findSet(parent, i.second);
// If the subsets are the same, then there is a cycle in the graph
if (x == y)
{
return true;
}
unionSet(parent, x, y);
}
return false;
}
int Graph::findSet(std::vector<int>& parent, int i)
{
if (parent[i] == -1)
{
return i;
}
return findSet(parent, parent[i]);
}
void Graph::unionSet(std::vector<int>& parent, int x, int y)
{
parent[x] = y;
}
// Sample test case
// Time complexity is O(E) where E is the number of edges
int main()
{
Graph graph;
graph.insertEdge(0, 1);
graph.insertEdge(0, 2);
graph.insertEdge(1, 2);
if (graph.isCycle())
{
std::cout << "Graph contains a cycle" << std::endl;
}
else{
std::cout << "Graph does not contain a cycle" << std::endl;
}
return 0;
}