【0】README

0.1)本文总结于 数据结构与算法分析, 源代码均为原创, 旨在实现 对不相交集合的路径压缩操作;
0.2)对求并后的集合进行路径压缩,目的是降低集合(合并树)的深度,减少find 操作的时间复杂度;
0.3) for introduction to non-intersect set ADT ,please refet to http://blog.csdn.net/PacosonSWJTU/article/details/49716905 , and for details of finding or unionSet operations towards non-intersect set , please refer to http://blog.csdn.net/pacosonswjtu/article/details/49717009


【1】 路径压缩相关

1.1)基于这样的观察:执行 Union操作 的任何算法都将产生相同的最坏情形的树,因为它必然会随意打破树间的平衡。因此,无需对整个数据结构重新加工而使算法加速的唯一方法是: 对Find 操作做些更聪明的工作;
1.2)路径压缩定义:设操作是Find(X), 此时路径压缩的效果是, 从X到根的路径上的每一个节点都使它的父节点变成根;执行find(15)后压缩路径的效果为:

对路径压缩算法的分析(Analysis)

  • A1)路径压缩的实施在于 使用额外的两次指针移动, 节点13和14 现在离根近了一个位置, 而节点15和16离根近了两个位置;
  • A2)因此, 对这些节点未来的快速访问将由于花费 额外的工作来进行路径压缩而得到补偿;

1.3)路径压缩对 基本的 Find操作改变不大。对 Find 操作来说,唯一的变化是 使得 S[X] 等于 由Find 返回的值;这样,在集合的根被递归地找到以后, X 就直接指向了它, 对通向根的路径上的每一个节点这将递归地出现,因此实现了路径压缩;
1.4)路径压缩可以和 大小求并完全兼容,这就使得两个例程可以同时实现;
1.5)路径压缩不完全与 按高度求并兼容,因为路径压缩可以改变树的高度;


【2】source code + printing results

2.1)download source code:
https://github.com/pacosonTang/dataStructure-algorithmAnalysis/blob/master/chapter8/p206_pathCompression.c

  • souce code statements:路径压缩源代码中使用的集合求并方法是: 按大小求并:

2.2)source code at a glance:

#include <stdio.h>
#include <malloc.h>#define ElementType int
#define Error(str) printf("\n error: %s \n",str) struct UnionSet;
typedef struct UnionSet* UnionSet;// we adopt the child-sibling expr
struct UnionSet
{int parent;int size;ElementType value;
};UnionSet makeEmpty();
UnionSet* initUnionSet(int size, ElementType* data);
void printSet(UnionSet* set, int size);
void printArray(ElementType data[], int size);
int find(int index, UnionSet* set);
void pathCompress(int, UnionSet*);// initialize the union set
UnionSet* initUnionSet(int size, ElementType* data)
{UnionSet* set;  int i;set = (UnionSet*)malloc(size * sizeof(UnionSet));if(!set){Error("out of space, from func initUnionSet");        return NULL;}   for(i=0; i<size; i++){set[i] = makeEmpty();if(!set[i])return NULL;set[i]->value = data[i];}return set;
}// allocate the memory for the single UnionSet and evaluate the parent and size -1
UnionSet makeEmpty()
{UnionSet temp;temp = (UnionSet)malloc(sizeof(struct UnionSet));if(!temp){Error("out of space, from func makeEmpty!");        return NULL;}temp->parent = -1;temp->size = 1;return temp;
}// merge set1 and set2 by size
void setUnion(UnionSet* set, int index1, int index2)
{//judge whether the index1 or index2 equals to -1 ,also -1 represents the rootif(index1 != -1)index1 = find(index1, set);if(index2 != -1)index2 = find(index2, set);if(set[index1]->size > set[index2]->size){set[index2]->parent = index1;set[index1]->size += set[index2]->size;}else{set[index1]->parent = index2;set[index2]->size += set[index1]->size;}
} //find the root of one set whose value equals to given value
int find(int index, UnionSet* set)
{UnionSet temp;  while(1){temp = set[index];if(temp->parent == -1)break;index = temp->parent;}return index;
}   // conducting path compression towards union set with given index
void pathCompress(int index, UnionSet* set)
{   int root;int i;int parent;//1st step: find the top root contains the element under index  root = find(index, set);//2nd step: path compression beginsi = set[index]->parent;set[index]->parent = root;while(i != -1) {       parent = set[i]->parent;                if(parent == root)break;set[i]->parent = root;i = parent;}
}int main()
{int size;UnionSet* unionSet;ElementType data[] = {110, 245, 895, 658, 321, 852, 147, 458, 469, 159, 347, 28};size = 12;printf("\n\t====== test for conducting path compression towards union set by size ======\n");//printf("\n\t=== the initial array is as follows ===\n");//printArray(data, depth); printf("\n\t=== the init union set are as follows ===\n");unionSet = initUnionSet(size, data); // initialize the union set over//printSet(unionSet, size);printf("\n\t=== after union(0, 1) + union(2, 3) + union(4, 5) + union(6, 7) + union(8, 9) + union(10 ,11) ===\n");setUnion(unionSet, 0, 1);setUnion(unionSet, 2, 3);setUnion(unionSet, 4, 5);setUnion(unionSet, 6, 7);setUnion(unionSet, 8, 9);   setUnion(unionSet, 10, 11); //printSet(unionSet, size);printf("\n\t=== after union(1, 3) + union(5, 7) + union(9, 11) ===\n");setUnion(unionSet, 1, 3);setUnion(unionSet, 5, 7);setUnion(unionSet, 9, 11);//printSet(unionSet, size);  printf("\n\t=== after union(3, 7) + union(7, 11) ===\n");setUnion(unionSet, 3, 7);setUnion(unionSet, 7, 11);  printSet(unionSet, size); printf("\n\t=== after pathCompress(0, unionSet) + pathCompress(8, unionSet) ===\n");pathCompress(0, unionSet) ;pathCompress(8, unionSet);printSet(unionSet, size); return 0;
}void printArray(ElementType data[], int size)
{int i;for(i = 0; i < size; i++)    printf("\n\t data[%d] = %d", i, data[i]);                    printf("\n\n");
} void printSet(UnionSet* set, int size)
{int i;UnionSet temp;for(i = 0; i < size; i++){       temp = set[i];printf("\n\t parent[%d] = %d", i, temp->parent);                }printf("\n");
}

