Non-Intersection Set class algorithm __ data structure

Source: Internet
Author: User
Tags array length

1, the disjoint set class algorithm:

This is an algorithm for dealing with equivalence problems, where equivalence refers to a set, A and B equivalence, which refers to A∈s,b∈s. By finding the two basic operations of a disjoint set class, you can find out if the set flags for A and B are consistent, and if they are consistent, they are a collection. Of course, for the representation of the set, the data structure of the forest is used. The flag of the collection is the root node, which, if the root node, is the same as the collection. In addition to the only root node, the rest of the node is stored in the parent node is the flag. Therefore, we need only one data to represent the structure of a disjoint set. Another operation of a disjoint set class is the union. The intention is to merge two elements that are not part of the same collection to make them equivalent




In the Union (int root1,int Root2) operation, there are three different ways of merging:

The first is in accordance with ROOT1 as the root, root2 as a child node of the way, that is, if Root1 and Root2 merged, it is necessary to root2 the central plains of the collection of symbols, replaced by pointing to ROOT1.

The second is the size of the number of nodes in the set to determine who merges who, if the number of ROOT1 is greater than the number of Root2, then root1 as the root, root2 as a node;

The third is the height of the tree in the set to determine who merges who; if the height of the ROOT1 is greater than the root2 height, then the ROOT1 is the root and the Root2 is the node;

The second to third reason for doing so is that, according to the first method, the result is that the tree becomes too deep and the find operation takes a long time each time. By controlling the number or height, the tree can be effectively slowed down and deeper. Of course, the depth of the tree will still grow. Try to optimize in find operation


Find path compression algorithm:

Each time the find (int x) is found with the node, the X is added below the root node. So, the next time you look for the root of X, in fact, just a search, it means that the number of recursion will be greatly reduced. This is sacrificing the breadth of the tree to reduce the depth of the tree. In fact, this approach works because our disjoint set classes are stored through a one-dimensional array, and the breadth doesn't have to be about the size of the




Import java.util.ArrayList;
	/** * Non-intersection Set class * * @author holy-spirit * */public class Disjset {private int length;

	Private int[] array;  
	 /** * Initialize the array to-1 * * Where the symbol, represents this is the root node, which means that the subscript is the number of the tree node after the sign of the tree, or the height value * If greater than 0, this is the child node of the tree, which contains the parent node subscript * * @param length */public disjset (int length) {if (length <= 0) {System.out.printl
			N ("Incorrect array initialization length");
		Return
		} array = new int[length];
		This.length = Array.Length;

		for (int i = 0; i < Array.Length i++) {Array[i] =-1; }/** * Gets the value of the entire set of subscripts * * @param index * subscript * @return The entire set of the list/public arraylist< integer> getdisjmember (int index) {int count = Array[index] < 0?
		(Array[index] *-1): (Array[find (Index)] *-1);
		arraylist<integer> list = new arraylist<> ();
		List.add (index);
		FindAll (list, index, count);
	return list;   /** * @param list * Need to return the list object * @param parentroot *         Parent node * @param count * Number of nodes in this collection * @return Collection list/private arraylist<integer> FindAll (A
		rraylist<integer> list, int parentroot, int count) {if (count = = 0) {return list;
				for (int i = 0; i < Array.Length i++) {if (array[i] = = parentroot) {list.add (i);
			FindAll (list, I, count--);
	} return list;
	/** * Get this array length * * @return/public int length () {return this.length;

		/** * To determine whether all merges * * @return/public boolean Isunionall () {Boolean isunionall = false; for (int i = 0; i < Array.Length i++) {if (Array[i) < 0) {if (isunionall) {Isunionall =!isunionall
					;
				Break

			} Isunionall =!isunionall;
	} return Isunionall; /** * Determines who is merged each time by comparing the number of two sets, and at the root node the number of nodes in the entire set * * @param root1 * @param root2/public void Unionbyszi

		E (int root1, int root2) {int sumsize = Array[root1] + Array[root2]; if (Array[root1] &Gt
			Array[root2]) {Array[root2] = sumsize;
		ARRAY[ROOT1] = Root2;
			else {array[root1] = sumsize;
		Array[root2] = ROOT1; The/** * is merged through the height of the collection * * The set of small height is called the child node of a highly large tree * * @param root1 * @param root2/public void unionbyhe
		igh (int root1, int root2) {if (ARRAY[ROOT1) < Array[root2]) {ARRAY[ROOT1] = Root2;
			else {if (array[root1] = = Array[root2]) {array[root1]--;

		} Array[root2] = ROOT1; /** * Path Compression lookup method identifies the tree set by the subscript of the root * * path compression thought is, the union of the last more or less will destroy the balance of a tree so every time * find, this find location of the parent node in the subscript value to the root node Subscript so that the height of the tree becomes smaller.  But * * @param x * @return eventually returns the root node of this collection, because the root node holds the size or height of the tree/public int find (int x) {if (Array[x] < 0)
		{return x;
		else {return array[x] = find (array[x]);
 }
	}

}


The idea of a disjoint set class is useful in many places, such as creating a maze ...







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.