Find the Connected Component in the Undirected Graph
来源:互联网 发布:office for mac 下载 编辑:程序博客网 时间:2024/06/01 18:44
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 is a subgraph in which any two vertices are connected to each other by paths, and which is connected to no additional vertices in the supergraph.)
Have you met this question in a real interview? Yes
Example
Given graph:
A——B C–E
|
D
Return {A,B,D}, {C,E}. Since there are two connected component which is {A,B,D}, {C,E}
/** * Definition for Undirected graph. * class UndirectedGraphNode { * int label; * ArrayList<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */public class Solution { /** * @param nodes a array of Undirected graph node * @return a connected set of a Undirected graph */ public List<List<Integer>> connectedSet(ArrayList<UndirectedGraphNode> nodes) { // Write your code here if(nodes==null || nodes.size()==0)return null; List<List<Integer>> result = new ArrayList<List<Integer>>(); Set<UndirectedGraphNode> visited = new HashSet<UndirectedGraphNode>(); for(UndirectedGraphNode node:nodes){ if(visited.contains(node)) continue; List<Integer> row =bfs(node,visited); result.add(row); } return result; } private List<Integer> bfs(UndirectedGraphNode node, Set<UndirectedGraphNode> visited){ List<Integer>pow = new ArrayList<>(); Queue<UndirectedGraphNode> q= new LinkedList<UndirectedGraphNode>(); q.offer(node); visited.add(node); while(!q.isEmpty()){ UndirectedGraphNode qnode =q.poll(); pow.add(qnode.label); for(UndirectedGraphNode neighbor:qnode.neighbors){ if(visited.contains(neighbor))continue; q.offer(neighbor); visited.add(neighbor); } } Collections.sort(pow); return pow; }}
0 0
- Find the Connected Component in the Undirected Graph
- 【LintCode】在无向图中找出各极大连通子图 Find the Connected Component in the Undirected Graph
- Connected Component in Undirected Graph
- leetcode03. Find the Weak Connected Component in the Directed Graph
- LintCode 431 Connected Component in Undirected Graph
- Sicily 1002. connected components in undirected graph
- [leetcode323]Number of Connected Components in an Undirected Graph
- LeetCode 323. 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 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
- No323. Number of Connected Components in an Undirected Graph
- 323. Number of Connected Components in an Undirected Graph
- leetcode323. Number of Connected Components in an Undirected Graph
- [leetcode] 323. Number of Connected Components in an Undirected Graph 解题报告
- 夺命雷公狗---js_mv思路
- 关于HTTP
- Spark之Scala语言常见应用举例
- 11_排序二叉树删除-3
- Java - 位运算
- Find the Connected Component in the Undirected Graph
- 关于建立我自己的网站
- jfinal学习1
- BHO开发浏览器插件-Adding Explorer Bars
- Android项目打包、Eclipse视图和UI控件
- 访问者(visitor)模式
- bzoj1191[HNOI2006]超级英雄Hero
- Android之AlertDialog自定义对话框
- 在 ubuntu中 安装谷歌