LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里: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 Graph, Number of Islands II.
LeetCode Number of Connected Components in an Undirected Graph的更多相关文章
- [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), ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- [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), ...
- 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), ...
- 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), ...
- 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...
- [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), ...
- [Locked] Number of Connected Components in an Undirected Graph
Number of Connected Components in an Undirected Graph Given n nodes labeled from 0 to n - 1 and a li ...
- 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 ...
随机推荐
- BZOJ2758 : [SCOI2012]Blinker的噩梦
首先将包含关系建树. 方法是将每个图形拆成上半边和下半边,从左往右扫描线,用Splay从下到上维护扫描线上所有图形. 每次加入一个新的图形$x$的时候,看看它下方第一个图形$y$,如果$y$是上半边, ...
- CSS:z-index层级在IE中无效
引用地址:http://apps.hi.baidu.com/share/detail/19853262 在CSS中,只能通过代码改变层级,这个属性就是z-index,要让z-index起作用有个小小前 ...
- BZOJ3293: [Cqoi2011]分金币
Description 圆桌上坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值. Inpu ...
- SDL实战,小游戏
http://www.cppblog.com/sandy/archive/2005/12/28/2219.html sdl1教学 http://kelvmiao.info/sdl-tutorial-c ...
- Bootstrap_让Bootstrap轮播插件carousel支持左右滑动手势的三种方法
Bootstrap 的 carousel.js 插件并没有支持手势. 3种解决方案 : jQuery Mobile (http://jquerymobile.com/download/) $(&quo ...
- osg实例介绍
osg实例介绍 转自:http://blog.csdn.net/yungis/article/list/1 [原]osgmotionblur例子 该例子演示了运动模糊的效果.一下内容是转自网上的:原理 ...
- JS文档生成工具:JSDoc 介绍
JSDoc是一个根据javascript文件中注释的信息,生成API文档的工具.生成的文档是html文件.类似JavaDoc和PHPDoc. 用法 /** 一坨注释之类的 */JSDoc会从/**开头 ...
- Advanced SQL
Top number of records SELECT column_name FROM table_name LIMIT 5; Like/Not Like SELECT * FROM Custom ...
- js判断微信浏览器
function is_weixin(){ //检查是否是微信浏览器 var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMes ...
- [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数
LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...