gitbook-way-to-algorithm
  • Introduction
  • Preface 前言
  • Content 目录
  • MathSymbolTable 数学符号表
  • Chapter-1 BasicKnowledge 第1章 基础知识
    • TimeComplexity 时间复杂度
    • Recursion 递归式
  • Chapter-2 Sort 第2章 排序
    • InsertSort 插入排序
    • BubbleSort 冒泡排序
    • QuickSort 快速排序
    • MergeSort 归并排序
  • Chapter-3 Search 第3章 搜索
    • BinarySearch 二分查找法(折半查找法)
    • AdditionMultiplicationPrinciple 加法乘法原理
    • BruteForce 暴力枚举
    • Recursion 递归
    • BreadthFirstSearch 广度优先搜索
    • BidirectionalBreadthSearch 双向广度搜索
    • AStarSearch A*搜索
    • DancingLink 舞蹈链
  • Chapter-4 DataStructure 第4章 数据结构
    • DisjointSet 并查集
    • PrefixTree(TrieTree) 前缀树
    • LeftistTree(LeftistHeap) 左偏树(左偏堆)
    • SegmentTree 线段树
    • FenwickTree(BinaryIndexedTree) 树状数组
    • BinarySearchTree 二叉查找树
    • AVLTree AVL平衡树
    • RedBlackTree 红黑树
    • SkipList 跳跃表
    • BPlusTree B+树
    • BMinusTree B-树
  • Chapter-5 DynamicProgramming 第5章 动态规划
    • Section-1 LinearDP 第1节 线性动规
      • LongestCommonSubsequence 最长公共子序列
      • LongestIncreasingSubsequence 最长递增子序列
      • BidirectionalSubsequence 双向子序列
      • MaximumContinuousSubsequenceSum 最大连续子序列和
      • LongestPalindromicSubsequence 最长回文子序列
    • Section-2 BagDP 第2节 背包问题
      • 01-Bag 01背包
      • CompleteBag 完全背包
      • TwoDimensionBag 二维背包
    • Section-3 RegionalDP 第3节 区域动规
      • MinimumMergeCost - 最小合并代价
      • UniquePath 唯一路径
      • TrianglePath 三角形路径
    • Section-4 TreeDP 第4节 树形动规
      • MaximumBinaryTree 最大二叉树
      • MaxMultipleTree 最大多叉树
      • MaximumBinaryTreeRadiusSum 最大二叉树和
  • Chapter-6 GraphTheory 第6章 图论
    • Section-1 Traverse 第1节 遍历
      • DepthFirstSearch 深度优先搜索
      • BreadthFirstSearch 广度优先搜索
      • TopologicalSort 拓扑排序
      • EulerCycle 欧拉回路
    • Section-2 MinSpanningTree 第2节 最小生成树
      • Kruskal Kruskal算法
      • Prim Prim算法
    • Section-3 ShortestPath 第3节 最短路径
      • BellmanFord BellmanFord算法
      • Dijkstra Dijkstra算法
      • FloydWarshall FloydWarshall算法
      • DifferentConstraints 差分约束
    • Section-4 StronglyConnectedComponents 第4节 强连通分支
      • Kosaraju Kosaraju算法
      • Tarjan Tarjan算法
      • 2-SAT 2-SAT问题
    • Section-5 NetworkFlow 第5节 网络流
      • EdmondsKarp EdmondsKarp算法(最短路径增广算法)
      • PushRelabel 压入与重标记算法
      • Dinic - Dinic算法
      • MinimumCostFlow - 最小费用流
      • MultipleSourceMultipleSinkMaxflow - 多源点多汇点最大流
    • Section-6 BinaryMatch 第6节 二分匹配
      • Hungarian 匈牙利算法
      • HopcroftKarp Hopcroft-Karp算法
      • MatchToMaxflow 二分匹配转化为最大流
      • KuhnMunkres Kuhn-Munkres算法
      • Introduction-Domination,Independent,Covering,Clique 介绍支配集、独立集、覆盖集和团
      • WeightedCoveringAndIndependentSet 最小点权覆盖和最大点权独立集
      • MinimumDisjointPathCovering 最小不相交路径覆盖
      • MinimumJointPathCovering 最小可相交路径覆盖
      • Coloring 染色问题
  • Chapter-7 CombinatorialMathematics 第7章 组合数学
    • FullPermutation 全排列
    • Combination 组合
    • Permutation 排列
    • PermutationGroup 置换群
  • Chapter-8 NumberTheory 第8章 数论
    • PrimeSieve 素数筛法
    • GreatestCommonDivisor 最大公约数
    • Euclid 欧几里得算法
    • ExtendedEuclid 扩展欧几里得算法
    • ChineseRemainerTheorem 中国剩余定理
    • ModularExponentiation 模幂运算
  • Chapter-9 LinearAlgebra 第9章 线性代数
    • Section-1 Matrix 第1节 矩阵
      • Strassen Strassen算法
      • GaussElimination 高斯消元法
      • LUP LUP分解
      • InverseMatrix 矩阵求逆
    • Section-2 LinearProgramming 第2节 线性规划
      • Simplex 单纯形算法
      • Dinkelback Dinkelback算法
  • Chapter-10 AnalyticGeometry 第10章 解析几何
    • Section-1 Polygon 第1节 多边形
      • Cross 向量叉积
      • SegmentIntersection 线段相交
      • Sweeping 扫除算法
      • ConvexPolygonArea 凸多边形面积
      • ConvexPolygonGravityCenter 凸多边形重心
      • NearestNeighbor 最近点对
    • Section-2 ConvexHull 第2节 凸包
      • GrahamScan Graham扫描算法
      • QuickHull 快速凸包算法
      • RotatingCalipers 旋转卡壳
  • Chapter-11 PatternMatch 第11章 文本匹配
    • SimpleMatch 简单匹配
    • AhoCorasickAutomata AC自动机
    • KnuthMorrisPratt KMP匹配算法
    • RabinKarp RabinKarp算法
    • BoyerMoore BoyerMoore算法
  • Chapter-12 GameTheory 第12章 博弈论
    • BashGame 巴什博弈
    • WythoffGame 威佐夫博弈
    • NimGame 尼姆博弈
