LeetCode 323. Number of Connected Components in an Undirected Graph(连通分量)

4 篇文章 0 订阅
3 篇文章 0 订阅

原题网址:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph.

Example 1:

     0          3
     |          |
     1 --- 2    4

Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2.

Example 2:

     0           4
     |           |
     1 --- 2 --- 3

Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]], return 1.

Note:
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.

方法一:深度优先搜索+并查集。

public class Solution {
    private int components;
    private void find(int prev, int node, boolean[] visit, int[] roots, List<Integer>[] graph) {
        if (visit[node]) return;
        if (prev != -1) {
            int pid = root(roots, prev);
            int rid = root(roots, node);
            if (pid != rid) {
                roots[pid] = rid;
                components --;
            }
        }
        visit[node] = true;
        for(int i=0; i<graph[node].size(); i++) {
            find(node, graph[node].get(i), visit, roots, graph);
        }
    }
    public int countComponents(int n, int[][] edges) {
        components = n;
        boolean[] visit = new boolean[n];
        List<Integer>[] graph = new List[n];
        for(int i=0; i<graph.length; i++) graph[i] = new ArrayList<>();
        for(int[] edge: edges) {
            graph[edge[0]].add(edge[1]);
            graph[edge[1]].add(edge[0]);
        }
        int[] roots = new int[n];
        for(int i=0; i<n; i++) roots[i] = i;
        for(int i=0; i<n; i++) {
            find(-1, i, visit, roots, graph);
        }
        return components;
    }
    private int root(int[] roots, int id) {
        while (id != roots[id]) {
            roots[id] = roots[roots[id]];
            id = roots[id];
        }
        return id;
    }
}

方法二:使用并查集来标记图的连通分量。

public class Solution {
    private int root(int[] roots, int i) {
        while (roots[i] != i) i = roots[roots[i]];
        return i;
    }
    public int countComponents(int n, int[][] edges) {
        int count = n;
        int[] roots = new int[n];
        for(int i=0; i<n; i++) roots[i] = i;
        for(int i=0; i<edges.length; i++) {
            int r1 = root(roots, edges[i][0]);
            int r2 = root(roots, edges[i][1]);
            if (r1 != r2) {
                roots[r2] = r1;
                count --;
            }
        }
        return count;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值