LeetCode Number of Connected Components in an Undirected Graph

时间:2022-03-13 10:43:17

原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/

题目:

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph.

Example 1:

     0          3
| |
1 --- 2 4

Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2.

Example 2:

     0           4
| |
1 --- 2 --- 3

Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]], return 1.

Note:
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.

题解:

使用一维UnionFind.

Time Complexity: O(elogn). e是edges数目. Find, O(logn). Union, O(1).

Space: O(n).

AC Java:

 class Solution {
public int countComponents(int n, int[][] edges) {
if(n <= 0){
return 0;
} UnionFind uf = new UnionFind(n);
for(int [] edge : edges){
if(!uf.find(edge[0], edge[1])){
uf.union(edge[0], edge[1]);
}
} return uf.size();
}
} class UnionFind{
private int count;
private int [] parent;
private int [] size; public UnionFind(int n){
this.count = n;
parent = new int[n];
size = new int[n];
for(int i = 0; i<n; i++){
parent[i] = i;
size[i] = 1;
}
} public boolean find(int i, int j){
return root(i) == root(j);
} private int root(int i){
while(i != parent[i]){
parent[i] = parent[parent[i]];
i = parent[i];
} return parent[i];
} public void union(int i, int j){
int rootI = root(i);
int rootJ = root(j);
if(size[rootI] > size[rootJ]){
parent[rootJ] = rootI;
size[rootI] += size[j];
}else{
parent[rootI] = rootJ;
size[rootJ] += size[rootI];
} this.count--;
} public int size(){
return this.count;
}
}

也可以使用BFS, DFS.

Time Complexity: O(n+e), 建graph用O(n+e), BFS, DFS 用 O(n+e). Space: O(n + e).

 public class Solution {
public int countComponents(int n, int[][] edges) {
List<List<Integer>> graph = new ArrayList<List<Integer>>();
for(int i = 0; i<n; i++){
graph.add(new ArrayList<Integer>());
} for(int [] edge : edges){
graph.get(edge[0]).add(edge[1]);
graph.get(edge[1]).add(edge[0]);
} HashSet<Integer> visited = new HashSet<Integer>();
int count = 0;
for(int i = 0; i<n; i++){
if(!visited.contains(i)){
// bfs(graph, i, visited);
dfs(graph, i, visited);
count++;
}
}
return count;
} public void bfs(List<List<Integer>> graph, int i, HashSet<Integer> visited){
LinkedList<Integer> que = new LinkedList<Integer>();
visited.add(i);
que.add(i);
while(!que.isEmpty()){
int cur = que.poll();
List<Integer> neighbours = graph.get(cur);
for(int neighbour : neighbours){
if(!visited.contains(neighbour)){
que.add(neighbour);
visited.add(neighbour);
}
}
}
} public void dfs(List<List<Integer>> graph, int i, HashSet<Integer> visited){
visited.add(i);
for(int neighbour : graph.get(i)){
if(!visited.contains(neighbour)){
dfs(graph, neighbour, visited);
}
}
}
}

跟上Find the Weak Connected Component in the Directed GraphNumber of Islands II.