## Approach 1: BFS

• Time:O(|V| + |E|)
• Space:O(|V| + |E|)

## C++

``````class Solution {
public:
int countComponents(int n, vector<vector<int>>& edges) {
int ans = 0;
vector<vector<int>> graph(n);
unordered_set<int> seen;

for (const auto& e : edges) {
const int u = e[0];
const int v = e[1];
graph[u].push_back(v);
graph[v].push_back(u);
}

for (int i = 0; i < n; ++i)
if (!seen.count(i)) {
bfs(graph, i, seen);
++ans;
}

return ans;
}

private:
void bfs(const vector<vector<int>>& graph, int node,
unordered_set<int>& seen) {
queue<int> q{{node}};
seen.insert(node);

while (!q.empty()) {
const int u = q.front();
q.pop();
for (const int v : graph[u])
if (!seen.count(v)) {
q.push(v);
seen.insert(v);
}
}
}
};
``````

## JAVA

``````class Solution {
public int countComponents(int n, int[][] edges) {
int ans = 0;
List<Integer>[] graph = new List[n];
Set<Integer> seen = new HashSet<>();

for (int i = 0; i < n; ++i)
graph[i] = new ArrayList<>();

for (int[] e : edges) {
final int u = e[0];
final int v = e[1];
}

for (int i = 0; i < n; ++i)
if (!seen.contains(i)) {
bfs(graph, i, seen);
++ans;
}

return ans;
}

private void bfs(List<Integer>[] graph, int node, Set<Integer> seen) {
Queue<Integer> q = new ArrayDeque<>(Arrays.asList(node));

while (!q.isEmpty()) {
final int u = q.poll();
for (final int v : graph[u])
if (!seen.contains(v)) {
q.offer(v);
}
}
}
}
``````

## Python

``````class Solution:
def countComponents(self, n: int, edges: List[List[int]]) -> int:
ans = 0
graph = [[] for _ in range(n)]
seen = set()

for u, v in edges:
graph[u].append(v)
graph[v].append(u)

def bfs(node: int, seen: Set[int]) -> None:
q = deque([node])

while q:
u = q.pop()
for v in graph[u]:
if v not in seen:
q.append(v)

for i in range(n):
if i not in seen:
bfs(i, seen)
ans += 1

return ans
``````

## Approach 2: DFS

• Time:O(|V| + |E|)
• Space:O(|V| + |E|)

## C++

``````class Solution {
public:
int countComponents(int n, vector<vector<int>>& edges) {
int ans = 0;
vector<vector<int>> graph(n);
unordered_set<int> seen;

for (const auto& e : edges) {
const int u = e[0];
const int v = e[1];
graph[u].push_back(v);
graph[v].push_back(u);
}

for (int i = 0; i < n; ++i)
if (seen.insert(i).second) {
dfs(graph, i, seen);
++ans;
}

return ans;
}

private:
void dfs(const vector<vector<int>>& graph, int u, unordered_set<int>& seen) {
for (const int v : graph[u])
if (seen.insert(v).second)
dfs(graph, v, seen);
}
};
``````

## JAVA

``````class Solution {
public int countComponents(int n, int[][] edges) {
int ans = 0;
List<Integer>[] graph = new List[n];
Set<Integer> seen = new HashSet<>();

for (int i = 0; i < n; ++i)
graph[i] = new ArrayList<>();

for (int[] e : edges) {
final int u = e[0];
final int v = e[1];
}

for (int i = 0; i < n; ++i)
dfs(graph, i, seen);
++ans;
}

return ans;
}

private void dfs(List<Integer>[] graph, int u, Set<Integer> seen) {
for (final int v : graph[u])
dfs(graph, v, seen);
}
}
``````

## Python

``````class Solution:
def countComponents(self, n: int, edges: List[List[int]]) -> int:
ans = 0
graph = [[] for _ in range(n)]
seen = set()

for u, v in edges:
graph[u].append(v)
graph[v].append(u)

def dfs(u: int, seen: Set[int]) -> None:
for v in graph[u]:
if v not in seen:
dfs(v, seen)

for i in range(n):
if i not in seen:
dfs(graph, i, seen)
ans += 1

return ans
``````

## Approach 3: UF

• Time:O(|V| + |E|)
• Space:O(|V| + |E|)

## C++

``````class UF {
public:
UF(int n) : count(n), id(n) {
iota(begin(id), end(id), 0);
}

void union_(int u, int v) {
const int i = find(u);
const int j = find(v);
if (i == j)
return;
id[i] = j;
--count;
}

int getCount() const {
return count;
}

private:
int count;
vector<int> id;

int find(int u) {
return id[u] == u ? u : id[u] = find(id[u]);
}
};

class Solution {
public:
int countComponents(int n, vector<vector<int>>& edges) {
UF uf(n);

for (const auto& e : edges)
uf.union_(e[0], e[1]);

return uf.getCount();
}
};
``````

## JAVA

``````class UF {
public UF(int n) {
count = n;
id = new int[n];
for (int i = 0; i < n; ++i)
id[i] = i;
}

public void union(int u, int v) {
final int i = find(u);
final int j = find(v);
if (i == j)
return;
id[i] = j;
--count;
}

public int getCount() {
return count;
}

private int count;
private int[] id;

private int find(int u) {
return id[u] == u ? u : (id[u] = find(id[u]));
}
}

class Solution {
public int countComponents(int n, int[][] edges) {
UF uf = new UF(n);

for (int[] e : edges)
uf.union(e[0], e[1]);

return uf.getCount();
}
}
``````

## Python

``````class UF:
def __init__(self, n: int):
self.count = n
self.id = list(range(n))

def union(self, u: int, v: int) -> None:
i = self.find(u)
j = self.find(v)
if i == j:
return
self.id[i] = j
self.count -= 1

def find(self, u: int) -> int:
if self.id[u] != u:
self.id[u] = self.find(self.id[u])
return self.id[u]

class Solution:
def countComponents(self, n: int, edges: List[List[int]]) -> int:
uf = UF(n)

for u, v in edges:
uf.union(u, v)

return uf.count
``````