Powered by GitBook
On this page
  • 并查集
  • 源码
  • 测试
  1. Chapter-4 DataStructure 第4章 数据结构

DisjointSet 并查集

PreviousChapter-4 DataStructure 第4章 数据结构NextPrefixTree(TrieTree) 前缀树

Last updated 6 years ago

并查集

拥有nnn个元素的集合s=[x0,x1,x2,⋯ ,xn−1]s = [ x_0, x_1, x_2, \cdots , x_{n-1} ]s=[x0​,x1​,x2​,⋯,xn−1​]分为两个家庭。每个家族只有唯一一个祖先,相同家庭的元素拥有相同的祖先。因此集合sss中的每个元素的祖先只有222种可能。并查集是一种适合元素分类的高效树形数据结构,支持快速分类和查询。

设father(x)father(x)father(x)为xxx的父节点,设ancestor(x)ancestor(x)ancestor(x)为xxx的祖先节点。当father(x)=xfather(x) = xfather(x)=x时称xxx为祖先节点。

对于拥有101010个成员的集合s=[0,1,2,3,4,5,6,7,8,9]s = [ 0,1,2,3,4,5,6,7,8,9 ]s=[0,1,2,3,4,5,6,7,8,9],将其分成两个家庭AAA和BBB。初始时令每个成员的父亲都是自己,如图所示:

当声明222个成员xix_ixi​和xjx_jxj​(xi≤xjx_i \le x_jxi​≤xj​)属于同一家庭,令xix_ixi​的节点祖先为xjx_jxj​的父亲(也可以反过来),即father(xj)=ancestor(xi)father(x_j) = ancestor(x_i)father(xj​)=ancestor(xi​)。这样的操作会使元素xjx_jxj​更接近祖先节点,从而缩短了递归向上查找的路径长度,该操作也称为压缩路径。

下面对上图中的集合sss进行具体演示:

(1)(1)(1)声明000和444属于同一家庭,设置father(4)=ancestor(0)=0father(4) = ancestor(0) = 0father(4)=ancestor(0)=0;

(2)(2)(2)声明111和999节点属于同一家庭,设置father(9)=ancestor(1)=1father(9) = ancestor(1) = 1father(9)=ancestor(1)=1;

(3)(3)(3)声明000和222节点属于同一家庭,设置father(2)=ancestor(0)=0father(2) = ancestor(0) = 0father(2)=ancestor(0)=0;

(4)(4)(4)声明111和333节点属于同一家庭,设置father(3)=ancestor(1)=1father(3) = ancestor(1) = 1father(3)=ancestor(1)=1;

(5)(5)(5)声明333和555节点属于同一家庭,设置father(5)=ancestor(3)=1father(5) = ancestor(3) = 1father(5)=ancestor(3)=1;

(6)(6)(6)声明666和888节点属于同一家庭,设置father(8)=ancestor(6)=6father(8) = ancestor(6) = 6father(8)=ancestor(6)=6;

(7)(7)(7)声明222和666节点属于同一家庭,设置father(6)=ancestor(2)=0father(6) = ancestor(2) = 0father(6)=ancestor(2)=0;

(8)(8)(8)声明111和777节点属于同一家庭,设置father(7)=ancestor(1)=1father(7) = ancestor(1) = 1father(7)=ancestor(1)=1;

合并两节点xxx和yyy时,根据固定规则设置father(y)=ancestor(x)father(y) = ancestor(x)father(y)=ancestor(x)(或者相反);查询节点xxx的祖宗节点时,若father(x)≠ancestor(x)father(x) \neq ancestor(x)father(x)=ancestor(x)则设置father(x)=ancestor(x)father(x) = ancestor(x)father(x)=ancestor(x)。并查集的分类、查询操作的时间复杂度接近O(1)O(1)O(1)。

源码

测试

DisjointSet.h
DisjointSet.cpp
DisjointSetTest.cpp
DisjointSet1.png
DisjointSet2.png
DisjointSet3.png
DisjointSet4.png
DisjointSet5.png
DisjointSet6.png
DisjointSet7.png
DisjointSet8.png
DisjointSet9.png