Union-find and Disjoint Set Union
来源:互联网 发布:linux syslog debug 编辑:程序博客网 时间:2024/06/03 16:19
Disjoint Set Union (DSU) is a data structure that keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. A union-find algorithm is an algorithm that performs two useful operations on such a data structure:
Find: Determine which subset a particular element is in. This can be used for determining if two elements are in the same subset.
Union: Join two subsets into a single subset.
DSU很适合用来定义连通分量之类的数据结构。在此基础上的union-find算法主要就是两件事,1.判断任意一个点属于哪个集合或者分量,2.将两个集合合并。
所以用来在无向图里面找环的时候特别好用。首先建立好图的点和线之间的关系,然后初始化一个全是独立的点与点之间的parent关系。将每个点的母结点初始化为-1,就是说每一个点都是自己的leading pixel。在之后用每一条边来建立parent关系就是将一个连通分量的各个点之间,通过这种parent的寻找联系起来。
当我们将每一条边对应的关系添加进DSU之前,如果这个边的两个点已经在一个集合里了,这个时候就证明有环路形成了。
c语言实现代码如下:
// A union-find algorithm to detect cycle in a graph#include <stdio.h>#include <stdlib.h>#include <string.h>// a structure to represent an edge in graphstruct Edge{ int src, dest;};// a structure to represent a graphstruct Graph{ // V-> Number of vertices, E-> Number of edges int V, E; // graph is represented as an array of edges struct Edge* edge;};// Creates a graph with V vertices and E edgesstruct Graph* createGraph(int V, int E){ struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph) ); graph->V = V; graph->E = E; graph->edge = (struct Edge*) malloc( graph->E * sizeof( struct Edge ) ); return graph;}// A utility function to find the subset of an element iint find(int parent[], int i){ if (parent[i] == -1) return i; return find(parent, parent[i]);}// A utility function to do union of two subsets void Union(int parent[], int x, int y){ int xset = find(parent, x); int yset = find(parent, y); parent[xset] = yset;}// The main function to check whether a given graph contains // cycle or notint isCycle( struct Graph* graph ){ // Allocate memory for creating V subsets int *parent = (int*) malloc( graph->V * sizeof(int) ); // Initialize all subsets as single element sets memset(parent, -1, sizeof(int) * graph->V); // Iterate through all edges of graph, find subset of both // vertices of every edge, if both subsets are same, then // there is cycle in graph. for(int i = 0; i < graph->E; ++i) { int x = find(parent, graph->edge[i].src); int y = find(parent, graph->edge[i].dest); if (x == y) return 1; Union(parent, x, y); } return 0;}// Driver program to test above functionsint main(){ /* Let us create following graph 0 | \ | \ 1-----2 */ int V = 3, E = 3; struct Graph* graph = createGraph(V, E); // add edge 0-1 graph->edge[0].src = 0; graph->edge[0].dest = 1; // add edge 1-2 graph->edge[1].src = 1; graph->edge[1].dest = 2; // add edge 0-2 graph->edge[2].src = 0; graph->edge[2].dest = 2; if (isCycle(graph)) printf( "graph contains cycle" ); else printf( "graph doesn't contain cycle" ); return 0;}
- Union-find and Disjoint Set Union
- Disjoint-Set Data Structure (Union Find Algorithm)
- LeetCode基础--Union Find(Disjoint Set)
- disjoint set (union-find set) (并查集)
- Data Structures (Weiss) Chapter 8: Union and Find, Disjoint Sets, union by sizes, C++
- 并查集(Union-Find Set)
- 并查集 Union-Find-Set
- 并查集(Union-Find Set)小结
- Union Find
- Union-Find
- Union-find
- Union-Find
- union find
- union-find
- UNION and UNION All
- union and union all
- 2.quick find and quick union
- 目标融合之 ——并查集(Disjoint Sets)||(Union Find Sets)
- EasyDemo*接口回调
- 准备一个dfs和bfs的模板吧
- 动态规划之编辑距离
- 关于return的使用方法
- [19]_总结一下find_grep_file_which\whereis_gzip\tar等常用命令的用法
- Union-find and Disjoint Set Union
- 初步讲VIM打造成C++ IDE
- java基础学习——反射机制与动态代理
- EasyDemo*UI布局常见控件(Warning)
- 【机器学习】作业6-EM算法
- 字符流编码问题之写操作
- python之 列表常用方法
- ROS打开USB摄像头
- websocket的seesion的getOpenSessions