[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), ...
随机推荐
- MyArrayList——自己实现ArrayList
注:转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5965205.html 代码已移植:https://github.com/ygj0930/MyAr ...
- N-Queens I II(n皇后问题)(转)
N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...
- 虚拟机安装VBoxAdditions增强功能
在VirtualBox上安装好CentOS后,需将VBoxAdditions增强功能安装上,该功能有如下作用: (1)实现客户机和主机间的鼠标平滑移动 (2)与主机实现文件共享 (3)安装虚拟显卡驱动 ...
- java sm3加密算法
java sm3加密算法实现 CreationTime--2018年7月13日09点28分 Author:Marydon 1.准备工作 所需jar包: bcprov-jdk15on-1.59.ja ...
- java 八种基本数据类型之与对应的封装类之间的相互转化
迁移时间--2017年5月26日17:47:37 Author:Marydon 一.java数据类型之基本数据类型 UpdateTime--2017年1月9日17:31:14 (三)格式转换 1. ...
- django之创建第7-6-第三种传值方式
1.创建bar.html文件 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- python3 发送邮件功能
阿-_-涵的博客 #首先写一个模块功能,发邮件功能打包起来 from smtplib import SMTP from email.mime.text import MIMEText def send ...
- 推断Windows版本号新方法
曾经在代码中推断用户的Windows操作系统版本号都是通过GetVersion或GetVersionEx获取版本号号,然后比較. 今天偶然发如今新的Visual Studio提供了新的函数来推断系统版 ...
- 不让复制是不可能的----js获取选中文字
在360百科.知乎上经常会遇见禁止复制文本的情形,这能挡住一部分人复制,却挡不住程序员的复制. HTML都给我了,难道一小段文本我都拿不下来吗? F12打开控制台,然后选中文本,在控制台下粘贴以下代码 ...
- GO语言中的几个关键思想
GO语言的设计理念与C++,Java,Python之流大相径庭. 一.没有函数重载 GO语言里面没有函数重载,Java.C#.C++三位大牛都是支持函数重载的,Python虽然不支持函数重载,但是支持 ...