Find the Weak Connected Component in the Directed Graph

Posted flagyuri

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 connected by direct edge path.)

Clarification

graph model explaination:
https://www.lintcode.com/help/graph

Example

Example 1:

Input: {1,2,4#2,4#3,5#4#5#6,5}
Output: [[1,2,4],[3,5,6]]
Explanation:
  1----->2    3-->5
        |        ^
        |        |
        |        6
        v
       ->4

Example 2:

Input: {1,2#2,3#3,1}
Output: [[1,2,3]]

思路:

可以把每条有向边看成无向边, 就等同于在无向图中寻找联通块.

两种做法: 1. BFS/DFS 2. 并查集 (但由于输入数据是有向边, 所以使用并查集更合适, 否则还需要先把有向边转换成无向边)

public class Solution {
    class UnionFind {
        HashMap<Integer, Integer> father = new HashMap<Integer, Integer>();
        UnionFind(HashSet<Integer> hashSet) {
            for(Integer now : hashSet) {
                father.put(now, now);
            }
        }
        int find(int x) {
            int parent =  father.get(x);

            while(parent != father.get(parent)) {
                parent = father.get(parent);
            }

            return parent;
        }
        int compressed_find(int x) {
            int parent =  father.get(x);

            while (parent != father.get(parent)) {
                parent = father.get(parent);
            }

            int temp = -1;
            int fa = father.get(x);

            while (fa != father.get(fa)) {
                temp = father.get(fa);
                father.put(fa, parent) ;
                fa = temp;
            }

            return parent;

        }

        void union(int x, int y) {
            int fa_x = find(x);
            int fa_y = find(y);

            if (fa_x != fa_y)
                father.put(fa_x, fa_y);
        }
    }
    
    List<List<Integer>>  print(HashSet<Integer> hashSet, UnionFind uf) {
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        HashMap<Integer, List <Integer>> hashMap = new HashMap<Integer, List<Integer>>();

        for (int i : hashSet) {
            int fa = uf.find(i);

            if (!hashMap.containsKey(fa)) {
                hashMap.put(fa,  new ArrayList<Integer>());
            }

            List<Integer> now =  hashMap.get(fa);
            now.add(i);
            hashMap.put(fa, now);
        }

        for (List<Integer> now: hashMap.values()) {
            Collections.sort(now);
            ans.add(now);
        }

        return ans;
    }

    public List<List<Integer>> connectedSet2(ArrayList<DirectedGraphNode> nodes) {
        HashSet<Integer> hashSet = new HashSet<Integer>();

        for (DirectedGraphNode now : nodes) {
            hashSet.add(now.label);

        for (DirectedGraphNode neighbour : now.neighbors) {
                hashSet.add(neighbour.label);
            }
        }

        UnionFind uf = new UnionFind(hashSet);

        for(DirectedGraphNode now : nodes) {
            for(DirectedGraphNode neighbour : now.neighbors) {
                int fnow = uf.find(now.label);
                int fneighbour = uf.find(neighbour.label);

                if (fnow != fneighbour) {
                    uf.union(now.label, neighbour.label);
                }
            }
        }
        return print(hashSet , uf);
    }
}

  

以上是关于Find the Weak Connected Component in the Directed Graph的主要内容,如果未能解决你的问题,请参考以下文章

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

[Redux] Using withRouter() to Inject the Params into Connected Components

Form submission canceled because the form is not connected

C - Find The Multiple

观文总结——A Survey of the Connected Vehicle Landscape—Architectures, Enabling Technologies, Applications

Connected to the target VM, address: '127.0.0.1:4405', transport: 'socket'