zoukankan      html  css  js  c++  java
  • [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 weak connected component of a directed graph is a subgraph in which any two vertices are connected by direct edge path.)

    Sort the element in each component in increasing order.

    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}

    Analysis

    The difference between this problem and Connected Component in Undirected Graph is that in this problem we have a directed graph, not undirected.

    In Connected Component in Undirected Graph both bfs and union find can be used to solve that problem. However, bfs is not suited for this problem of directed graph. An incoming edge to a bfs source vertex is not visible to this source vertex, causing it unable to include its upstream vertex into the same weak connected component.  Union find is a solution that works well both on undirected and directed graphs.

    Algorithm

    1. Define a union find with hashmap as its internal storage data structure.

    2. Connect all graph nodes using all the edges.

    3. Iterate through the uf and collect all weakly connected components. Add nodes that share the same root to the same component.

     1 /**
     2  * Definition for Directed graph.
     3  * class DirectedGraphNode {
     4  *     int label;
     5  *     ArrayList<DirectedGraphNode> neighbors;
     6  *     DirectedGraphNode(int x) { label = x; neighbors = new ArrayList<DirectedGraphNode>(); }
     7  * };
     8  */
     9 public class Solution {
    10     /**
    11      * @param nodes a array of Directed graph node
    12      * @return a connected set of a directed graph
    13      */
    14     private class UnionFind {
    15         private HashMap<Integer, Integer> father = null;
    16         public UnionFind(HashSet<Integer> labels){
    17             father = new HashMap<Integer, Integer>();
    18             for(int label : labels){
    19                 father.put(label, label);
    20             }
    21         }
    22         public int find(int label){
    23             int parent = father.get(label);
    24             while(parent != father.get(parent)){
    25                 parent = father.get(parent);
    26             }
    27             int next;
    28             while(label != father.get(label)){
    29                 next = father.get(label);
    30                 father.put(label, parent);
    31                 label = next;
    32             }
    33             return parent;
    34         }
    35         public void connect(int x, int y){
    36             int root_x = find(x);
    37             int root_y = find(y);
    38             if(root_x != root_y){
    39                 father.put(root_x, root_y);
    40             }
    41         }
    42     }
    43     public List<List<Integer>> connectedSet2(ArrayList<DirectedGraphNode> nodes) {
    44         List<List<Integer>> results = new ArrayList<List<Integer>>();
    45         if(nodes == null || nodes.size() == 0){
    46             return results;
    47         }
    48         HashSet<Integer> labels = new HashSet<Integer>();
    49         for(DirectedGraphNode node : nodes){
    50             labels.add(node.label);
    51             for(DirectedGraphNode neighbor : node.neighbors){
    52                 labels.add(neighbor.label);
    53             }
    54         }
    55         UnionFind uf = new UnionFind(labels);
    56         for(DirectedGraphNode node : nodes){
    57             for(DirectedGraphNode neighbor : node.neighbors){
    58                 uf.connect(node.label, neighbor.label);    
    59             }
    60         }
    61         getAllWCC(labels, uf, results);
    62         return results;
    63     }
    64     private void getAllWCC(HashSet<Integer> labels,
    65                             UnionFind uf,
    66                             List<List<Integer>> results){
    67         HashMap<Integer, ArrayList<Integer>> wcc = new HashMap<Integer, ArrayList<Integer>>();
    68         for(int label : labels){
    69             int labelRoot = uf.find(label);
    70             if(!wcc.containsKey(labelRoot)){
    71                 wcc.put(labelRoot, new ArrayList<Integer>());
    72             }
    73             wcc.get(labelRoot).add(label);
    74         }
    75         for(ArrayList<Integer> comp : wcc.values()){
    76             Collections.sort(comp);
    77             results.add(comp);
    78         }
    79     }
    80 }

    Related Problems

    Connected Component in Undirected Graph 

  • 相关阅读:
    开源 .net license tool, EasyLicense !
    Logging with NLog
    Logging with Log4net (二)
    Logging with Debug And Trace (一)
    ThoughtWorks代码挑战——FizzBuzzWhizz
    开源插件 :MahApps.Metro.IconPacks
    Java地址:
    小程序源码下载[demo整理自github]
    多块图形合并(自动合并相交块)
    Textbox
  • 原文地址:https://www.cnblogs.com/lz87/p/7500093.html
Copyright © 2011-2022 走看看