[Lintcode] Find the Weak Connected Component in the Directed Graph

Source: Internet
Author: User
Tags lintcode

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 with which any of the vertices is 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 is and connected component which is{A,B,D} and {C,E,F}

Note

Sort the element in the set in increasing order.

Solution:

and check the set. Iterate through each change and update the collection where each node resides.

/** Definition for Directed graph. * struct DIRECTEDGRAPHNODE {* int label; * Vector<directedgraphnode *&G T Neighbors; * Directedgraphnode (int x): label (x) {}; * }; */classSolution {//Use union-set to solvePrivate:    intFind (unordered_map<int,int> &nodemap,intlabel) {        if(nodemap.find (label) = =Nodemap.end ()) {Nodemap[label]=label; returnlabel; //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            intres =Nodemap[label];  while(Nodemap[res]! =Res) Res=Nodemap[res]; returnRes; }    } 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(inti =0; i < nodes.size (); + +i) {             for(intj =0; J < (nodes[i]->neighbors). Size (); + +j) {                intS1 = Find (Nodemap, nodes[i]->label); intS2 = Find (Nodemap, (nodes[i]->neighbors) [j]->label); if(S1! =S2) {                    //Union sets                    if(S1 < s2) NODEMAP[S2] =S1; ElseNODEMAP[S1] =S2; }Else{                    // do nothing} }} unordered_map<int,int>Setid2vecid;  for(inti =0; i < nodes.size (); + +i) {            intLabel = nodes[i]->label; intSetId =Find (nodemap, label); if(Setid2vecid.find (setId) = =Setid2vecid.end ()) {Vector<int>VEC; Setid2vecid[setid]=result.size ();            Result.push_back (VEC); }            intIDX =Setid2vecid[setid];        Result[idx].push_back (label); }         for(inti =0; i < result.size (); + +i) sort (Result[i].begin (), Result[i].end ()); returnresult; }};

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

[Lintcode] Find the Weak Connected Component in the Directed Graph

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.