美文网首页
union-find 算法

union-find 算法

作者: 放开那个BUG | 来源:发表于2022-03-30 21:45 被阅读0次

1、前言

union-find 为并查集算法,原本的用途是判断图的连通性问题,连通性是指图中的两个节点是否相连。说到这,我们是否能想到用 dfs、bfs 判断连通性问题?其实是可以的,union-find 只是多出来一种解决思路而已。

所以记住一点,基本上能用并查集来做的题,bfs、dfs 都能做。

2、概念

并查集的概念很简单,就是原本大家都是在不同的集合里,然后通过合并,最后合并到一个集合里面,就像不同的帮派合并一样。并查集最基本的功能就是 union(合并)。代码如下:

public class UnionFind {
        private int[] parent;

        public UnionFind(int n) {
            parent = new int[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
            }
        }

        private int find(int x) {
            while (x != parent[x]) {
                parent[x] = parent[parent[x]];
                x = parent[x];
            }
            return x;
        }

        public void union(int x, int y) {
            int xRoot = find(x);
            int yRoot = find(y);
            if (xRoot == yRoot) {
                return;
            }

            parent[xRoot] = yRoot;
        }
    }

我们可以用并查集来做一个岛问题。岛问题用 dfs、bfs 都很好找,并查集也很好做。核心思想:将都是1的地方用并查集合并起来,然后最后并查集中1的数量就是岛屿的数量。

class Solution {
    public int numIslands(char[][] grid){
        if(grid == null || grid.length == 0 || grid[0].length == 0){
            return 0;
        }

        int[][] directions = {{1, 0}, {0, 1}};
        int row = grid.length, column = grid[0].length, space = 0;
        UnionFind unionFind = new UnionFind(row * column);
        for(int i = 0; i < row; i++){
            for(int j = 0; j < column; j++){
                if(grid[i][j] == '0'){
                    space++;
                }else {
                    for (int[] direction : directions) {
                        int newX = i + direction[0];
                        int newY = j + direction[1];
                        if(newX < row && newY < column && grid[newX][newY] == '1'){
                            unionFind.union(i * column + j, newX * column + newY);
                        }
                    }
                }
            }
        }

        return unionFind.count - space;
    }

    class UnionFind{
        private int[] parent;
        private int count;

        public UnionFind(int n){
            this.count = n;
            this.parent = new int[n];
            for(int i = 0; i < n; i++){
                this.parent[i] = i;
            }
        }

        private int find(int x){
            while(x != this.parent[x]){
                this.parent[x] = this.parent[this.parent[x]];
                x = this.parent[x];
            }
            return x;
        }

        public void union(int x, int y){
            int xParent = find(x);
            int yParent = find(y);
            if(xParent == yParent){
                return;
            }
            this.parent[xParent] = yParent;
            this.count--;
        }
    }
}

相关文章

  • 《算法4》1.5 - Union-Find 算法,Python实

    Union-Find 算法(中文称并查集算法)是解决动态连通性(Dynamic Conectivity)问题的一种...

  • Union-Find算法详解

    ----------- 今天讲讲 Union-Find 算法,也就是常说的并查集算法,主要是解决图论中「动态连通性...

  • C++代写:CSE100 Six Degrees of Kevi

    代写算法类作业,需要用到BFS和Union-Find两个算法。这个作业比较坑的地方是需要对算法进行优化,否则计算不...

  • Union-Find算法

    动态连通性问题中,如何判断触点是否相连接,可以抽象表述为如何编写程序来过滤掉序列中所有无意义的整数对。连通性问题只...

  • union-find算法

    Api: 加权quick-union算法:将小数的根节点连接到大树的根节点 最优解法:路径压缩的加权quick-u...

  • union-find 算法

    1、前言 union-find 为并查集算法,原本的用途是判断图的连通性问题,连通性是指图中的两个节点是否相连。说...

  • 基础算法-Union-Find(动态连通性)--quick-fi

    今天学习的算法是比较基础和典型的算法Union-Find(动态连通性)。 题目介绍 问题的输入是一系列整数对,其中...

  • Union-Find并查集算法

    1. 概念 Union-Find 算法通常叫做并查集算法,它主要用于处理集合的合并和查询问题。顾名思义,Union...

  • LeetCode之Satisfiability of Equal

    问题: 方法:很有意思的一道题,主要用到了Union-Find算法,需要先学习下这个算法。其中最主要的是find(...

  • 基础算法-Union-Find(动态连通性)--加权quick-

    今天是基于Union-Find(动态连通性)的quick-union的优化,称为加权quick-union算法实现...

网友评论

      本文标题:union-find 算法

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