-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathQuickUnion.java
182 lines (140 loc) · 4.57 KB
/
QuickUnion.java
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package AlgorithmJava;
/**
Quick Union implementation V1
(Created by GPT)
Quick Union with Path Compression Example
Input: n = 5, edges = [[0,1],[1,2],[3,4]]
This input represents 5 nodes and 3 edges connecting them. The goal is to manage the union and find operations using Quick Union with path compression.
### Initial State
1. **Initialization**:
Each element is its own root.
```java
int[] root = new int[n];
for (int i = 0; i < n; i++) {
root[i] = i;
}
// root = [0, 1, 2, 3, 4]
```
### Union Operations
2. **Union(0, 1)**:
- Find the root of 0 and 1.
- Set the root of 1 to be the root of 0.
```java
root[1] = find(root, 0); // root[1] = 0
// root = [0, 0, 2, 3, 4]
```
3. **Union(1, 2)**:
- Find the root of 1 (which involves path compression).
- Set the root of 2 to be the root of 1.
```java
root[2] = find(root, 1); // root[2] = 0 (after path compression)
// root = [0, 0, 0, 3, 4]
```
4. **Union(3, 4)**:
- Find the root of 3 and 4.
- Set the root of 4 to be the root of 3.
```java
root[4] = find(root, 3); // root[4] = 3
// root = [0, 0, 0, 3, 3]
```
### Find Operations with Path Compression
Let's perform some `find` operations to understand how path compression works.
5. **Find(2)**:
- Find the root of 2.
- Root of 2 is 0 (which is already its root).
```java
int root2 = find(root, 2); // root2 = 0
```
6. **Find(1)**:
- Find the root of 1.
- Root of 1 is 0 (which is already its root).
```java
int root1 = find(root, 1); // root1 = 0
```
### Explanation of the Find Method with Path Compression
Here is the `find` method implementation again for reference:
```java
private int find(int[] root, int e) {
if (root[e] == e) {
return e; // The element is the root of its set
} else {
root[e] = find(root, root[e]); // Path compression
return root[e];
}
}
```
### Detailed Example with Steps
#### Union(0, 1)
- Initial roots: `[0, 1, 2, 3, 4]`
- Find root of 0: `find(root, 0) -> 0`
- Find root of 1: `find(root, 1) -> 1`
- Union: `root[1] = 0`
- Roots after union: `[0, 0, 2, 3, 4]`
#### Union(1, 2)
- Initial roots: `[0, 0, 2, 3, 4]`
- Find root of 1: `find(root, 1) -> 0`
- `root[1]` is 0, and `find(root, 0) -> 0`
- Path compression: `root[1] = 0`
- Find root of 2: `find(root, 2) -> 2`
- Union: `root[2] = 0`
- Roots after union: `[0, 0, 0, 3, 4]`
#### Union(3, 4)
- Initial roots: `[0, 0, 0, 3, 4]`
- Find root of 3: `find(root, 3) -> 3`
- Find root of 4: `find(root, 4) -> 4`
- Union: `root[4] = 3`
- Roots after union: `[0, 0, 0, 3, 3]`
### Final State
After performing all the union operations, the root array is:
```
root = [0, 0, 0, 3, 3]
```
This indicates two distinct sets:
- One set with root 0: {0, 1, 2}
- Another set with root 3: {3, 4}
### Checking Connections
To check if two nodes are connected, we use the `find` method to see if they share the same root.
For example:
- `find(root, 2) -> 0`
- `find(root, 1) -> 0`
Both nodes 2 and 1 have the same root (0), indicating they are in the same set.
*/
public class QuickUnion {
private int[] root;
// Initialize the root array where each element is its own root initially
public QuickUnion(int size) {
root = new int[size];
for (int i = 0; i < size; i++) {
root[i] = i;
}
}
// Find the root of the element
public int find(int e) {
if (root[e] == e) {
return e;
} else {
root[e] = find(root[e]); // Path compression
return root[e];
}
}
// Union operation to connect two elements
public void union(int e1, int e2) {
int rootE1 = find(e1);
int rootE2 = find(e2);
if (rootE1 != rootE2) {
root[rootE1] = rootE2; // Merge the sets
}
}
// Check if two elements are connected
public boolean connected(int e1, int e2) {
return find(e1) == find(e2);
}
public static void main(String[] args) {
QuickUnion qu = new QuickUnion(5);
qu.union(0, 1);
qu.union(1, 2);
qu.union(3, 4);
System.out.println("0 and 2 connected: " + qu.connected(0, 2)); // true
System.out.println("0 and 3 connected: " + qu.connected(0, 3)); // false
}
}