-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycleDetectionUnionFind.cpp
More file actions
114 lines (97 loc) · 2.26 KB
/
CycleDetectionUnionFind.cpp
File metadata and controls
114 lines (97 loc) · 2.26 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
#include <iostream>
#include <vector>
#include <unordered_map>
// QUESTION: https://www.techiedelight.com/union-find-algorithm-cycle-detection-graph/
// REFERENCE: https://algorithms.tutorialhorizon.com/graph-find-cycle-in-undirected-graph-using-disjoint-set-union-find/
struct Edge
{
int src;
int dest;
};
class DisjointSet
{
private:
std::unordered_map<int, int> parent;
std::unordered_map<int, int> rank;
public:
DisjointSet(int N)
{
for (int i = 1; i <= N; ++i)
{
parent[i] = i;
rank[i] = 0;
}
}
int find(int x)
{
if (x != parent[x])
{
parent[x] = find(parent[x]);
}
return parent[x];
}
void makeUnion(int x, int y)
{
int rootx = find(x);
int rooty = find(y);
if (rootx == rooty)
return;
if (rank[rootx] > rank[rooty]) {
parent[rooty] = rootx;
} else if (rank[rooty] > rank[rootx]) {
parent[rootx] = rooty;
} else {
parent[rootx] = rooty;
rank[rooty]++;
}
}
};
class UndirectedGraph
{
private:
std::vector<std::vector<int>> adj;
int nodes;
public:
UndirectedGraph(int n, const std::vector<Edge>& edges)
: nodes(n)
{
adj.resize(nodes);
for (const auto& iter : edges) {
adj[iter.src].push_back(iter.dest);
// Don't do the following! The logic used in union find will end up
// detecting an edge 1-2 as a cycle because, we will first encounter 1-2 and
// mark 1 as the parent for 1 and 2. Then we will encounter 2-1 and we will see that
// they have the same root 1.
//adj[iter.dest].push_back(iter.src);
}
}
bool hasCycle()
{
DisjointSet obj(nodes);
for (int i = 0; i < nodes; ++i)
{
int start = i;
for (int j : adj[i])
{
int dest = j;
int root_source = obj.find(start);
int root_dest = obj.find(dest);
if (root_source == root_dest)
return true;
// you can also pass the start and dest variables. Since you have
// already computed the roots, you can pass this info. FIND will
// first compute the roots only
obj.makeUnion(root_source, root_dest);
}
}
return false;
}
};
int main()
{
std::vector<Edge> edges{ {1, 2}, {1, 7}, {1, 8}, {2, 3}, {2, 6}, {3, 4}, {3, 5}, {8, 9}, {8, 12}, {9, 10}, {9, 11}, {11, 12} };
int n = 13;
UndirectedGraph obj(n, edges);
std::cout << "Graph has cycles: " << obj.hasCycle() << std::endl;
return 0;
}