Algorithm (algorithms) version 4th practice 1.5.22

Source: Internet
Author: User

 packagecom.qiusongde;Importedu.princeton.cs.algs4.StdOut;Importedu.princeton.cs.algs4.StdStats; public classExercise1522 { public Static DoubleTIMETRIALFORQF (intTintNint[] edges) {Stopwatch Timer=NewStopwatch (); //repeat the experiment T times         for(intt = 0; T < t; t++) {edges[t]=ERDOSRENYI.COUNTBYQF (N); }                returnTimer.elapsedtime (); }         public Static DoubleTimetrialforweiqu (intTintNint[] edges) {Stopwatch Timer=NewStopwatch (); //repeat the experiment T times         for(intt = 0; T < t; t++) {edges[t]=Erdosrenyi.countbyweiqu (N); }                returnTimer.elapsedtime (); }         public Static DoubleMeanint[] edges) {        returnStdstats.mean (edges); }     public Static voidmain (string[] Args) {intT = Integer.parseint (args[0]); intedgesqf[] =New int[T]; intedgesqu[] =New int[T]; DoublePREVQF = TIMETRIALFORQF (T, 125, edgesqf); DoublePrevqu = Timetrialforweiqu (T, 125, edgesqu);  for(intN = 250;true; N + =N) {DoubleTIMEQF =TIMETRIALFORQF (T, N, edgesqf); Doubletimequ =timetrialforweiqu (T, N, edgesqu); DoubleMeanqfconnect =mean (edgesqf); DoubleMeanquconnect =mean (edgesqu); stdout.printf ("%6d%7.1f%7.1f%7.1f%7.1f\n", n, meanqfconnect, timeqf/prevqf, meanquconnect, timequ/prevqu); PREVQF=timeqf; Prevqu=timequ; }    }}

 packagecom.qiusongde;Importedu.princeton.cs.algs4.StdOut;Importedu.princeton.cs.algs4.StdRandom;Importedu.princeton.cs.algs4.StdStats;Importedu.princeton.cs.algs4.UF;Importedu.princeton.cs.algs4.WeightedQuickUnionUF; public classErdosrenyi { public Static intCountbyuf (intN) {intedges = 0; UF UF=NewUF (N);  while(uf.count () > 1) {            inti =Stdrandom.uniform (N); intj =Stdrandom.uniform (N);            Uf.union (i, j); Edges++; }                returnedges; }         public Static intCOUNTBYQF (intN) {intedges = 0; Ufquickfind uf=NewUfquickfind (N);  while(uf.count () > 1) {            inti =Stdrandom.uniform (N); intj =Stdrandom.uniform (N);            Uf.union (i, j); Edges++; }                returnedges; }         public Static intCountbyqu (intN) {intedges = 0; Ufquickunion uf=Newufquickunion (N);  while(uf.count () > 1) {            inti =Stdrandom.uniform (N); intj =Stdrandom.uniform (N);            Uf.union (i, j); Edges++; }                returnedges; }         public Static intCountbyweiqu (intN) {intedges = 0; Weightedquickunionuf uf=NewWeightedquickunionuf (N);  while(uf.count () > 1) {            inti =Stdrandom.uniform (N); intj =Stdrandom.uniform (N);            Uf.union (i, j); Edges++; }                returnedges; }         public Static voidmain (string[] Args) {intn = integer.parseint (args[0]);//Number of vertices            intTrials = Integer.parseint (args[1]);//Number of Trials            int[] edges =New int[trials]; //Repeat the experiment trials times             for(intt = 0; T < trials; t++) {edges[t]=Countbyuf (n); }            //report StatisticsSTDOUT.PRINTLN ("the" N ln n = "+ 0.5 * n *Math.log (n)); Stdout.println ("mean =" +Stdstats.mean (edges)); Stdout.println ("stddev =" +Stdstats.stddev (edges)); }}

 package   com.qiusongde;  public  class   Stopwatch { private  final  long   start;  public   Stopwatch () {start  = System.currenttimemillis ();  public  double   elapsedtime () { long  now = System        . Currenttimemillis ();     return  (now-start)/1000.0; }    }

Results:

      761.1     2.9   761.4     1.0 1698.6  2.8 1717.8 5.2 3739.0     3.8  3737.7     2.0  8184.8     3.7 8174.3  2.1     4000  17773.4     3.9 17799.1     2.2  8000 38312.6     4.1 38229.6     2.2

Algorithm (algorithms) version 4th practice 1.5.22

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.