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.)

 
Example

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的更多相关文章

  1. lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素

    题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...

  2. [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 ...

  3. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  4. LeetCode 323. Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  5. 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), ...

  6. 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 ...

  7. [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), ...

  8. [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 ...

  9. [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), ...

随机推荐

  1. Webwork【08】结合实战简析Controller 配置

    虽然现在 MVC 框架层出不穷,但做为 Struts 前身的 webwork. 其经典程度不亚于贝利之于足球,双 11 之于淘宝特卖. 本篇将结合 webwork controller 配置文件 xw ...

  2. 使用c语言调用python小结

    近期在做一个漏洞展示平台,攻击实现部分使用python实现.c语言实现部分使用libcli库做一个类似telnet的东东,回调函数run的时候调用python模块. 针对c调用python,做个了小d ...

  3. 〖Linux〗安装和使用virtualenv,方便多个Python版本中切换

    1. 安装pip easy_install pip 2. 安装virtualenvwrapper sudo pip install virtualenvwrapper 3. 使用virtualenv ...

  4. Axure 图片轮播(广告通栏图片自动播放效果)

    baiduYunpan:http://pan.baidu.com/s/1eRPCy90 里面的“图片轮播”部件即可实现这个功能

  5. 新安装的ubuntu编辑器问题

    转自:https://blog.csdn.net/xiangaichou/article/details/20235041 VI部分 1. 上下左右总是出ABCD,还占行,特难用.这种情况出现在ubu ...

  6. 使用Thrift让Python为Java提供服务

    Thrift是基于TCP的,谷歌的GRPC是基于HTTP的.Thrift和GRPC都是比直接写个web接口进行调用更完美的方式,最明显的一点就是:我们可以定义结构体,避免了手动解析的过程. 但是,在将 ...

  7. 快排法求第k大

    快排法求第k大,复杂度为O(n) import com.sun.media.sound.SoftTuning; import java.util.Arrays; import java.util.Ra ...

  8. Java6 WebService的发布(转)

      Java6 WebService的发布   转:http://lavasoft.blog.51cto.com/62575/227988/   WebService服务发布往往比较混乱,Axis2的 ...

  9. OC中instancetype与id的区别

    1.在ARC环境下: instancetype用来在编译期确定实例的类型,而使用id的话,编译器不检查类型, 运行时检查类型. 2.在MRC环境下: instancetype和id一样,不做具体类型检 ...

  10. 【DeepLearning】Exercise:PCA in 2D

    Exercise:PCA in 2D 习题的链接:Exercise:PCA in 2D pca_2d.m close all %%=================================== ...