问题描写叙述:

计算机科学中,并查集是一种树型的数据结构,其保持着用于处理一些不相交集合(Disjoint
Sets)的合并及查询问题。有一个联合-查找算法union-find algorithm)定义了两个操作用于此数据结构:

Find:确定元素属于哪一个子集。它能够被用来确定两个元素是否属于同一子集;

Union:将两个子集合并成同一个集合;

实现并查集的关键是实现union-find algorithm, 本文依据经常使用的四种算法,实现了这个类,详细算法实现请參看维基百科;

制造測试数据集,測试几种方法之间性能的指标;

程序代码:



#ifndef _DISJOINT_SET_H_
#define _DISJOINT_SET_H_ #include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <math.h> #include "windows.h" enum DISJOINTWAY
{
COMMON_WAY,
COMPREE_WAY,
WEIGHT_WAY,
WEIGHT_COMPRESS_WAY
}; /*
* encapsulate the class of disjoint set
*
*/ #define MAXDISJOINTSET 0xffffff
class DisjointSet
{
public:
DisjointSet( int maxSize = MAXDISJOINTSET ):m_item(0), m_size(maxSize)
{
m_item = new int[maxSize];
for( int i = 0; i < m_size; i++ )
{
m_item[i] = i;
} m_path = new int[maxSize];
memset( m_path, 1, sizeof(int)*maxSize );
} ~DisjointSet()
{
Clear();
} /*
* find interface
*
*/
int Find( DISJOINTWAY way, int input )
{
assert( input < m_size );
switch( way )
{
case COMMON_WAY:
return ImplFindFirst( input );
case COMPREE_WAY:
return ImplFindSecond( input );
case WEIGHT_WAY:
return ImplFindWeight( input );
case WEIGHT_COMPRESS_WAY:
return ImplFindWeightCompree( input );
default:
return -1;
}
} /*
* make union
*
*/
void Union( DISJOINTWAY way, int first, int second )
{
assert( first < m_size && second < m_size );
switch( way )
{
case COMMON_WAY:
ImplUnionFirst( first, second );
break;
case COMPREE_WAY:
ImplUnionSecond( first, second );
break;
case WEIGHT_WAY:
ImplUnionWeighted( first, second );
break;
case WEIGHT_COMPRESS_WAY:
ImplUnionCompree( first, second );
break;
default:
break;
} } /*
*
*
*/
void Clear()
{
delete [] m_item;
m_item = 0; delete [] m_path;
m_path = 0; m_size = 0;
} protected: int ImplFindFirst( int input )
{
assert( input < m_size );
return m_item[input];
} int ImplFindSecond( int input )
{
int i = input;
for( ; i != m_item[i]; i = m_item[i] ); return i;
} int ImplFindWeight( int input )
{
int i = input;
for( ; i != m_item[i]; i = m_item[i] ); return i; } int ImplFindWeightCompree( int input )
{
int i = input;
for( ; i != m_item[i]; i = m_item[i] )
m_item[i] = m_item[m_item[i]]; return i;
} /*
*
*
*/
void ImplUnionFirst( int first, int second )
{
int x = m_item[first];
int y = m_item[second]; if( x != y )
{
m_item[first] = y;
} for( int i = 0; i < m_size; i++ )
{
if( x == m_item[i] )
m_item[i] = y;
}
} /*
*
*
*/
void ImplUnionSecond( int& first, int& second )
{
if( first != second )
{
m_item[first] = second;
}
} /*
*
*
*/
void ImplUnionWeighted( int first, int second )
{
if( first != second )
{
if( m_path[first] < m_path[second] )
{
m_item[first] = second;
m_path[second] += m_path[first];
}
else
{
m_item[second] = first;
m_path[first] += m_path[second];
}
}
} /*
*
*
*/
void ImplUnionCompree( int first, int second )
{
if( first != second )
{
if( m_path[first] < m_path[second] )
{
m_item[first] = second;
m_path[second] += m_path[first];
}
else
{
m_item[second] = first;
m_path[first] += m_path[second];
}
} } protected: int* m_item;
int m_size; int* m_path; }; void TestDisjointSetSimple()
{
DisjointSet djoint;
int i = djoint.Find( COMMON_WAY, 1 );
int j = djoint.Find( COMMON_WAY, 3 );
if( i != j )
djoint.Union( COMMON_WAY, 1, 3 ); i = djoint.Find( COMMON_WAY, 2 );
j = djoint.Find( COMMON_WAY, 5 );
if( i != j )
djoint.Union( COMMON_WAY, i, j ); i = djoint.Find( COMMON_WAY, 2 );
j = djoint.Find( COMMON_WAY, 6 );
if( i != j )
djoint.Union( COMMON_WAY, i, j ); i = djoint.Find( COMMON_WAY, 6 );
j = djoint.Find( COMMON_WAY, 7 );
if( i != j )
djoint.Union( COMMON_WAY, i, j ); assert( djoint.Find( COMMON_WAY, 2 ) == djoint.Find( COMMON_WAY, 7 ) ); i = djoint.Find( COMMON_WAY, 1 );
j = djoint.Find( COMMON_WAY, 7 );
if( i != j )
djoint.Union( COMMON_WAY, i, j ); assert( djoint.Find( COMMON_WAY, 3 ) == djoint.Find( COMMON_WAY, 7 ) );
} void TestDisjointSetComplex( DISJOINTWAY way, const char* str )
{ unsigned long start = GetTickCount();
DisjointSet djoint; const int len = 1000000;
const int base = 60000;
int halfLen = len / 2;
srand( time(NULL) );
for( int i = 0; i < len; i++ )
{
int first = rand() % base;
int second = rand() % base;
if( i > halfLen )
{
first += base;
second += base;
} if( first != second )
{
first = djoint.Find( way, first );
second = djoint.Find( way, second );
if( first != second )
djoint.Union( way, first, second ); assert( djoint.Find( way, first ) == djoint.Find( way, second ) );
}
} unsigned long interval = GetTickCount() - start;
printf(" %s way consume time is %d \n", str, interval ); } void TestSuiteDisjointSet()
{
TestDisjointSetSimple(); const char* str[] = {"common", "compress", "weight", "weight compress"};
for( int i = WEIGHT_COMPRESS_WAY; i >= 0; i--)
{
TestDisjointSetComplex((DISJOINTWAY)i, str[i] );
} } #endif

