/******************************************************************************
* Name: Andy Guna
* NetID: guna@princeton.edu
* Precept: P01
*
* Partner Name: N/A
* Partner NetID: N/A
* Partner Precept: N/A
*
* Operating system: Windows
* Compiler: drjava
* Text editor / IDE: notepad++
*
* Have you taken (part of) this course before: yes
* Have you taken (part of) the Coursera course Algorithm, Part I: no
*
* Hours to complete assignment (optional):
*
* Description: This example demonstrates the use of WeightedUnionFindUF, StdRandom and Stopwatch
* Classes. The code addresses the following problem. Given a set of N points, numbered
* from 0 ... N-1, if any two points are randomly selected and merged, when will the
* points 0 and N-1 be in the same set.
*
*******************************************************************************/
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
import edu.princeton.cs.algs4.Stopwatch;
import edu.princeton.cs.algs4.StdRandom;
public class UFExample1 {
public static void main(String[] args) {
Stopwatch Clock = new Stopwatch();
int N = Integer.parseInt(args[0]);
int nexttolast = N-1;
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);
System.out.println("connecting " + i + " and " + j);
}
if (UF1.connected(0,N-1)) {
System.out.println("\n EXITING ... now 0 and " + nexttolast + " are connected ");
break;
}
}
System.out.println("The elapsed time is " + Clock.elapsedTime());
}
}