原题链接在这里: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. Harbor私有仓库搭建

    1.安装docker yum install -y dockersystemctl start dockersystemctl enable docker 2.安装docker-compose 1.下 ...

  2. extern "C" 有关问题

    之前帮老板编译一个库的代码,遇到了一些问题,后来发现问题出现在extern "C"语法上. 1. C/C++语法extern 关键字 extern是C/C++语言中表明函数和全局变 ...

  3. Zuul

    一.zuul是什么 zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架. ...

  4. Linux Shell基础 多个命令中的分号(;)、与(&&) 、 或(||)

    概述 在 Bash 中,如果需要让多条命令按顺序执行,则有这样方法,如表 1 所示. 多命令执行符 格 式 作 用 : 命令1 ; 命令2 多条命令顺序执行,命令之间没有任何逻辑关系 &&am ...

  5. package.json字段简要解析

    name 必填 应用名称 version 必填 应用版本 description 选填 应用描述,多用于搜索,在npm search 时可以用到 keywords 选填 应用关键字,也多用于搜索 sc ...

  6. Cocos2d-x项目移植到WP8系列之八:CCLabelTTF显示中文不换行

    原文链接: http://www.cnblogs.com/zouzf/p/3985330.html 在wp8平台上,CCLabeTTF显示中文不会自动换行,看了下源码,原来底层的实现是根据text的空 ...

  7. iOS 学习之 UITabBarController

    - (IBAction)btnClick:(id)sender { UITabBarController *tabBarCtrl = [[[UITabBarController alloc] init ...

  8. gcc编译c、c++入门

    一.c语言 1.在当前目录下新建c文件 $:vim hello.c 2.按i进入编辑模式.按esc退出编辑模式,输入源代码 #include <stdio.h> int main(void ...

  9. HTML5 画布canvas

    SVG的<defs> <symbols> 元素用于预定义一个元素使其能够在SVG图像中重复使用 <svg xmlns="http://www.w3.org/20 ...

  10. Linux之Xinetd服务介绍

    一.概念:1.独立启动的守护进程:stand-alone,每个特定服务都有单独的守护进程,这个处理单一服务的始终存在的进程就是独立启动的守护进程. 2.超级守护进程:多个服务统一由一个进程管理,该进程 ...