1. Overview
The combined lookup set (disjoint set or Union-find set) is a tree-type data structure that is often used to handle merging and querying problems of disjoint sets (disjoint Sets).
2. Basic operation
And the collection is a very simple data structure, it mainly involves two basic operations, respectively:
A Merging two disjoint collections
B Determines whether two elements belong to the same set
(1) Merging two disjoint sets (Union (X,y))
The merge operation is simple: set an array father[x] to represent the number of the "Father" of X. The way to merge two disjoint sets, then, is to find one of the collection's father's father (the oldest ancestor), pointing to the father of the oldest ancestor of another set.
The above figure is two disjoint sets, B is the merged Father (b): =father (g)
(2) Determining whether two elements belong to the same set (Find_set (x))
This action can be converted to finding whether the oldest ancestor of two elements is the same. Recursive implementations can be used.
3, optimize
(1) find_set (x), path compression
When looking for ancestors, we generally use recursive lookup, but when a lot of elements or the whole tree into a chain, each find_set (x) is the complexity of O (n). In order to avoid this situation, we need to compress the path, that is, when we find the ancestor node by recursion, the "backtracking" points directly to the ancestors of its descendants, so that the complexity becomes O (1) when Find_set (x) Later, as shown in the following figure. Visible, path compression facilitates the search later.
(2) Union (X,Y), combined by rank
That is, merging less elements into a collection of elements, so that the height of the tree will be relatively small after merging.
4. Programming to achieve 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68-69