Algorithm (algorithms) version 4th practice 1.5.1

Source: Internet
Author: User

The change of the ID array:

0 1 2 3 4 5 6 7 8 9 10 components9 00 1 2 3 4 5 6 7 8 0 9 components3 40 1 244 5 6 7 8 0 8 components5 80 1 2) 4 486 7 8) 0 7 components7 20 1 2 4 4 8 628 0 6 components2 10 114 4 8 618 0 5 components5 70 1 1) 4 416 110 4 components0 341 1 4 4 1 6 1 143 components4 211 11 11 6 1 112 Components

Operation Count Analysis:

The Find () function accesses the array 1 times per Call.

The connected function calls the Find () function every two times, so it accesses the array 2 Times.

The Union function accesses the array in the following number of Times: 2 + N + (1,n-1). Where 2 calls the Find () function for two times, n is the n number of times group, (1,n-1) is the possible array substitution number.

     public Static voidmain (string[] Args) {//Initialize N components        intN =Stdin.readint (); Ufquickfind uf=NewUfquickfind (N);                Stdout.println (uf);  while(!Stdin.isempty ()) {                        intp =Stdin.readint (); intQ =Stdin.readint (); if(uf.connected (p, Q)) {//Ignore if connectedStdout.println (p + "" + q + "is Connected")); Continue; } uf.union (p, q);//Connect p and QStdout.println (p + "" +q);        Stdout.println (uf); }

For this client, the connected function and the union function are called once for each data pair.

The following log group accesses are analyzed:

9 0:2 + 2 + 10 + 1

3 4:2 + 2 + 10 + 1

5 8:2 + 2 + 10 + 1

7 2:2 + 2 + 10 + 1

2 1:2 + 2 + 10 + 2

5 7:2 + 2 + 10 + 2

0 3:2 + 2 + 10 + 2

4 2:2 + 2 + 10 + 4

Algorithm (algorithms) version 4th practice 1.5.1

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.