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