[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), ...
随机推荐
- JSP九大内置对象辨析
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6043096.html JSP中一共预先定义了9个这样的对象,分别为:request.response.sess ...
- Oracle Data Integrator 12c----一致性 CDC(Consistent CDC)
一致性 CDC 中引入了变化集的概念.一个变化集中可以包括多个相互存在关联关系(如主外键引用关系)的表.CDC 在捕获和发布一个变化集中的变化时能够保证数据的一致性.这个练习介绍如何使用能够保证一致性 ...
- 〖Linux〗在tmux同时使用bash和zsh
个人有两份tmux配置文件: ~/.tmux.conf # 使用zsh,主要是日常使用,zsh太好使用了 ~/.tmux.conf.bash # 使用bash,主要是Android编译使用 按照tmu ...
- phonegap(cordova) 自己定义插件代码篇(五)----android ,iOS 集成微信登陆
统一登陆还是非常有必要的,安全,放心.代码 /*cordov 微信自己定义插件*/ (function (cordova) { var define = cordova.define; define( ...
- C++高性能转换大小写算法
简述 有一个需求,是需要将URL中的query参数的key全部转换为小写或者大写,键值对的数量有点多,但全部都是英文字母,无需考虑非字母的情况. 实现比较快的做法是使用STL或C标准库中的转换接口,如 ...
- Spring Boot 使用Jar打包发布, 并使用 Embedded Jetty/Tomcat 容器
Jar包发布 在项目pom.xml中, 如果继承了Spring Boot的starter parent, 那么默认已经包含打包需要的plugins了, 设置为jar就能直接打包成包含依赖的可执行的ja ...
- Mac OS使用技巧十九:Safari碉堡功能之二查看网页源代码
由于大三下的时候选修了搜索技术.了解了网络上搜索引擎和网络爬虫的信息扒取的一些东西,后来我们做了一个比較水的东西.就是仅仅扒取了几家较大的下载站点几十个软件的评分下载量等信息,当用户输入一个 ...
- A. Kyoya and Photobooks(Codeforces Round #309 (Div. 2))
A. Kyoya and Photobooks Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He ...
- 【linux】linux 环境下 安装禅道(转载) -- 跟web服务器无关,无视apache、nginx!!!
下载地址:http://www.zentao.net/download/zentao10.0.beta-80076.html 参考文章 链接 :https://blog.csdn.net/xinxin ...
- Easyui入门视频教程 第09集---登录完善 图标自定义
目录 ----------------------- Easyui入门视频教程 第09集---登录完善 图标自定义 Easyui入门视频教程 第08集---登录实现 ajax button的使用 ...