[Of algorithm series] parallel query set (non-Intersection Set), algorithm series 2

Source: Internet
Author: User

[Of algorithm series] parallel query set (non-Intersection Set), algorithm series 2

I. Overview

The Disjoint set or Union-find set is a tree-type data structure. It is often used to process the merge and query of some Disjoint Sets.

There is a union-find algorithm that defines two operations for this data structure:

Find: determines the subset of an element. It can be used to determine whether two elements belong to the same subset. Union: combines two subsets into the same set.

Because it supports these two types of operations, a non-intersection is also known as union-find data structure or merge-find set ). Other important methods, MakeSet, are used to create a single element set. With these methods, many classic division problems can be solved.

To define these methods more accurately, you need to define how to represent the set. A common strategy is to select a fixed element for each set, which is called a representative to represent the entire set. Next. Find (x) returns the representative of the set to which x belongs, while Union (x, y) uses the representative of x and y as the parameter.

Ii. Main Operations

1.MakeSet(x)2.Find(x)3.Union(x,y)

2.1 MakeSet (x) create a new set

Create a new set, and its unique member (because it represents) is x. Because the set is not intersecting, it is required that x does not appear in other sets.

2.2 Find (x) contains the representatives of x Sets

Returns a pointer pointing to the representative of a (unique) set containing x.

2.3 Union (x, y) merge two non-intersecting sets

Merges dynamic sets that contain x and y into a new set. The resulting set can be any member of two sets. However, in many cases, we generally choose one of the two sets as the new representative.

Three non-intersecting sets forest (with a root tree representing a set)

Non-intersecting sets can be implemented using linked lists, but there is also a faster method-a root tree represents a set, each node in the tree contains a member of the Set, each tree represents a set. For example:

The tree on the left represents the set {B, c, e, h} and its c Represents. The tree on the right represents the set {d, f, g} and Its f represents.

3.1 MakeSet (x)

MakeSet creates a tree that contains only one node. At the beginning, the parent node is itself.

# Define N 100 // apply for memory size int parent [N]; // parent [x] indicates the parent node void MakeSet (int x) of x) {parent [x] = x ;}

3.2 Find (x)

Find (x) indicates the (unique) set containing x. Keep searching along the parent node pointer until the root is found.

Int Find (int x) {// The root node, that is, the set represents if (x = parent [x]) {return x ;} // if // search for Find (parent [x]) along the parent node pointer;}

3.3 Union (x, y)

The Union operation points the root of a tree to the root of another tree. For example:

// Merge void Union (int x, int y) {x = Find (x); y = Find (y); parent [y] = x ;}

4. Optimization

4.1 merge by rank

The idea is to direct a tree with fewer nodes to the root of a tree with more nodes. Instead of showing the size of the subtree with each node as the root, we use a method that simplifies the analysis. For each node, we use rank to represent an upper bound of the node height (from this node to the number of the highest paths on a leaf node of a certain descendant. In rank-based merge, the root with a smaller rank points to the root with a larger rank in the Union operation.

Rank [x] indicates the rank of the x node. When a set is created by MakeSet, the initial rank of the unique node in the corresponding tree is 0, and each Find operation does not change any rank.

// Parent [x] indicates the parent node rank [x] of x indicates the rank void MakeSet (int x) of x {parent [x] = x; rank [x] = 0 ;}

When the Union clause is applied to the two integers, there are two cases:
(1) When the two ranks are not equal, we call the root with a higher rank the parent node with a smaller rank, but the rank itself remains unchanged.
(2) When the two rank values are equal, select one root node as the parent node and increase its rank value.

void Union(int x, int y){    x = Find(x);    y = Find(y);    if(x == y) {        return;    }//if    if(rank[x] > rank[y]){        parent[y] = x;    }//if    else if(rank[x] < rank[y]){        parent[x] = y;    }//else    else{        rank[x]++;    }//else}

4.2 path compression

When looking for an ancestor, we generally use recursive search. However, when many elements or the entire tree is changed to a chain, Every time finding (x) is the complexity of O (n. To avoid this situation, we need to compress the path, that is, when we find the ancestor node after "recursion" and "backtrack", we direct all its child nodes to the ancestor by the way, in this way, the complexity of finding (x) is changed to O (1), as shown in. It can be seen that path compression facilitates future searches.

The triangle represents the child tree, and its root is the node shown in.

// Findint Find (int x) with path compression {// The root node is the set representing if (x! = Parent [x]) {// update node x to point it to the root parent [x] = Find (parent [x]);} // if return parent [x];}

Find is a two-way method: one is to rise along the search path until the root is found; the other is to drop along the search path, and each node is updated to point to the root node.

Complexity Analysis

The spatial complexity is O (N), the time complexity of building a set is O (1), and the time complexity of N merge M searches is O (M Alpha (N )), here Alpha is an inverse function of the Ackerman function, which is within a very large range (we have observed an estimated cosmic range of 10 to the power of 80 atoms, which is smaller than the previously mentioned range) the value of this function can be regarded as not greater than 4, so the operation of querying sets can be regarded as a linear relationship with m.

APPLICATION 6

The query set is often used as another complex data structure or algorithm storage structure. Common applications include: finding the number of connected components in an undirected graph, recent common ancestor (LCA), sorting jobs with restrictions, and achieving the Kruskar algorithm to find the Minimum Spanning Tree.

Seven references

Query set
Parallel query set of Data Structure
Introduction to Algorithms

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.