A weighted fast merging algorithm for path compression in Java algorithm 4--connectivity problems

Source: Internet
Author: User

It is very difficult to find an algorithm that guarantees linear time performance. There are also some simple methods to improve the weighted fast merging algorithm. Ideally, we want each node to be connected directly to its root, but don't want to change the number of lines as fast-merging algorithms do. We can simply connect all the nodes we have checked to the root to get close to the ideal situation. We can easily implement this method, the method name is a compressed path, when the merge operation, through each path to add a line, that is, the path encountered by each vertex corresponding to the ID array value is set to connect to the tree root. The net result is that the tree is almost completely flattened, approaching the ideal state obtained by the Quick Find method.

There are many other ways to implement path compression The following procedure implements path compression: to make each line in the path to the tree root point to the next node of the route, which is simpler than full path compression and achieves the same net result. We call this algorithm "weighted fast merging of binary path compression".

Public class quickuw{    public static void main (String[]  args)     {        int n=integer.parseint (args [0]);         int id[]=new int[N],sz[]=new int[N];         for (int i=0;i<n;i++)          {            id[i]=i;             sz[i]=1;         }        for (In.init ();! In.empty ();)         {             int i,j,p=in.getint (), Q=in.getint ();             for (I=p;i!=id[i];i=id[i])             id[i]=id[id[i]];             for (J=q;j!=id[i];j=id[j])              id[j]=id[id[j]];             if (i==j)  continue;            if ( SZ[I]&LT;SZ[J])             {                 id[i]=j;                 sz[j]+=sz[i];             }             else            {                     id[j]=i;                 sz[j]+=sz[j];             }              }}


This article is from the "Flying Fish Technology" blog, please be sure to keep this source http://flyingfish.blog.51cto.com/9580339/1622754

A weighted fast merging algorithm for path compression in Java algorithm 4--connectivity problems

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.