Query sets and their linked list and non-Intersection Set forests

Source: Internet
Author: User

When you need to determine whether two objects belong to the same set of applications, and query the set is a very effective data structure. The query set has three basic operations: Make-set, find-set, and union. Defines an operation sequence with M make-set, find-set, and Union (n make-set operations. The number of Union operations in this operation sequence is no more than N-1 (because each union operation reduces the number of sets by 1 ).

Using a linked list is a very convenient way to implement and query sets. Each set has a head pointer and a tail pointer. Each object has a pointer field pointing to the element, which means that the element can be selected at will, if a set has a certain nature, you can also sort the elements in the set according to certain rules (such as sorting ).

If the linked list is used, the time for the make-set operation is O (1), the time for the find-set operation is O (1), and the two sets are merged, the tail pointer can be used to quickly locate the desired link. It takes only constant time to change the header pointer and tail pointer, and the time of each union operation is O (n). n-1 total time is O (n ^ 2 ).

If weighted merge is used for Union, after a long set is merged to a short set, the total union time is O (nlgn ), because the first change of each object indicates that the set length of the element pointer is not less than 2, and the second change indicates that the set length of the element pointer is not less than 2 ^ 2, the number of lgn changes indicates that the set length of the element pointer is not less than N, and the maximum length of the set is N. Therefore, the maximum number of changes indicates that the number of element pointers is lgn. There are n objects in total, so the union operation time is O (nlgn ).

It is the most efficient method to implement and query sets in a non-intersecting set forest. The time required for M make-set, find-set, and Union (n make-Set Operations) operation sequences is (N * alpha (n )), alpha (n) <= 4. the following two heuristic algorithms are used when you do not want to subscribe to a collection forest:

1) Merge by rank. The rank is defined as the maximum height from the root to the leaf, and the set with a small rank is merged into the set with a large rank.

2) path compression. The find-set operation is a two-way method. The first operation is to locate the root node from the bottom up, and the second operation is to direct the pointer of each object in the search path to the root node.

The pseudocode for implementing and querying a set in a non-Intersection Set forest is as follows:

Make-set (x) <br/> 1 p (x) <-x <br/> 2 rank [x] <-0 </P> <p> Union (x, y) <br/> 1 Link (find-set (x), find-set (y) </P> <p> Link (x, y) <br/> 1 if rank [x]> rank [y] <br/> 2 then P [y] <-x <br/> 3 if rank [x] = rank [y] <br/> 4 then rank [y] <-rank [y] + 1 </P> <p> Find-set (X) <br/> 1 If X! = P [x] <br/> 2 then P [x] <-find-set (P [x]) <br/> 3 return x </P> <p>

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.