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. Java的静态成员类

    Java的静态成员类: 1.静态成员类是最简单的嵌套类.最好把它看成一个普通的类,只是碰巧被声明在另一个类的内部而已,它可以访问外围类的所以成员,包括那些声明为私有的成员. 2.静态成员类是外围类的一 ...

  2. Android tree应用框架

    简单介绍 一个好的Android应用开发框架,能够加快Android开发速度,今天笔记基于很多开源项目自写了一款Android应用框架. 内容 框架包含:界面管理(Activity管理).数据库操作( ...

  3. oracle 如何查看oracle数据库版本

      select * from v$version 写在最后 哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!! 相关推荐: 个人主页 oracle专题  

  4. CSS知识点集锦

      CSS知识点集锦 CreateTime--2016年9月29日09:43:10Author:Marydon UpdateTime--2017年3月21日08:03:13 2.CSS样式优先级问题 ...

  5. Android开发之使用HttpURLConnection进行POST请求

    一.前提准备 在开始实际编码之前,我们有必要先了解下将会用的类以及方法,进行一个大体的了解. 1.URL类 这个类主要的功能是定位到要获取资源的网址以及打开连接.比如下面的代码: URL realur ...

  6. ADAMS绳索仿真

    在实际生活中有大量的绳索类传动运动形式.绳索类部件看似简单,但是用计算机软件对其模拟时却存在较大障碍.以钢丝绳为例,通常都是通过细长的钢丝螺旋缠绕在一起形成,在工作时当施加拉力载荷于其上,除了材料自身 ...

  7. CPLSetErrorHandlerEx函数Bug

    CPLSetErrorHandlerEx(gdal/gdal/port/cpl_error.cpp,当前github中代码)当前函数实现如下 CPLErrorHandler CPL_STDCALL C ...

  8. 高密度WIFI部署要点

    1. 划分AP组,分组带宽控制 根据区域的人数密集程度划分不同的AP组,并进行优化策略调整,分组分权限进行带宽控制,以确保单用户的2.4G带宽不低于1M,5G用户不低于2M2. 相邻AP错开信道 超高 ...

  9. Debug 路漫漫-01

    运行到子函数时提示报错:  === 这个断点一步步debug下来是顺利的,但是咋就超出数组范围了呢,这会是什么问题. ——sess肯定超过索引了,那个sess(:,2)的值肯定超过V的行数了. ——由 ...

  10. 模拟器集成3DTouch-b

    本文主要讲解3DTouch各种场景下的集成,开发主屏幕应用icon上的快捷选项标签(Home Screen Quick Actions),静态设置 UIApplicationShortcutItem ...