美文网首页LintCode解题思路LintCode解题思路
lintcode 432. 找出有向图中的弱联通分量

lintcode 432. 找出有向图中的弱联通分量

作者: Anseis | 来源:发表于2018-04-30 09:46 被阅读1次

中等题,lintcode
利用并查集来把每个集合的node 给确定起来, 首先建立并查集结构,然后利用connect方法把所有的点都归在各自的大集合下。

最后遍历一遍点,根据集合将统一联通块内的点加入到一个list,这样就可以得到结果
代码如下

/**
 * Definition for Directed graph.
 * class DirectedGraphNode {
 *     int label;
 *     ArrayList<DirectedGraphNode> neighbors;
 *     DirectedGraphNode(int x) { label = x; neighbors = new ArrayList<DirectedGraphNode>(); }
 * };
 */


public class Solution {
    /*
     * @param nodes: a array of Directed graph node
     * @return: a connected set of a directed graph
     */
     
    public List<List<Integer>> connectedSet2(ArrayList<DirectedGraphNode> nodes) {
        // write your code here
        List<List<Integer>> res = new ArrayList<>();
        if(nodes == null || nodes.size() == 0){
            return res;
        }
        List<Integer> list = new ArrayList<>();
        for(DirectedGraphNode node:nodes){
            list.add(node.label);
        }
        UnionFind uf = new UnionFind(list);
        
        //对所有的点进行归类处理
        for(DirectedGraphNode node: nodes){
            for(DirectedGraphNode nei: node.neighbors){
                uf.connect(nei.label, node.label);
            }
        }
        // set里面存放集合编号
        Set<Integer> set = new HashSet<>();
        for(int i = 0; i <nodes.size(); i++){
            System.out.println(uf.find(nodes.get(i).label));
            if(!set.contains(uf.find(nodes.get(i).label))){
                set.add(uf.find(nodes.get(i).label));
                int label = uf.find(nodes.get(i).label);
                List<Integer> ls = new ArrayList<>();
                for(int j = 0; j <nodes.size(); j++ ){
                    if(uf.find(nodes.get(j).label) == label){
                        ls.add(nodes.get(j).label);
                    }
                }
                res.add(ls);
            }
        }
        return res;
    }
}
class UnionFind{
    Map<Integer, Integer> map = new HashMap<>();
 
    public UnionFind(List<Integer> ls){
        for(int a: ls){
            map.put(a, a);
        }
    }
    
    int find(int x){
        if(map.get(x) == x){
            return x;
        }
        int f = find(map.get(x));
        map.put(x, f);
        return map.get(x);
    }
    
    void connect(int a, int b){
        int f_a = find(a);
        int f_b = find(b);
        
        if(f_a != f_b){
            map.put(f_a, f_b);
        }
    }
}

相关文章

  • lintcode 432. 找出有向图中的弱联通分量

    中等题,lintcode利用并查集来把每个集合的node 给确定起来, 首先建立并查集结构,然后利用connect...

  • LintCode 432 [Find the Weak Conn

    原题 请找出有向图中弱联通分量的数目。图中的每个节点包含其邻居的 1 个标签和1 个列表。 (一个有向图中的相连节...

  • 2021-01-09

    通过移除图中的某个节点可以增加图的联通分量,则这个节点为关键节点,可以通过,去除某个节点,不断计算其联通分量是否增...

  • 几个概念:有向图、无向图、加权图、简单图、联通、联通分量、生成树、强连通分量、强联通图图的存储:邻接矩阵(二维、一...

  • 活动分组——无向图的连通分量个数

    一、相关概念 连通分量 无向图中,极大连通子图称为连通分量1)连通图的连通分量只有一个,即自身2)非连通的无向图有...

  • 【Python算法】算法基础-概念区分

    图论: 连通图: 连通图基于联通的概念。在一个无向图中,若顶点a,到b有路径相连,则称a,b是连通的。如果图中的任...

  • 10.图的深度优先遍历、联通分量与寻路

    图的深度优先遍历、联通分量与寻路 点击这里,前提知晓... 深度优先遍历对有向图和无向图都可以使用 一、图的深度优...

  • Graphx图算法【6】强联通分量StronglyConnect

    强连通分量是指在有向图中,如果两个顶点 、 之间有一条从 到 的有向路径,同时还有一条从 到 的有...

  • NetworksX 图 使用

    1.有向图 2.无向图 3.节点颜色图 计算:计算1:求无向图的任意两点间的最短路径 结果 计算2:求出有向图中得...

  • SCC算法初解

    在算法学习之路上漂泊,遇见了图,而分无向与有向。在本文中主要讲解关于有向图中的求极大连通分量的算法,主要是Kasa...

网友评论

    本文标题:lintcode 432. 找出有向图中的弱联通分量

    本文链接:https://www.haomeiwen.com/subject/onrzlftx.html