程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 並查集UFSet類

並查集UFSet類

編輯:關於C++

/*

Name: 並查集UFSet類

Copyright: 始發於goal00001111的專欄;允許自由轉載,但必須注明作者和出處

Author: goal00001111

Date: 23-12-08 15:21

Description: 實現了普通的查找和合並的算法,也實現了壓縮路徑和按大小求並高效 算法,並對兩者進行了測試比較。

有關算法的分析討論詳見拙作《一種簡單而有趣的數據結構--並查集》:

http://blog.csdn.net/goal00001111/archive/2008/12/24/3595844.aspx

*/

#include <iostream>
#include <time.h>
using namespace std;
const int MAX = 50000; //集合的最大成員數量
class UFSet{ //並查集類
public:
UFSet(int s = MAX); //構造函數
UFSet(const UFSet & obj); //拷貝構造函數
~UFSet() //析構函數
{
delete []father;
}
UFSet & operator = (const UFSet & obj); //賦值函數
int FindFather(int pos); //尋找序號pos的族長大人
bool Unite(int posI, int posJ);//將成員posI和posJ合並到同一個家族
int FindFatherAndReducePath(int pos); //查找族長並壓縮路徑
bool UnionBySize (int posI, int posJ); //按大小求並
bool SameFamily(int posI, int posJ); //判斷成員posI和posJ是否屬於同一家族
void PrintUFSet();
private:
int *father; //並查集成員數組,存放各元素的父親結點
int size; //並查集成員數量
};
UFSet::UFSet(int s) : size(s) //構造函數
{
father = new int[size + 1];
for (int i=0; i<=size; i++) //所有的數組元素值均初始化為-1
father[i] = -1;
}
UFSet::UFSet(const UFSet & obj) //拷貝構造函數
{
size = obj.size;
father = new int[size + 1];
for (int i=0; i<=size; i++)
father[i] = obj.father[i];
}
UFSet & UFSet::operator = (const UFSet & obj) //賦值函數
{
if (this == &obj) //檢查自賦值
return *this;
delete []father; //釋放原有的內存資源
size = obj.size; //分配新的內存資源,並復制內容
father = new int[size + 1];
for (int i=0; i<=size; i++)
father[i] = obj.father[i];
return *this; //返回本對象的引用
}
int UFSet::FindFather(int pos)//尋找序號pos的族長大人。若pos本身是族長,則返回 自身
{
if (father[pos] < 0)
return pos;
return FindFather(father[pos]);
}
bool UFSet::Unite(int posI, int posJ)//將成員posI和posJ合並到同一個家族
{
//首先各自去尋找自己的族長
int fI = FindFather(posI);
int fJ = FindFather(posJ);
if (fI == fJ) //如果是同一個族長門下,不必合並,即合並失敗
return false;
else
father[fJ] = fI; //否則fI當族長:誰讓posI站在posJ的前面呢!
return true;
}
int UFSet::FindFatherAndReducePath(int pos)//查找族長並壓縮路徑
{
if (father[pos] < 0)
return pos;
//若自己不是族長,則找到族長後,將所途經的結點均指向族長
return father[pos] = FindFatherAndReducePath(father[pos]);
}
bool UFSet::UnionBySize(int posI, int posJ)//按大小求並
{
//首先各自去尋找自己的族長
int fI = FindFatherAndReducePath(posI);
int fJ = FindFatherAndReducePath(posJ);
if (fI == fJ) //如果是同一個族長門下,不必合並,即合並失敗
return false;
else if (father[fI] < father[fJ])
{//如果族長fI的實力比fJ強,即|fI|>|fJ|,則fI當族長,並修改father[fI]和 father[fJ]
father[fI] += father[fJ];
father[fJ] = fI;
}
else //否則fJ當族長
{
father[fJ] += father[fI];
father[fI] = fJ;
}
return true;
}
bool UFSet::SameFamily(int posI, int posJ)//判斷成員posI和posJ是否屬於同一家族
{
return FindFatherAndReducePath(posI) == FindFatherAndReducePath(posJ);
}
void UFSet::PrintUFSet()//輸出集合的所有元素
{
for (int i=0; i<=size; i++)
cout << father[i] << ' ';
cout << endl;
}
int main()
{
time_t startTime, endTime;
UFSet obj;
int p, q;
time(&startTime);
for (int i=0; i<MAX; i++)
{
p = rand() % MAX + 1;
q = rand() % MAX + 1;
if (p == q)
continue;
//cout << p << "-" << q << " ";
// if (i % 10 == 0)
// cout << endl;
obj.Unite(p, q);
}
while (1)
{
p = rand() % MAX + 1;
q = rand() % MAX + 1;
if (p == q)
continue;
if (obj.SameFamily(p, q))
cout << endl << p << "≡" << q << endl;
else
cout << endl << p << "!≡" << q << endl;
break;
}
time(&endTime);
cout << difftime(endTime, startTime) << endl;
//////////////////////////////////////////////////////////////////////////
time(&startTime);
for (int i=0; i<MAX; i++)
{
p = rand() % MAX + 1;
q = rand() % MAX + 1;
if (p == q)
continue;
//cout << p << "-" << q << " ";
// if (i % 10 == 0)
// cout << endl;
obj.UnionBySize(p, q);
}
while (1)
{
p = rand() % MAX + 1;
q = rand() % MAX + 1;
if (p == q)
continue;
if (obj.SameFamily(p, q))
cout << endl << p << "≡" << q << endl;
else
cout << endl << p << "!≡" << q << endl;
break;
}
time(&endTime);
cout << difftime(endTime, startTime) << endl;
system("pause");
return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved