[LintCode][DFS] Find the Connected Component in the Undirected Graph
Problem
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.)
Clarification
Learn more about representation of graphsExample
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}
Solution
DFS找出聯通塊
/**
* Definition for Undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
private:
set<int> visited;
public:
/**
* @param nodes a array of Undirected graph node
* @return a connected set of a Undirected graph
*/
vector<vector<int>> connectedSet(vector<UndirectedGraphNode*>& nodes) {
vector<vector<int>> ret;
for(int i = 0; i < nodes.size(); i++) {
if (visited.count(nodes[i]->label) == 0) {
vector<int> res;
visited.insert(nodes[i]->label);
dfs(nodes[i], res);
sort(res.begin(), res.end());
ret.push_back(res);
}
}
return ret;
}
void dfs(UndirectedGraphNode *node, vector<int> &res) {
res.push_back(node->label);
for(int i = 0; i < node->neighbors.size(); i++) {
int label = node->neighbors[i]->label;
if (visited.count(label) == 0) {
visited.insert(label);
dfs(node->neighbors[i], res);
}
}
}
};
相關文章
- LeetCode-Number of Connected Components in an Undirected GraphLeetCode
- 翻譯|How to Export a Connected ComponentExport
- Clone Graph leetcode java(DFS and BFS 基礎)LeetCodeJava
- DFS樹
- Windows Server DFSWindowsServer
- Codeforces Round #689 (Div. 2, based on Zed Code Competition)-B. Find the Spruce(DFS+記憶化搜尋)Zed
- SAP Connected Logistics解決方案
- Shaping Regions(dfs)API
- RMAN 6217 not connected to auxiliary database with a net service nameUXDatabase
- WPF draw graph
- Important directory for OBIEE 11g for system component and java componentImportJava
- 什麼是@Component,@Component的作用是什麼
- 論文解讀(Graph-MLP)《Graph-MLP: Node Classification without Message Passing in Graph》
- HotSpot JVM ComponentHotSpotJVM
- DFS演算法原理演算法
- Entity Framework Tutorial Basics(21):CRUD Operation in Connected ScenarioFramework
- Connected to an idle instance – while database is runningWhileDatabase
- [LintCode] Daily TemperaturesAI
- LintCode 子樹
- LintCode-Backpack
- LintCode-HeapifyAPI
- 圖論(Graph Theory)圖論Graph Theory
- Graph Theory with ApplicationsGraph TheoryAPP
- jQuery find()jQuery
- find 命令
- find命令
- React 手稿 – Component stateReact
- 初識 Web ComponentWeb
- 關於元素排列的DFS
- 【題目整理】dfs入門
- bzoj4500: 矩陣(dfs)矩陣
- 【DFS】HDU 5423 Rikka with Tree
- DFS lock handle事件處理事件
- Oil Deposits(DFS,基礎題)
- DFS剪枝最佳化策略
- 一文了解 Nebula Graph DBaaS 服務——Nebula Graph Cloud ServiceCloud
- Paper -- DenseNet:Densely Connected Convolutional NetworkSENet
- asmcmd:connected to an idle instance ORA12705ASM