https://blog.csdn.net/LKJgdut/article/details/106913884
代码如下:
第一版并查集 Quick Find
#include <cassert>
using namespace std;
namespace UF1 {
class UnionFind {
private:
int *id;
int count;
public:
UnionFind(int n) {
count = n;
id = new int[n];
for (int i = 0; i < n; i++)
id[i] = i;
}
~UnionFind() {
delete[] id;
}
int find(int p) {
assert(p >= 0 && p < count);
return id[p];
}
bool isConnected(int p, int q) {
return find(p) == find(q);
}
void unionElements(int p, int q) {
int pID = find(p);
int qID = find(q);
if (pID == qID)
return;
for (int i = 0; i < count; i++)
if (id[i] == pID)
id[i] = qID;
}
};
}
**
这种QuickUnion才是并查集真正的实现思路
**
#include <cassert>
using namespace std;
namespace UF2{
class UnionFind{
private:
int* parent;
int count;
public:
UnionFind(int count){//最开始,将每一个下标对应元素指向自己(即没有与其他元素属于一个集合)
parent = new int[count];
this->count = count;
for( int i = 0 ; i < count ; i ++ )
parent[i] = i;
}
~UnionFind(){
delete[] parent;
}
/**
查找过程, 查找元素p所对应的集合编号,即查找元素p所在树的根结点元素的编号
O(h)复杂度, h为树的高度
*/
int find(int p){
assert( p >= 0 && p < count );
while( p != parent[p] )
p = parent[p];
return p;
}
bool isConnected( int p , int q ){
return find(p) == find(q);
}
void unionElements(int p, int q){
int pRoot = find(p);
int qRoot = find(q);
if( pRoot == qRoot )
return;
parent[pRoot] = qRoot;
}
};
}
基于size的优化
namespace UF3{
class UnionFind{
private:
int* parent;
int* sz; // sz[i]表示以i为根的集合中元素个数
int count;
public:
UnionFind(int count){
parent = new int[count];
sz = new int[count];
this->count = count;
for( int i = 0 ; i < count ; i ++ ){
parent[i] = i;
sz[i] = 1;
}
}
~UnionFind(){
delete[] parent;
delete[] sz;
}
int find(int p){
assert( p >= 0 && p < count );
while( p != parent[p] )
p = parent[p];
return p;
}
bool isConnected( int p , int q ){
return find(p) == find(q);
}
void unionElements(int p, int q){
int pRoot = find(p);
int qRoot = find(q);
if( pRoot == qRoot )
return;
if( sz[pRoot] < sz[qRoot] ){
parent[pRoot] = qRoot;
sz[qRoot] += sz[pRoot];
}
else{
parent[qRoot] = pRoot;
sz[pRoot] += sz[qRoot];
}
}
};
}
基于rank的优化
namespace UF3{
class UnionFind{
private:
int* parent;
int* rank; // rank[i]表示以i为根的集合所表示的树的层数
int count;
public:
UnionFind(int count){
parent = new int[count];
rank = new int[count];
this->count = count;
for( int i = 0 ; i < count ; i ++ ){
parent[i] = i;
rank[i] = 1;
}
}
~UnionFind(){
delete[] parent;
delete[] rank;
}
int find(int p){
assert( p >= 0 && p < count );
while( p != parent[p] )
p = parent[p];
return p;
}
bool isConnected( int p , int q ){
return find(p) == find(q);
}
void unionElements(int p, int q){
int pRoot = find(p);
int qRoot = find(q);
if( pRoot == qRoot )
return;
if( rank[pRoot] < rank[qRoot] ){
parent[pRoot] = qRoot;
}
else if( rank[qRoot] < rank[pRoot]){
parent[qRoot] = pRoot;
}
else{ // rank[pRoot] == rank[qRoot]
parent[pRoot] = qRoot;
rank[qRoot] += 1;
}
}
};
}
路径压缩
namespace UF5{
class UnionFind{
private:
int* parent;
int* rank;
int count;
public:
UnionFind(int count){
parent = new int[count];
rank = new int[count];
this->count = count;
for( int i = 0 ; i < count ; i ++ ){
parent[i] = i;
rank[i] = 1;
}
}
~UnionFind(){
delete[] parent;
delete[] rank;
}
int size(){
return count;
}
bool isConnected( int p , int q ){
return find(p) == find(q);
}
int find(int p){
assert( p >= 0 && p < count );
// path compression 1
while( p != parent[p] ){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
// path compression 2
// if( p != parent[p] )
// parent[p] = find( parent[p] );
// return parent[p];
}
void unionElements(int p, int q){
int pRoot = find(p);
int qRoot = find(q);
if( pRoot == qRoot )
return;
if( rank[pRoot] < rank[qRoot] )
parent[pRoot] = qRoot;
else if( rank[qRoot] < rank[pRoot])
parent[qRoot] = pRoot;
else{ // rank[pRoot] == rank[qRoot]
parent[pRoot] = qRoot;
rank[qRoot] ++;
}
}
void show(){
for( int i = 0 ; i < count ; i ++ )
cout<<i<<" : "<<parent[i]<<endl;
}
};
}