Professional Documents
Culture Documents
Pi19404
February 17, 2013
Contents
Contents
Union Find Algorithm Overview
0.1 Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0.2 Equivalence Relation . . . . . . . . . . . . . . . . . . . . . . 0.2.1 Formal Definition . . . . . . . . . . . . . . . . . . . 0.2.2 Equivalence Relation and Disjoint Subsets . . . 0.2.3 Equivalence Class . . . . . . . . . . . . . . . . . . . . 0.3 Union Find Algorithm . . . . . . . . . . . . . . . . . . . . . 0.3.1 Quick Find . . . . . . . . . . . . . . . . . . . . . . . . 0.3.2 Quick Union . . . . . . . . . . . . . . . . . . . . . . . 0.3.3 Weighted Quick Unions . . . . . . . . . . . . . . . . 0.3.4 Weighted Quick Unions with path compression 0.4 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
3 3 4 4 5 5 6 7 8 9 9 9
2|9
3|9
P R; Va P A P R; then
(b; a)
(a; b)
PR
(a; c)
PR
Let A be a non empty set .A Relation R on A (subset of R of A x A) is called a equivalence relation on A is the following hold for all a; b; c P A. 1. Reflexive : 2. Symmetric :
a
a
a
b P R; then
b
a
a
3. Transitive :a b and
c then
c
A equivalence relation is the one which satisfies reflexive ,symmetric and transitive relations.
4|9
Union Find Algorithm Overview A partition of X is a set P of nonempty subsets of X, such that every element of X is an element of a single element of P. The elements of P are pairwise disjoint and their Union is X.
[a]
,[a]
:=
fx P
The equivalence class form a partition of set X. X is the union of equivalence of equivalence classes and distinct equivalence classes are disjoint. if X is a union of non empty mutually disjoint subsets ,then there is a equivalence relation of X such that each subset defines a equivalence class.
5|9
Union Find Algorithm Overview to checks if two elements of the graph satisfy the equivalence relation,belong to same equivalence class,are connected. To construct a connected grap we require to join two connected components.This represents union of two equivalent classes. A element is added to the two disjoint equivalent classes so that they are combined to single equivalent class. Thus we require a basic union find data structure to support the following operations :find(A) - find what equivalence class element A belongs union(A,B) - merger equivalence class of elements A and B. These basic operations can be used to build more complex operations.
6|9
Union Find Algorithm Overview alence class changes the index of all elements of equivalence class a and b to same index. The union operations is require to construct the graph defining the problem. We will use the following convention merge(a,b) we will assign the index of equivalence class of b to a. Thus for all the elements of equivlance class a index are changed to index of b. we will consider the performace cost for above algorithm.The cost is in terms of memory access. For initialization and union the cost is N while for find operation it is 1. If we have to perform M union operations it will lead to MN memory accesses. Thus M=N it takes quadratic time.Thus quick find proves to be very expensive for larger number of unions.
7|9
Union Find Algorithm Overview of root of a. Again the performance measurement is number of memory accesses. initialization is N memory accesses. To find the root worst case scenario is N. The find operation worst case scenario is 2N. The union operation worst case is 2N. As tree gets taller the find operation gets expensive However advantage is in terms of union operation. However this still is too slow an algorithm for large problems.
At each merge operation depth of tree increases by 1. Consider tree T1 containing x and another tree T2. Let jT 2j jT 1j .If x is merged with element of T2 depth of element x from root increases by 1. The size of tree containing atleast increases by a factor of 2. In the worst case scenario we get size of trees as
20 ; 21 ; 22 ; 23 ; : : : ; 2k
8|9
Union Find Algorithm Overview .Thus size of tree can double atmost k times ,log2 (N ). Thus depth of any node is k or log2 (N ).
initialization takes proportial time N. The union and find both take constant times log2 (N ) time. Thus the algorithm is scalable as N increases.
0.4 Code
Code for quick find and quick union is available in repository https: //github.com/pi19404/m19404/tree/master/Algorithm/UnionFind/
9|9