-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeightedQuickUnionPathCompressionUF.java
More file actions
93 lines (81 loc) · 2.33 KB
/
WeightedQuickUnionPathCompressionUF.java
File metadata and controls
93 lines (81 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
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
83
84
85
86
87
88
89
90
91
92
93
package wqupc;
import java.util.Scanner;
public class WeightedQuickUnionPathCompressionUF {
private int[] id; // id[i] = parent of i
private int[] sz; // sz[i] = number of objects in subtree rooted at i
private int count; // number of components
// Create an empty union find data structure with N isolated sets.
public WeightedQuickUnionPathCompressionUF(int N)
{
count = N;
id = new int[N];
sz = new int[N];
for (int i = 0; i < N; i++)
{
id[i] = i;
sz[i] = 1;
}
}
// Return the number of disjoint sets.
public int count()
{
return count;
}
// Return component identifier for component containing p
public int root(int p)
{
while (p != id[p])
{
id[p] = id[id[p]];
p = id[p];
}
return p;
}
// Are objects p and q in the same set?
public boolean connected(int p, int q)
{
return root(p) == root(q);
}
// Replace sets containing p and q with their union.
public void union(int p, int q)
{
int i = root(p);
int j = root(q);
if (i == j) return;
// make smaller root point to larger one
if(sz[i] < sz[j])
{
id[i] = j;
sz[j] += sz[i];
}
else
{
id[j] = i;
sz[i] += sz[j];
}
count--;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("No: of components = ");
int N = input.nextInt();
WeightedQuickUnionPathCompressionUF uf = new WeightedQuickUnionPathCompressionUF(N);
// 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.
while (input.hasNextInt())
{
int p = input.nextInt();
int q = input.nextInt();
if (uf.connected(p, q))
{
System.out.println("Already connected!");
continue;
}
uf.union(p, q);
System.out.println(p + " and " + q + " are now connected");
}
System.out.println(uf.count() + " components");
}
}