[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 label and a list of its neighbors. (a connected set of a directed graph is a subgraph in which any two vertices are connected by direct edge path.)

Example

Given graph:

A----->B  C
 \     |  |
  \    |  |
   \   |  |
    \  v  v
     ->D  E <- F

Return {A,B,D}, {C,E,F}. Since there are two connected component which are{A,B,D} and {C,E,F}

Note

Sort the element in the set in increasing order.

Solution:

并查集。遍历每一条变,更新每个节点所在集合。

/**
 * Definition for Directed graph.
 * struct DirectedGraphNode {
 *     int label;
 *     vector<DirectedGraphNode *> neighbors;
 *     DirectedGraphNode(int x) : label(x) {};
 * };
 */
class Solution {
    //use union-set to solve
private:
    int find(unordered_map<int, int> &nodeMap, int label){
        if(nodeMap.find(label) == nodeMap.end()){
            nodeMap[label] = label;
            return label;
            //if this node doesn‘t belong to any union-set, create a new set
        }else{
            //this node belongs to some set, find the root of the set
            int res = nodeMap[label];
            while(nodeMap[res] != res)
                res = nodeMap[res];
            return res;
        }
    }
public:
    /**
     * @param nodes a array of directed graph node
     * @return a connected set of a directed graph
     */
    vector<vector<int>> connectedSet2(vector<DirectedGraphNode*>& nodes) {
        unordered_map<int, int> nodeMap;
        vector<vector<int> > result;
        for(int i = 0;i < nodes.size();++i){
            for(int j = 0;j < (nodes[i]->neighbors).size();++j){
                int s1 = find(nodeMap, nodes[i]->label);
                int s2 = find(nodeMap, (nodes[i]->neighbors)[j]->label);
                if(s1 != s2){
                    //union two sets
                    if(s1 < s2) nodeMap[s2] = s1;
                    else nodeMap[s1] = s2;
                }else{
                    //do nothing
                }
            }
        }

        unordered_map<int, int> setId2VecId;
        for(int i = 0;i < nodes.size();++i){
            int label = nodes[i]->label;
            int setId = find(nodeMap, label);
            if(setId2VecId.find(setId) == setId2VecId.end()){
                vector<int> vec;
                setId2VecId[setId] = result.size();
                result.push_back(vec);
            }
            int idx = setId2VecId[setId];
            result[idx].push_back(label);
        }
        for(int i = 0;i < result.size();++i)
            sort(result[i].begin(), result[i].end());

        return result;
    }
};

Inspired by: http://www.cnblogs.com/easonliu/p/4607300.html

时间: 2024-10-10 08:45:43

[LintCode] Find the Weak Connected Component in the Directed Graph的相关文章

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 contains a label and a list of its neighbors. (a weak connected component of a directed graph is a maximum subgraph in which any two vertices are conne

[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 i

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

题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与超级图中的其它顶点相连.) 样例 给定图: A------B C \ | | \ | | \ | | \ | | D E 返回 {A,B,D}, {C,E}.其中有 2 个相连的元素,即{A,B,D}, {C,E} 解题: 广度优先+递归,写不出来,程序来源 Java程序: /** * Defini

Connected Component in Undirected Graph

Description Find connected component in undirected graph. Each node in the graph contains a label and a list of its neighbors. (A connected component of an undirected graph is a subgraph in which any two vertices are connected to each other by paths,

[email&#160;protected] Strongly Connected Component

Strongly Connected Components A directed graph is strongly connected if there is a path between all pairs of vertices. A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph. For example, there are 3 SCCs in

[HDU6271]Master of Connected Component

[HDU6271]Master of Connected Component 题目大意: 给出两棵\(n(n\le10000)\)个结点的以\(1\)为根的树\(T_a,T_b\),和一个拥有\(m(m\le10000)\)个结点的图\(G\).\(T_a,T_b\)的每一个结点上都有一个信息,表示\(G\)中的一条边\((u_i,v_i)\).对于\(i\in[1,n]\),询问从\(T_a\)和\(T_b\)上分别取出链\(1\sim i\),将链上的信息所表示的边加入\(G\)中后,\(G

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), write a function to find the number of connected components in an undirected graph. Example 1: 0 3 | | 1 --- 2 4 Given n = 5 and edges = [[0, 1], [1,

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), write a function to find the number of connected components in an undirected graph. Example 1: 0 3 | | 1 --- 2 4 Given n = 5 and edges = [[0, 1], [1,

[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), write a function to find the number of connected components in an undirected graph. Example 1: 0          3 |          | 1 --- 2    4 Given n = 5 and