【LintCode】在无向图中找出各极大连通子图 Find the Connected Component in the Undirected Graph
来源:互联网 发布:面膜推荐知乎 编辑:程序博客网 时间:2024/06/05 14:26
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.)
(lintcode上这道题的翻译有点问题。)
Example
Given graph:
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) { List<List<Integer>> resultSet = new LinkedList<List<Integer>>(); Set<Integer> visited = new HashSet<Integer>(); for(int i = 0; i < nodes.size(); i++) { if(!visited.contains(nodes.get(i).label)) {//若该节点未被访问过则加入队列,开始访问其邻居节点 List<Integer> list = new ArrayList<Integer>(); Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>(); queue.add(nodes.get(i)); list.add(nodes.get(i).label); visited.add(nodes.get(i).label); while(!queue.isEmpty()) {//将该节点未访问过的邻居节点加入队列 UndirectedGraphNode node = queue.poll(); for(int j = 0; j < node.neighbors.size(); j++) { UndirectedGraphNode nodeNei = node.neighbors.get(j); if(!visited.contains(nodeNei.label)) { queue.add(nodeNei); list.add(nodeNei.label); visited.add(nodeNei.label); } } } Collections.sort(list);//排序 resultSet.add(list); } } return resultSet; }}
0 0
- 【LintCode】在无向图中找出各极大连通子图 Find the Connected Component in the Undirected Graph
- Find the Connected Component in the Undirected Graph
- Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
- LintCode 431 Connected Component in Undirected Graph
- Connected Component in Undirected Graph
- leetcode03. Find the Weak Connected Component in the Directed Graph
- Sicily 1002. connected components in undirected graph
- LeetCode 323. Number of Connected Components in an Undirected Graph(连通分量)
- 求无向连通图的点双连通分支(不包含割点的极大连通子图)
- 无向图1(UNDIRECTED GRAPHS)
- [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
- 华为OJ(将真分数分解为埃及分数)
- jQuery基础----19jQuery遍历之同级遍历
- SpringMVC与mysql进行登录验证
- 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)..(注意,字母出现的次数,要过滤掉数字)
- Java开发技巧记录
- 【LintCode】在无向图中找出各极大连通子图 Find the Connected Component in the Undirected Graph
- jQuery基础----20jQuery遍历之过滤
- UVA 11059 Maximum Product (暴力)
- maven 入门
- CodeForces 545D贪心
- Oracle Cursor分类与实例(清晰易懂)
- LightOJ 1008
- 第85讲:Scala中For表达式的强大表现力实战
- mac os x 终端 采用 Solarized 配色方案