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), 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.

Analysis:

From here: https://discuss.leetcode.com/topic/32752/easiest-2ms-java-solution

This is 1D version of Number of Islands II. For more explanations, check out this 2D Solution.

  1. n points = n islands = n trees = n roots.
  2. With each edge added, check which island is e[0] or e[1] belonging to.
  3. If e[0] and e[1] are in same islands, do nothing.
  4. Otherwise, union two islands, and reduce islands count by 1.
  5. Bonus: path compression can reduce time by 50%.

Hope it helps!

Solution:

 1 public class Solution {
 2     public int countComponents(int n, int[][] edges) {
 3         int[] roots = new int[n];
 4         for (int i=0;i<n;i++) roots[i] = i;
 5         int num = n;
 6
 7         int len = edges.length;
 8         for (int i=0;i<len;i++){
 9             int root1 = findRoot(roots,edges[i][0]);
10             int root2 = findRoot(roots,edges[i][1]);
11
12             if (root1!=root2){
13                 roots[root1] = root2;
14                 num--;
15             }
16         }
17
18         return num;
19     }
20
21     public int findRoot(int[] roots, int node){
22         while (roots[node]!=node){
23             roots[node] = roots[roots[node]];
24             node = roots[node];
25         }
26         return node;
27     }
28 }
时间: 2024-07-30 18:58:27

LeetCode-Number of Connected Components in an Undirected Graph的相关文章

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), 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,

[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), 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

Number of Connected Components in an Undirected Graph

1 public class Solution { 2 private int[] parent; 3 public int countComponents(int n, int[][] edges) { 4 if (edges.length == 0) { 5 return n; 6 } 7 parent = new int[n]; 8 for (int i = 0; i < n; i++) { 9 parent[i] = i; 10 } 11 12 for (int i = 0; i <

[LintCode] Find the Connected Component in the Undirected Graph

Find the Connected Component in the Undirected Graph Find the number connected component in the undirected graph. Each node in the graph contains a label and a list of its neighbors. (a connected component (or just component) of an undirected graph i

lintcode 容易题:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素

题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与超级图中的其它顶点相连.) 样例 给定图: A------B C \ | | \ | | \ | | \ | | D E 返回 {A,B,D}, {C,E}.其中有 2 个相连的元素,即{A,B,D}, {C,E} 解题: 广度优先+递归,写不出来,程序来源 Java程序: /** * Defini

uva live 7638 Number of Connected Components (并查集)

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5660 题意: 每两个点如果他们的gcd大于1的话就可以连一条边,问在这些数里面有多少个联通块. 题解: 我们可以用筛法倍数.然后用并查集将他们连通起来,2 3 6 本来2 和3的gcd 为1,但是他们可以通过6使得连通. 还有就是要注意 15 25 35 这个数据.

#Leetcode# 817. Linked List Components

https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked list containing unique integer values. We are also given the list G, a subset of the values in the linked list. Return the number of connected componen

Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components? 图论

E. Connected Components? You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x,?y) such that there is no edge between x and y, and if some pair

Connected Component in Undirected Graph

Description Find connected component in undirected graph. Each node in the graph contains a label and a list of its neighbors. (A connected component of an undirected graph is a subgraph in which any two vertices are connected to each other by paths,