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

  • 相关阅读:
    Android 图片的缩略图
    Android 非Activity类引用getResources()方法问题的解决方法
    Android 广播(内部类)
    Android 消息广播Intent传递数据
    Android 防止按钮连续点击的方法(Button,ImageButton等)
    Android 广播机制(两种注册方法)与中断广播
    Android 使用意图传递数据
    Android 使用全局变量传递数据
    Android 使用剪切板传递数据
    android 使用静态变量传递数据
  • 原文地址:https://www.cnblogs.com/lz87/p/7500093.html
Copyright © 2020-2023  润新知