Java programming and detailed explanation of path compression code of search set

First, let's look at two images of path compression:

Union find sets is a very delicate and practical data structure, which is mainly used to deal with the merging of some disjoint sets. Some common uses include finding connected subgraphs, Kruskal algorithm for finding minimum spanning trees, least common ancestors (LCA), etc.

When using a joint query set, there will be a set of disjoint dynamic sets s = {S1, S2,..., s k}. Generally, an integer will be 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 a representative. It doesn't care which elements are included in each set, and which element is selected as the representative. What we care about is that for a given element, we can quickly find the set (representative) where the element is located and merge the set where the two elements are located, and the time complexity of these operations is constant.

There are three basic operations of consolidation query:

Makeset (s): create a new union query set, which contains s single element sets. Unionset (x, y): merge the set of element X and element y. it is required that the set of X and Y does not intersect. If they intersect, they will not be merged. Find (x): find the representative of the set where element X is located. This operation can also be used to judge whether two elements are in the same set. Just compare their respective representatives.

summary

The above is all about the detailed explanation of the path compression code of Java programming and search set in this paper. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>