-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionFind.java
More file actions
123 lines (100 loc) · 3.87 KB
/
UnionFind.java
File metadata and controls
123 lines (100 loc) · 3.87 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package TimeAndSpace.DSA;
import java.util.*;
public class UnionFind {
private int[] parent;
private int[] size;
// Constructor: initializes the Union-Find data structure
public UnionFind(int n) {
parent = new int[n];
size = new int[n];
// Initially, each node is its own parent and has size 1
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
// Find operation with path compression
public int find(int p) {
if (parent[p] != p) {
parent[p] = find(parent[p]); // Path compression
}
return parent[p];
}
// Union operation with union by size
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
// If they are already in the same set, do nothing
if (rootP == rootQ) return;
// Union by size: attach the smaller tree under the larger tree
if (size[rootP] < size[rootQ]) {
parent[rootP] = rootQ;
size[rootQ] += size[rootP];
} else {
parent[rootQ] = rootP;
size[rootP] += size[rootQ];
}
}
// Check if two elements are in the same set
public boolean connected(int p, int q) {
return find(p) == find(q);
}
// Optional: For debugging purposes, print the parent array
public void printParent() {
System.out.println("Parent array: " + Arrays.toString(parent));
}
// Optional: For debugging purposes, print the size array
public void printSize() {
System.out.println("Size array: " + Arrays.toString(size));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
UnionFind uf = new UnionFind(n);
while (true) {
System.out.println("\nChoose an operation:");
System.out.println("1. Union (p, q)");
System.out.println("2. Find (p)");
System.out.println("3. Check if connected (p, q)");
System.out.println("4. Print Parent Array");
System.out.println("5. Print Size Array");
System.out.println("6. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter two elements to union (p q): ");
int p = scanner.nextInt();
int q = scanner.nextInt();
uf.union(p, q);
System.out.println("Union completed.");
break;
case 2:
System.out.print("Enter the element to find its root (p): ");
p = scanner.nextInt();
int root = uf.find(p);
System.out.println("Root of " + p + " is " + root);
break;
case 3:
System.out.print("Enter two elements to check if connected (p q): ");
p = scanner.nextInt();
q = scanner.nextInt();
boolean isConnected = uf.connected(p, q);
System.out.println("Are " + p + " and " + q + " connected? " + isConnected);
break;
case 4:
uf.printParent();
break;
case 5:
uf.printSize();
break;
case 6:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}