01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
// Exercise 1.5.7 (Solution published at http://algs4.cs.princeton.edu/)
package algs15;
import java.util.Arrays;
import stdlib.*;
/* **************************************************************************
* Compilation: javac QuickFindUF.java
* Execution: java QuickFindUF < input.txt
* Dependencies: StdIn.java StdOut.java
*
* Quick-find algorithm.
*
* % java QuickFindUF < largeUF.txt
* QuickFindUF # components: 6 [1054.198000] // 17 minutes
*
****************************************************************************/
public class QuickFindUF implements UF {
private int[] id;
private int count;
// instantiate N isolated components 0 through N-1
public QuickFindUF(int N) {
count = N;
id = new int[N];
for (int i = 0; i < N; i++)
id[i] = i;
}
// return number of connected components
public int count() {
return count;
}
// are elements p and q in the same component?
public boolean connected(int p, int q) {
return id[p] == id[q];
}
// Return component identifier for component containing p
public int find(int p) {
return id[p];
}
// merge components containing p and q
public void union(int p, int q) {
if (connected(p, q)) return;
int pid = id[p]; // loser
int qid = id[q]; // champion
for (int i = 0; i < id.length; i++)
if (id[i] == pid) id[i] = qid;
count--;
}
public String toString() { return Arrays.toString (id); }
public void toGraphviz() { GraphvizBuilder.ufToFile (id); }
public static void main(String[] args) {
boolean print = true;
StdIn.fromFile ("data/tinyUF.txt");
//StdIn.fromFile ("data/mediumUF.txt"); print = false;
//StdIn.fromFile ("data/largeUF.txt"); print = false;
int N = StdIn.readInt();
QuickFindUF uf = new QuickFindUF(N);
if (print) { uf.toGraphviz(); StdOut.println(" : " + uf); }
// read in a sequence of pairs of integers (each in the range 0 to N-1),
// calling find() for each pair: If the members of the pair are not already
// call union() and print the pair.
Stopwatch sw = new Stopwatch ();
while (!StdIn.isEmpty()) {
int p = StdIn.readInt();
int q = StdIn.readInt();
if (uf.connected(p, q)) continue;
uf.union(p, q);
if (print) { StdOut.println(p + " " + q + ": " + uf); uf.toGraphviz(); }
}
StdOut.format("QuickFindUF # components: %d [%f]\n", uf.count(), sw.elapsedTime ());
}
}
|