原题链接在这里: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.

LeetCode 323. Number of Connected Components in an Undirected Graph的更多相关文章

  1. [LeetCode] 323. 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), ...

  2. 323. Number of Connected Components in an Undirected Graph (leetcode)

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  3. 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...

  4. 323. 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 n ...

  5. 323. Number of Connected Components in an Undirected Graph

    算连接的..那就是union find了 public class Solution { public int countComponents(int n, int[][] edges) { if(e ...

  6. [LeetCode] 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), ...

  7. LeetCode Number of Connected Components in an Undirected Graph

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

  8. Number of Connected Components in an Undirected Graph -- LeetCode

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  9. [Swift]LeetCode323. 无向图中的连通区域的个数 $ 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), ...

随机推荐

  1. iOS Autolayout 在tableView scrollView 适用 学习

    1  如何自动适应cell的高度 autolayout  里面 使用 systemLayoutSizeFittingSize 方法 (系统通过 已知的完整的Constraints和view的属性来计算 ...

  2. IMP导入小记

    1.创建表空间 create tablespace example_tablespace datafile 'e:\****.dbf' size 10m reuse autoextend on nex ...

  3. MySQL-重做日志 redo log -原理

    [redo log buffer][redo log file]-原理 目录: 1.重做日志写入过程图 2.相关知识点汇总图 3.redo_log_buffer 原理 4.redo_log_file ...

  4. 基于Visual c++ 2012的php扩展开发 - HelloWord!

    1.cmd进入命令行模式,并进入php-5.6.20-src/ext源代码的ext目录下输入命令php ext_skel_win32.php --extname=HelloWord,执行结果如下图: ...

  5. JAVA基础补漏--基础数据类型与引用数据类型

    ==在不同数据类型中意义不同. 在基础数据类型中,表示的是数值的比较. 在引用数据类型中,表示的是内存地址值的比较. 一.基本数据类型: byte:Java中最小的数据类型,在内存中占8位(bit), ...

  6. Linux VPS实用简单安全配置

    今天,和大家一起来分享VPS最基本的安全配置. 第一.修改SSH端口 VPS默认的SSH端口是22,那些扫描穷举密码的,也势必从22开始,所以,修改22为一个其他的数字,是非常有必要的. 好了,SSH ...

  7. linux下firefox显示中文乱码的问题

    只需要yum install "@Chinese Support" 然后注销,再登录一下,刷新浏览器就可以正常显示中文了,当然前提是浏览器的字符编码为utf-8以及默认显示中文,这 ...

  8. tomcat 日志禁用

    1.禁用catalina.out日志通过修改catalina.sh配置可以控制tomcat不生成该文件只要将if [ -z "$CATALINA_OUT" ] ; then CAT ...

  9. 【P2361】yyy棋(博弈论+贪心+模拟)

    这个题看上去本来不好处理,然而善意的题面已经基本告诉你做法了,小时候玩的那个游戏就是代码的核心.动动脑子想想,如果长和宽的积是奇数,那么一定要先手,如果是偶数,那么后手就会获胜. 好了,那么怎么处理对 ...

  10. Apache Phoenix基本操作-1

    本篇我们将介绍phoenix的一些基本操作. 1. 如何使用Phoenix输出Hello World? 1.1 使用sqlline终端命令 sqlline.py SZB-L0023780:2181:/ ...