编程语言
首页 > 编程语言> > Day34-数据结构与算法-并查集

Day34-数据结构与算法-并查集

作者:互联网


title: Day34-数据结构与算法-并查集
date: 2020-12-14 17:59:00
author: 子陌


常用的经典数据结构

并查集-需求分析

并查集(Union Find)

如何存储数据

如何存储数据

接口设计

初始化

初始化

并查集公共抽象类抽取

package com.zimo.算法.并查集;

/**
 * 并查集 - 公共抽象类抽取
 *
 * @author Liu_zimo
 * @version v0.1 by 2020/12/15 10:16
 */
public abstract class UnionFind {
    protected int[] parents;

    public UnionFind(int capacity) {
        if (capacity < 0){
            throw new IllegalArgumentException("capacity must be >= 1");
        }
        parents = new int[capacity];
        for (int i = 0; i < parents.length; i++) {
            parents[i] = i;
        }
    }

    /**
     * 查找v所属的集合(根节点)
     * @param v
     * @return
     */
    public abstract int find(int v);

    /**
     * 合并v1、v2所在的集合
     */
    public abstract void union(int v1, int v2);

    /**
     * 检查v1、v2是否属于同一个集合
     * @return 返回检查结果
     */
    public boolean isSame(int v1, int v2){
        return find(v1) == find(v2);
    }

    protected void rangeCheck(int v){
        if (v < 0 || v >= parents.length) throw new IllegalArgumentException("v is out of bounds");
    }
}

Quick Find实现

Quick-Find

package com.zimo.算法.并查集.QuickFind;

import com.zimo.算法.并查集.UnionFind;

/**
 * 并查集 - Quick_Find
 *
 * @author Liu_zimo
 * @version v0.1 by 2020/12/15 10:16
 */
public class QuickFind extends UnionFind {
    public QuickFind(int capacity) {
        super(capacity);
    }

    public int find(int v){
        rangeCheck(v);
        return parents[v];
    }

    /**
     * 将v1所在的集合所有元素,嫁接到v2的父节点上
     */
    public void union(int v1, int v2){
        int p1 = find(v1);
        int p2 = find(v2);
        if (p1 == p2) return;

        for (int i = 0; i < parents.length; i++) {
            if (parents[i] == p1){
                parents[i] = p2;
            }
        }
    }
}

Quick Union实现

Quick-Union

package com.zimo.算法.并查集.QuickUnion;

import com.zimo.算法.并查集.UnionFind;

/**
 * 并查集 - Quick_Union
 *
 * @author Liu_zimo
 * @version v0.1 by 2020/12/15 18:00
 */
public class QuickUnion extends UnionFind {
    public QuickUnion(int capacity) {
        super(capacity);
    }

    /**
     * 通过parent链表不断地向上找,直到找到根节点
     */
    @Override
    public int find(int v) {
        rangeCheck(v);
        while (v != parents[v]){
            v = parents[v];
        }
        return v;
    }

    /**
     * 将v1的根节点嫁接到v2的根节点上
     */
    @Override
    public void union(int v1, int v2) {
        int p1 = find(v1);
        int p2 = find(v2);
        if (p1 == p2) return;
        parents[p1] = p2;
    }
}

Quick Union优化

基于size 的优化
package com.zimo.算法.并查集.QuickUnion;

/**
 * 并查集 - Quick_Union - 基于size 的优化
 *      元素少的树 嫁接到 元素多的树
 *
 * @author Liu_zimo
 * @version v0.1 by 2020/12/16 10:33:50
 */
public class QuickUnion_Size extends QuickUnion {
    private int[] sizes;
    public QuickUnion_Size(int capacity) {
        super(capacity);
        sizes = new int[capacity];
        for (int i = 0; i < capacity; i++) {
            sizes[i] = 1;
        }
    }

    @Override
    public void union(int v1, int v2) {
        int p1 = find(v1);
        int p2 = find(v2);
        if (p1 == p2) return;
        if (sizes[p1] < sizes[p2]){
            parents[p1] = p2;
            sizes[p2] += sizes[p1];
        }else {
            parents[p2] = p1;
            sizes[p1] += sizes[p2];
        }
    }
}
基于rank的优化
package com.zimo.算法.并查集.QuickUnion;

/**
 * 并查集 - Quick_Union - 基于rank的优化
 *      矮的树 嫁接到 高的书
 *
 * @author Liu_zimo
 * @version v0.1 by 2020/12/16 10:52:48
 */
public class QuickUnion_Rank extends QuickUnion {
    private int[] ranks;
    public QuickUnion_Rank(int capacity) {
        super(capacity);
        ranks = new int[capacity];
        for (int i = 0; i < capacity; i++) {
            ranks[i] = 1;
        }
    }

    @Override
    public void union(int v1, int v2) {
        int p1 = find(v1);
        int p2 = find(v2);
        if (p1 == p2) return;
        if (ranks[p1] < ranks[p2]){
            parents[p1] = p2;
        }else if (ranks[p1] > ranks[p2]){
            parents[p2] = p1;
        }else {
            parents[p1] = p2;
            ranks[p2] += 1;     // 如果两个树高一样,那么嫁接之后高度才会发生变化
        }
    }
}

1.路径压缩优化(Path Compression Question)

路径压缩

package com.zimo.算法.并查集.QuickUnion;

/**
 * 并查集 - Quick_Union - 基于rank的优化 + 路劲压缩
 *
 * @author Liu_zimo
 * @version v0.1 by 2020/12/16 10:52:48
 */
public class QuickUnion_RankPathCompression extends QuickUnion_Rank {

    public QuickUnion_RankPathCompression(int capacity) {
        super(capacity);
    }

    @Override
    public int find(int v) {
        rangeCheck(v);
        if (parents[v] != v){
            parents[v] = find(parents[v]);
        }
        return parents[v];
    }
}

路径分裂-减半

2.路径分裂(Path Spliting)

package com.zimo.算法.并查集.QuickUnion;

/**
 * 并查集 - Quick_Union - 基于rank的优化 + 路劲分裂
 *      使路径上的每个节点都指向其祖父节点(parent的parent)
 *
 * @author Liu_zimo
 * @version v0.1 by 2020/12/18 11:18:45
 */
public class QuickUnion_RankPathSpliting extends QuickUnion_Rank {

    public QuickUnion_RankPathSpliting(int capacity) {
        super(capacity);
    }

    @Override
    public int find(int v) {
        rangeCheck(v);
        while (v  != parents[v]){
            int p = parents[v];
            parents[v] = parents[parents[v]];
            v = p;
        }
        return v;
    }
}

3.路径减半(Path Halving)

package com.zimo.算法.并查集.QuickUnion;

/**
 * 并查集 - Quick_Union - 基于rank的优化 + 路劲减半
 *      使路径上每隔一个节点就指向其祖父节点(parent的parent)
 *
 * @author Liu_zimo
 * @version v0.1 by 2020/12/18 11:33:12
 */
public class QuickUnion_RankPathHalving extends QuickUnion_Rank {

    public QuickUnion_RankPathHalving(int capacity) {
        super(capacity);
    }

    @Override
    public int find(int v) {
        rangeCheck(v);
        while (v  != parents[v]){
            parents[v] = parents[parents[v]];
            v = parents[v];
        }
        return v;
    }
}

总结

如果是自定义类型,想使用并查集

  1. 方案1:自定类型转成整型后使用并查集(比如生成哈希值)
  2. 方案2:使用链表 + 映射(Map)

标签:capacity,int,Day34,查集,QuickUnion,parents,数据结构,public
来源: https://blog.csdn.net/qq_38205875/article/details/111402333