2.3)printing results:

不相交集合求并的路径压缩相关推荐

  1. 【数据结构笔记19】File Transfer的C语言实现,集合的简化表示,按秩归并,路径压缩

    本次笔记内容: File Transfer - C语言实现(4小节共42:43) 文章目录 集合的简化表示 题意理解与实现 题目与样例 程序框架 简单实现Find与Uion函数 按秩归并改进Union ...

  2. 22中超联赛day8 1007(hdu7226) Darnassus 并查集(按秩合并+路径压缩)+ 链式前向星桶排 + Kruskal求最小生成树

    Darnassus 题目描述 Even the World Tree must bow to the cycle of life. Everything born will die. Archimon ...

  3. 算法导论之用于不相交集合的数据结构

    不相交集合,即集合内元素无交集.在一些具体应用中,需将n个不同的元素分成一组不相交的集合.不相交集合的两个重要操作,找出给定元素所属的集合和合并两个集合.为支持不相交集合的操作,需要设计和维护数据结构 ...

  4. 数据结构与算法——并查集(不相交集合)

    文章目录 认识并查集 并查集解析 基本思想 如何查看a,b是否在一个集合? a,b合并,究竟是a的祖先合并在b的祖先上,还是b的祖先合并在a上? 其他路径压缩? 代码实现 结语 认识并查集 对于并查集 ...

  5. vb6实现union数据结构_数据结构与算法——并查集(不相交集合)

    首发公众号:bigsai 认识并查集 对于并查集(不相交集合),很多人会感到很陌生,没听过或者不是特别了解.实际上并查集是一种挺高效的数据结构.实现简单,只是所有元素统一遵从一个规律所以让办事情的效率 ...

  6. 并查集路径压缩_并查集(UnionFind)技巧总结

    什么是并查集 在计算机科学中,并查集是一种树型的数据结构,用于处理一些不交集(Disjoint Sets)的合并及查询问题.有一个联合-查找算法(Union-find Algorithm)定义了两个用 ...

  7. 算法导论-用于不相交集合的数据结构

    21.2-4 对于图21-3中操作序列的运行时间,给出其紧确的渐近界.假定采用的是链表表示和加权合并启发式策略. 解:make-set,O(n):加权合并启发,每次将较短链表链接到较长链表,即每次将长 ...

  8. 并查集路径压缩_第二十五天:并查集

    今天是释然发题解的第二十五天,以后会经常和大家分享学习路上的心得,希望和大家一起进步,一起享受coding的乐趣 本文约1400字,预计阅读5分钟 昨天我们学习了动态规划之线性规划,忘记的小伙伴们可以 ...

  9. 迪杰斯特拉--- 模板(求最短路径/输出路径/所有路径都可以走的做法)

    迪杰斯特拉--- 模板(求最短路径/输出路径/所有路径都可以走的做法) 1.0版 #include <iostream> using namespace std;const int max ...

最新文章

  1. micropython mqtt_MicroPython使用MQTT协议接入OneNET云平台
  2. 写了个牛逼的日志切面,甩锅更方便了!
  3. html中如何等比缩小图片,css如何将图片等比缩放
  4. 剑指OFFER之跳台阶(九度OJ1388)
  5. 网页服务器和mysql服务器_实现Web服务器之间使用同一个MYSQL和相同的网页配置文件的方法...
  6. 演练 打印直角三角形
  7. 适合0基础的web开发系列教程-canvas
  8. Sqlite中文排序
  9. LeetCode 链表相关题目总结
  10. CentOS TinyProxy http(s)上网代理及置代理上网的方法
  11. 《大数据技术原理与应用》林子雨 期末复习重点(总结)
  12. raize控件的安装注意
  13. linux 8t的硬盘格式化,linux 新添加的硬盘格式化并挂载到目录下
  14. im即时通讯开发:万人群聊技术方案实践
  15. Linux 系统Error starting userland proxy: listen tcp4 0.0.0.0:xx端口: bind: address already in use的端口占用问题
  16. 怎么离线发布全国谷歌高清卫星影像地图瓦片
  17. 1378:最短路径(shopth)——Floyd
  18. 腾讯3轮面试都问了Android事件分发,最强技术实现
  19. 索引是什么,如何实现?
  20. 高等数学强化3:一元函数积分学 P积分

热门文章

  1. Display Substring
  2. 2020牛客暑期多校训练营(第六场)
  3. YbtOJ#943-平方约数【莫比乌斯反演,平衡规划】
  4. 51nod1220-约数之和【莫比乌斯反演,杜教筛】
  5. jzoj3846-七天使的通讯【二分图判定】
  6. Ch5302-金字塔【区间dp】
  7. P1801-黑匣子_NOI导刊2010提高【堆】
  8. 【jzoj】2018.2.3NOIP普及组——D组模拟赛
  9. 2021牛客暑期多校训练营1 G-Game of Swapping Numbers(最优解转化+贪心)
  10. html数据复制到剪切板