forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0323-number-of-connected-components-in-an-undirected-graph.cpp
57 lines (50 loc) · 1.46 KB
/
0323-number-of-connected-components-in-an-undirected-graph.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
/*
Graph of n nodes, given edges array, return # of connected components
Ex. n = 5, edges = [[0,1],[1,2],[3,4]] -> 2
Union find, for each edge combine, if already in same set keep traversing
If not in same set, decrement count by 1, count will store # of components
Time: O(n)
Space: O(n)
*/
class Solution {
public:
int countComponents(int n, vector<vector<int>>& edges) {
vector<int> parents;
vector<int> ranks;
for (int i = 0; i < n; i++) {
parents.push_back(i);
ranks.push_back(1);
}
int result = n;
for (int i = 0; i < edges.size(); i++) {
int n1 = edges[i][0];
int n2 = edges[i][1];
result -= doUnion(parents, ranks, n1, n2);
}
return result;
}
private:
int doFind(vector<int>& parents, int n) {
int p = parents[n];
while (p != parents[p]) {
parents[p] = parents[parents[p]];
p = parents[p];
}
return p;
}
int doUnion(vector<int>& parents, vector<int>& ranks, int n1, int n2) {
int p1 = doFind(parents, n1);
int p2 = doFind(parents, n2);
if (p1 == p2) {
return 0;
}
if (ranks[p1] > ranks[p2]) {
parents[p2] = p1;
ranks[p1] += ranks[p2];
} else {
parents[p1] = p2;
ranks[p2] += ranks[p1];
}
return 1;
}
};