LeetCode-Number of Connected Components in an Undirected Graph
2016-08-12 13:45
465 查看
Given
Example 1:
Given
Example 2:
Given
Note:
You can assume that no duplicate edges will appear in
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.
With each edge added, check which island is
If
Otherwise, union two islands, and reduce islands count by
Bonus: path compression can reduce time by
Hope it helps!
Solution:
nnodes labeled from
0to
n - 1and 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 = 5and
edges = [[0, 1], [1, 2], [3, 4]], return
2.
Example 2:
0 4 | | 1 --- 2 --- 3
Given
n = 5and
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.
npoints =
nislands =
ntrees =
nroots.
With each edge added, check which island is
e[0]or
e[1]belonging to.
If
e[0]and
e[1]are in same islands, do nothing.
Otherwise, union two islands, and reduce islands count by
1.
Bonus: path compression can reduce time by
50%.
Hope it helps!
Solution:
public class Solution { public int countComponents(int n, int[][] edges) { int[] roots = new int ; for (int i=0;i<n;i++) roots[i] = i; int num = n; int len = edges.length; for (int i=0;i<len;i++){ int root1 = findRoot(roots,edges[i][0]); int root2 = findRoot(roots,edges[i][1]); if (root1!=root2){ roots[root1] = root2; num--; } } return num; } public int findRoot(int[] roots, int node){ while (roots[node]!=node){ roots[node] = roots[roots[node]]; node = roots[node]; } return node; } }
相关文章推荐
- LeetCode Number of Connected Components in an Undirected Graph
- leetcode Number of Connected Components in an Undirected Graph
- Number of Connected Components in an Undirected Graph -- LeetCode
- Leetcode: Number of Connected Components in an Undirected Graph
- [leetcode323]Number of Connected Components in an Undirected Graph
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
- Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
- [Locked] Number of Connected Components in an Undirected Graph
- Number of Connected Components in an Undirected Graph
- Number of Connected Components in an Undirected Graph
- LeetCode 323. Number of Connected Components in an Undirected Graph
- LeetCode 323. Number of Connected Components in an Undirected Graph(连通分量)
- Leetcode 323. Number of Connected Components in an Undirected Graph
- Leetcode 323. Number of Connected Components in an Undirected Graph
- Number of Connected Component in An Undirected Graph
- LeetCode 323. Number of Connected Components in an Undirected Graph
- No323. Number of Connected Components in an Undirected Graph
- 323. Number of Connected Components in an Undirected Graph
- sicily 4378 connected components in undirected graph
- WCF:Maximum number of items that can be serialized or deserialized in an object graph is '65536'.