Detailed description of path compression code for java programming implementation and query sets, and detailed description of java

Source: Internet
Author: User

Detailed description of path compression code for java programming implementation and query sets, and detailed description of java

First, let's take a look at the two path-compressed images:

Union-find Sets is a very delicate and practical data structure, which is mainly used to deal with the merging of non-intersecting Sets. Some Common uses include the Kruskal Algorithm for Finding connected subgraphs, the Least spanning tree, and the Least Common Ancestors (LCA.

When the dataset is used and queried, a set of dynamic sets S = {S 1, S 2, vertex, S k} that are not intersecting will first exist }, generally, an integer is used to represent an element in the set.
Each set may contain one or more elements, and an element in the set is selected as the representative. The specific elements contained in each set are not concerned, and the specific element selected as the representative is generally not concerned. We are concerned that for a given element, we can quickly find the set where the element is located (the representative) and merge the sets where the two elements are located, the time complexity of these operations is constant.

The basic operations of the query set are as follows:

MakeSet (s): Creates a new query set, which contains s single-element sets.
UnionSet (x, y): merges the set where element x and element y are located. The set where x and y are located does not overlap.
Find (x): find the representative of the set where element x is located. This operation can also be used to determine whether two elements are in the same set. Just compare their representatives.

Package com. dataStructure. union_find; // our fifth Union-Findpublic class UnionFind5 {// rank [I] indicates the number of layers of trees represented by the set with I as the root. // in subsequent code, we do not maintain the semantics of rank, that is, the value of rank is in the path compression process, it may not be the layer value of the tree. // This is why rank is not called height or depth. It is just used as a standard private int [] rank; private int [] parent; // parent [I] indicates the parent node private int count pointed to by element I; // number of data // constructor public UnionFind5 (int count) {rank = new int [count]; parent = new int [count]; thi S. count = count; // initialization. Each parent [I] points to itself, indicating that each element has its own set for (int I = 0; I <count; I ++) {parent [I] = I; rank [I] = 1 ;}/// the complexity of searching for the set number corresponding to element p // O (h, h is the height of the tree private int find (int p) {assert (p> = 0 & p <count); // path compression 1 while (p! = Parent [p]) {parent [p] = parent [parent [p]; p = parent [p];} return p; // path compression 2, recursive Algorithm // if (p! = Parent [p]) // parent [p] = find (parent [p]); // return parent [p];} // check whether element p and element q belong to a set // O (h) Complexity. h is the height of the tree public boolean isConnected (int p, int q) {return find (p) = find (q);} // merge the complexity of the set to which element p and element q belong. // O (h, h is the height of the tree public void unionElements (int p, int q) {int pRoot = find (p); int qRoot = find (q); if (pRoot = qRoot) return; // determine the direction of merging based on the number of elements in the tree where the two elements are located. // merge a set with fewer elements into a set with more elements. if (rank [pRoot] <rank [qRoot ]) {parent [pRoot] = qRoot;} else if (rank [qRoot] <rank [pRoot]) {parent [qRoot] = pRoot ;} else {// rank [pRoot] = rank [qRoot] parent [pRoot] = qRoot; rank [qRoot] + = 1; // at this time, I maintain the rank value }}}

Summary

The above is all the details about the path compression code of java programming implementation and query set, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.