-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisJointSet.java
More file actions
130 lines (109 loc) · 3.84 KB
/
DisJointSet.java
File metadata and controls
130 lines (109 loc) · 3.84 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
124
125
126
127
128
129
130
import java.util.Scanner;
public class DisJointSet {
int parent[];
int rank[];
int size[];
public DisJointSet(int n) {
// Initialize arrays of size n
parent = new int[n];
rank = new int[n];
size = new int[n];
// Initialize parent, rank, and size for each element
for (int i = 0; i < n; i++) {
parent[i] = i; // Each element is its own parent
rank[i] = 0; // Initial rank is 0
size[i] = 1; // Initial size is 1
}
}
public int findparent(int node) {
if (node == parent[node]) {
return node;
}
// Path compression
return parent[node] = findparent(parent[node]);
}
public void union(int src, int dest) {
int u_p_src = findparent(src - 1); // Adjusting for 0-based indexing
int u_p_dest = findparent(dest - 1); // Adjusting for 0-based indexing
if (u_p_dest == u_p_src) {
return; // They are already in the same set
}
// Union by rank
if (rank[u_p_src] < rank[u_p_dest]) {
parent[u_p_src] = u_p_dest;
} else if (rank[u_p_dest] < rank[u_p_src]) {
parent[u_p_dest] = u_p_src;
} else {
parent[u_p_dest] = u_p_src;
rank[u_p_src]++;
}
}
public void union_by_size(int src, int dest) {
int u_p_src = findparent(src - 1);
int u_p_dest = findparent(dest - 1);
if (u_p_src == u_p_dest) {
return; // They are already in the same set
}
// Union by size
if (size[u_p_src] < size[u_p_dest]) {
parent[u_p_src] = u_p_dest;
size[u_p_dest] += size[u_p_src];
} else {
parent[u_p_dest] = u_p_src;
size[u_p_src] += size[u_p_dest];
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
DisJointSet d = new DisJointSet(n);
// Perform unions using union by rank
d.union(1, 2);
d.union(2, 3);
d.union(4, 5);
d.union(6, 7);
d.union(5, 6);
// Check if elements 3 and 7 are in the same set
if (d.findparent(3 - 1) == d.findparent(7 - 1)) {
System.out.println("Same (Union by rank)");
} else {
System.out.println("Not Same (Union by rank)");
}
// Union 3 and 7
d.union(3, 7);
// Print the parent array to see the result of the unions
System.out.print("Parent array after unions (Union by rank): ");
for (int i = 0; i < n; i++) {
System.out.print(d.parent[i] + " ");
}
System.out.println();
// Reset and perform unions using union by size
DisJointSet d2 = new DisJointSet(n);
d2.union_by_size(1, 2);
d2.union_by_size(2, 3);
d2.union_by_size(4, 5);
d2.union_by_size(6, 7);
d2.union_by_size(5, 6);
// Check if elements 3 and 7 are in the same set
if (d2.findparent(3 - 1) == d2.findparent(7 - 1)) {
System.out.println("Same (Union by size)");
} else {
System.out.println("Not Same (Union by size)");
}
// Union 3 and 7
d2.union_by_size(3, 7);
// Print the parent array and size array
System.out.print("Parent array after unions (Union by size): ");
for (int i = 0; i < n; i++) {
System.out.print(d2.parent[i] + " ");
}
System.out.println();
System.out.print("Size array after unions (Union by size): ");
for (int i = 0; i < n; i++) {
System.out.print(d2.size[i] + " ");
}
System.out.println();
sc.close(); // Close the scanner to prevent resource leak
}
}