and check Set (Java implementation)

Source: Internet
Author: User
Tags int size

And check set: Using array to realize the idea

The number stored in the array represents the collection to which it belongs. For example, Arr[4]==1; Representative 4 is the first group. If Arr[7]==1, the Representative 7 is also the first group. Since arr[4] = = arr[7] = = 1, then the description 4 and 7 belong to a set,

Code
/** * Array Implementation and check set, the element inside the number represents the collection number */public class Unionfind {/** * array, representing and checking all elements * * private int[] ID;    /** * and check the number of elements of the set */private int size; /** * Constructs a new and checked set * * @param size Initial Size */public unionfind (int size) {//initialization number this.size =        Size        Initializes the array, each of which points to its own id = new Int[size];        for (int i = 0; i < size; i++) {id[i] = i; }}/** * See which collection the element belongs to * * @param element to be viewed * @return The collection where element elements are located */private int Find (    int element) {return id[element]; }/** * Determine if two elements belong to a collection * * @param firstelement first element * @param secondelement Second element * @return <c     Ode>boolean</code> returns True if it is. */public boolean isconnected (int firstelement, int secondelement) {return find (firstelement) = = Find (secondele    ment); }/** * Merges the collection of two elements, that is, connecting two elements * * @param firstelement first element * @param secondelement the second element */pub LIC void unionelements (int firstelement, int secondelement) {//Find the collection where firstelement is located int firstunion = Find (Fir        Stelement);        Find the collection where secondelement is located int secondunion = find (secondelement);        If these two are not the same collection, then merge. if (firstunion! = secondunion) {//iterates through the array, merging the original firstunion, secondunion into secondunion for (int i = 0; I & Lt This.size;                i++) {if (id[i] = = firstunion) {Id[i] = secondunion; }}}}/** * This collection uses an array implementation, in order to see the internal data more visually, using the print array */private void Printarr () {for (i        NT Id:this.id) {System.out.print (id + "\ t");    } System.out.println ();        } public static void Main (string[] args) {int n = 10;        Unionfind Union = new Unionfind (n);        System.out.println ("initial:");        Union.printarr ();        System.out.println ("Connected 5 6");        Union.unionelements (5, 6);        Union.printarr (); SYSTEM.OUT.PRINTLN ("Connect1 2 ");        Union.unionelements (1, 2);        Union.printarr ();        System.out.println ("Connected 2 3");        Union.unionelements (2, 3);        Union.printarr ();        System.out.println ("Connected 1 4");        Union.unionelements (1, 4);        Union.printarr ();        System.out.println ("Connected 1 6");        Union.unionelements (1, 6);        Union.printarr ();        System.out.println ("1 5 is connected:" + union.isconnected (1, 5));    System.out.println ("1 8 is connected:" + union.isconnected (1, 8)); }}
And look up: tree-shaped structure

Cond....

and set Optimization: path compression

Cond....

and check Set (Java implementation)

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.