/****************************************************************************** * Name: * NetID: * Precept: * * Partner Name: N/A * Partner NetID: N/A * Partner Precept: N/A * * Operating system: * Compiler: * Text editor / IDE: * * Have you taken (part of) this course before: * Have you taken (part of) the Coursera course Algorithm, Part I: * * Hours to complete assignment (optional): * * to run the code > java UFExample2 N T (where N is the data size and * T is the trials) ******************************************************************************/ import edu.princeton.cs.algs4.WeightedQuickUnionUF; import edu.princeton.cs.algs4.Stopwatch; import edu.princeton.cs.algs4.StdRandom; public class UFExample2 { public static void main(String[] args) { int N = Integer.parseInt(args[0]); // data size int T = Integer.parseInt(args[1]); // trials double count = 0; for (int k=1; k<=T; k++) { Stopwatch Clock = new Stopwatch(); WeightedQuickUnionUF UF1 = new WeightedQuickUnionUF(N); while (true) { int i = StdRandom.uniform(N); int j = StdRandom.uniform(N); if (!UF1.connected(i,j)){ UF1.union(i,j); } if (UF1.connected(0,N-1)) { // some message break; } } count += Clock.elapsedTime(); System.out.println("Trial " + k + " " + Clock.elapsedTime()); } System.out.println( "N=" + N + " average time: " + String.format("%.2g%n", count/(1.0*T))); } }