[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 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.)
Given graph:
A------B C
\ | |
\ | |
\ | |
\ | |
D E
Return {A,B,D}, {C,E}
. Since there are two connected component which is {A,B,D}, {C,E}
DFS:
/**
* Definition for Undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
/**
* @param nodes a array of Undirected graph node
* @return a connected set of a Undirected graph
*/
void dfs(vector<UndirectedGraphNode*> &nodes, vector<int> &path,
unordered_set<UndirectedGraphNode*> &visit, UndirectedGraphNode* n) {
visit.insert(n);
path.push_back(n->label);
for (auto &nn : n->neighbors) if (visit.find(nn) == visit.end()) {
dfs(nodes, path, visit, nn);
}
}
vector<vector<int>> connectedSet(vector<UndirectedGraphNode*>& nodes) {
// Write your code here
unordered_set<UndirectedGraphNode*> visit;
vector<vector<int>> res;
vector<int> path;
for (auto &n : nodes) {
if (visit.find(n) == visit.end()) {
path.clear();
dfs(nodes, path, visit, n);
sort(path.begin(), path.end());
res.push_back(path);
}
}
return res;
}
};
BFS:
/**
* Definition for Undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
/**
* @param nodes a array of Undirected graph node
* @return a connected set of a Undirected graph
*/
vector<vector<int>> connectedSet(vector<UndirectedGraphNode*>& nodes) {
// Write your code here
unordered_set<UndirectedGraphNode*> visit;
vector<vector<int>> res;
vector<int> path;
queue<UndirectedGraphNode*> que;
for (auto &n : nodes) {
if (visit.find(n) == visit.end()) {
path.clear();
visit.insert(n);
for (que.push(n); !que.empty(); que.pop()) {
auto u = que.front();
path.push_back(u->label);
for (auto nn : u->neighbors) if (visit.find(nn) == visit.end()) {
visit.insert(nn);
que.push(nn);
}
}
sort(path.begin(), path.end());
res.push_back(path);
}
}
return res;
}
};
[LintCode] Find the Connected Component in the Undirected Graph的更多相关文章
- lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素
题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...
- [LintCode] Find the Weak Connected Component in the Directed Graph
Find the number Weak Connected Component in the directed graph. Each node in the graph contains a ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- 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), ...
- Find the Weak Connected Component in the Directed Graph
Description Find the number Weak Connected Component in the directed graph. Each node in the graph c ...
- [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), ...
- [Locked] Number of Connected Components in an Undirected Graph
Number of Connected Components in an Undirected Graph Given n nodes labeled from 0 to n - 1 and a li ...
- [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), ...
随机推荐
- 解决 weblogic poi3.9 报错 a different type with name "javax/xml/namespace/QName"
解决 java.lang.LinkageError: loader constraint violation: loader (instance of weblogic/utils/classload ...
- ES6学习笔记八:类与继承
一:Class ES6 提供了更接近传统语言的写法,引入了 Class(类)这个概念,作为对象的模板.通过class关键字,可以定义类. 定义“类”的方法的时候,前面不需要加上function这个关键 ...
- 〖Linux〗Linux的smb地址转换Windows格式(两者互转)
因为个人常用办公PC是Linux,打开文件共享什么的是 smb:// 的,而不是Windows的 \\ 需要复制文件路径给别人的时候,发smb://给一个使用Windows办公的用户不算很得体的方法 ...
- ROS中利用V-rep进行地图构建仿真
V-rep中显示激光扫描点 在VREP自带的场景中找到practicalPathPlanningDemo.ttt文件,删除场景中多余的物体只保留静态的地图.然后在Model browser→comp ...
- shell笔记-算术运算
算术运算在Bash shell环境中,可以利用let.(( ))和[]执行基本的算术操作.而在进行高级操作时,expr和bc这两个工具也会非常有用.let命令可以直接执行基本的算术操作.当使用let时 ...
- 有限状态机(FSM)的Java 学习FSM
本文从简单的例子入手,逐步演变成非常复杂的程序. 在简明 状态模式(5.8)中,状态之间的变换由外界控制,或者说,多种状态是分割的.无关的.状态模式最有趣的地方正是讨论其状态的变迁. 1.引子 空调( ...
- 小米路由Mini刷Breed, 潘多拉和LEDE
1. 下载breed,地址 http://breed.hackpascal.net/ 2. 下载小米Mini的开发板rom, 地址 http://www1.miwifi.com/miwifi_down ...
- 基于RESTful API 怎么设计用户权限控制?
前言 有人说,每个人都是平等的:也有人说,人生来就是不平等的:在人类社会中,并没有绝对的公平,一件事,并不是所有人都能去做:一样物,并不是所有人都能够拥有.每个人都有自己的角色,每种角色都有对某种资源 ...
- fedora开机出现There is a problem with the configuration server. (/usr/libexec/gconf-sanity-check-2 exited with status 256)
Install problem!The configuration defaults for GNOME Power Manager have not been installed correctly ...
- 一行Python
只要理解了函数式编程,使用神奇的Lambda, 配合列表推导以及复杂一点的判断语句,任何的python 代码都可以转换成一行代码 1.打印九九乘法表 # 打印九九乘法表 print('\n'.join ...