compile and run in visual studio 2005

以下图片是几种方法执行时间之比較,最直白方法的时间到如今还没输出,所以就没有显示:

并查集类的c++封装,比較union_find algorithm四种实现方法之间的性能区别的更多相关文章

  1. 百度地图和高德地图坐标系的互相转换 四种Sandcastle方法生成c#.net帮助类帮助文档 文档API生成神器SandCastle使用心得 ASP.NET Core

    百度地图和高德地图坐标系的互相转换   GPS.谷歌.百度.高德坐标相互转换 一.在进行地图开发过程中,我们一般能接触到以下三种类型的地图坐标系: 1.WGS-84原始坐标系,一般用国际GPS纪录仪记 ...

  2. get,post,put,delete四种基础方法对应增删改查

    PUT,DELETE,POST,GET四种基础方法对应增删改查 1.GET请求会向数据库发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改.增加数 ...

  3. 四种Sandcastle方法生成c#.net帮助类帮助文档

    方法一 前端时间在网上收集和自己平时工作总结整理了<干货,比较全面的c#.net公共帮助类>,整理完成上传github之后我又想,既然是帮助类,总得有个帮助文档于是乎想到了Sandcast ...

  4. 类与接口(二)java的四种内部类详解

    引言 内部类,嵌套在另一个类的里面,所以也称为 嵌套类; 内部类分为以下四种: 静态内部类 成员内部类 局部内部类 匿名内部类 一.静态内部类 静态内部类: 一般也称"静态嵌套类" ...

  5. RBAC类在ThinkPHP中的四种使用方法

    第一类:放在登陆控制器的登陆操作中 1.RBAC::authenticate(); 用于在用户表中查找表单提交的用户名的数据,实质上就是一条用户表查寻语句,=====> return M(mod ...

  6. 关于android studio中使用class.forname()方法动态获取类实例报NO CLASS FOUND异常的几种处理方法

    最近在做一个项目的时候需要用到反射来回调子类的方法,但是在反射过程中总是在class.forname()方法抛出NO CLASS FOUND异常,经过几部检查,问题解决,在此总结一下引起该问题的原因 ...

  7. Hashtable类中的四种遍历方法对比

    要遍历一个Hashtable,api中提供了如下几个方法可供我们遍历: keys() - returns an Enumeration of the keys of this Hashtable ke ...

  8. poj1456(贪心+并查集)

    题目链接: http://poj.org/problem?id=1456 题意: 有n个商品, 已知每个商品的价格和销售截止日期, 每销售一件商品需要花费一天, 即一天只能销售一件商品, 问最多能买多 ...

  9. poj 2236 并查集

    并查集水题 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring& ...

随机推荐

  1. The only legal comparisons are between two numbers, two strings, or two dates.

    The only legal comparisons are between two numbers, two strings, or two dates. Left  hand operand is ...

  2. 软件测试Web数据分析工具HttpWatch安装

    1.下载破解版:http://www.pc6.com/softview/SoftView_13845.html 2.安装时引入授权文件并汉化: 3.打开IE9,菜单:管理加载项->工具栏和扩展, ...

  3. shell脚本编写笔记

    包含转载内容,转载自http://wenku.baidu.com/link?url=jtCHxEYzgGve6P64U3JRQRgU6nhpGvqFLLpWu9I2Htq6hi9TGLudRFkk7r ...

  4. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.4.6

    Let $A$ and $B$ be two matrices (not necessarily of the same size). Relative to the lexicographicall ...

  5. ChineseCounter.cs 统计中文文本中常用字占比

    http://www.tuicool.com/articles/qmMba2 1 using System; using System.IO; using System.Collections.Gen ...

  6. FOJ 1608 Huge Mission 线段树

    每个节点维护一个最小值,更新发现如果大于最小值,直接向下更新.速度还可以.. #include<cstdio> #include<algorithm> #include< ...

  7. eclipse 远程wifi调试android程序

    [原文]http://leanote.com/blog/view/541f8b2dbda4e44f75000000 下载wifidebug http://pan.baidu.com/s/1i3stnF ...

  8. iOS开发UI篇—实现一个私人通讯录小应用【转】

    转一篇学习segue不错的教程 一.该部分主要完成内容 1.界面搭建                        2.功能说明 (1).只有当账号和密码输入框都有值的时候,登录按钮才能交互 (2). ...

  9. Ubuntu 12.04 安装Scrapy爬虫框架

    转自:http://www.cnblogs.com/HelloPython/ 亲测有效 根据Scrapy安装指南(http://doc.scrapy.org/en/latest/intro/insta ...

  10. 《GettingThingsDone》--GTD学习笔记(一)-GTD理论

    利用春节假期阅读了<Getting Things Done>一书,下文整理了下阅读过程中做的读书笔记和心得. ==GTD理论== 一. 目的: 1. 收集需要处理的事情把它置于一个脱离大脑 ...