Skip to content

Commit 58d738b

Browse files
committed
Minor refactoring of disjointSet
1 parent e4e33fd commit 58d738b

File tree

4 files changed

+24
-32
lines changed

4 files changed

+24
-32
lines changed

src/dataStructures/disjointSet/quickFind/generalised/QuickFind.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package src.dataStructures.disjointSet.quickFind.generalised;
2+
23
import java.util.ArrayList;
34
import java.util.List;
45

56
/**
6-
* Implementation of quick-find algorithm; Turns a list of objects into a data structure that supports union operations
7+
* Implementation of quick-find structure; Turns a list of objects into a data structure that supports union operations
78
* @param <T> generic type of object to be stored
89
*/
910
public class QuickFind<T> {
@@ -28,8 +29,8 @@ public QuickFind(List<T> input) {
2829
* @param item to be added
2930
*/
3031
public void add(T item) {
31-
this.objects.add(item);
32-
this.identity.add(this.identity.size()); // identity of the new item
32+
objects.add(item);
33+
identity.add(identity.size()); // identity of the new item
3334
}
3435

3536
/**
@@ -38,19 +39,19 @@ public void add(T item) {
3839
* @param objTwo object in another component
3940
*/
4041
public void union(T objOne, T objTwo) {
41-
if (!this.objects.contains(objOne) || !this.objects.contains(objTwo)) {
42+
if (!objects.contains(objOne) || !objects.contains(objTwo)) {
4243
System.out.println("One or more of the objects do not exist!");
4344
return;
4445
}
45-
int idxOne = this.objects.indexOf(objOne);
46-
int compOne = this.identity.get(idxOne);
47-
int idxTwo = this.objects.indexOf(objTwo);
48-
int compTwo = this.identity.get(idxTwo);
46+
int idxOne = objects.indexOf(objOne);
47+
int compOne = identity.get(idxOne);
48+
int idxTwo = objects.indexOf(objTwo);
49+
int compTwo = identity.get(idxTwo);
4950

5051
int size = this.objects.size();
5152
for (int i = 0; i < size; i++) {
52-
if (this.identity.get(i) == compOne) {
53-
this.identity.set(i, compTwo);
53+
if (identity.get(i) == compOne) {
54+
identity.set(i, compTwo);
5455
}
5556
}
5657
}
@@ -61,13 +62,12 @@ public void union(T objOne, T objTwo) {
6162
* @return list of elements.
6263
*/
6364
public List<T> retrieveComponent(T objOne) {
64-
int idx = this.objects.indexOf(objOne);
65-
int comp = this.identity.get(idx);
66-
int size = this.objects.size();
65+
int idx = objects.indexOf(objOne);
66+
int comp = identity.get(idx);
6767
List<T> ret = new ArrayList<>();
68-
for (int i = 0; i < size; i++) {
69-
if (this.identity.get(i) == comp) {
70-
ret.add(this.objects.get(i));
68+
for (int i = 0; i < objects.size(); i++) {
69+
if (identity.get(i) == comp) {
70+
ret.add(objects.get(i));
7171
}
7272
}
7373
return ret;

src/dataStructures/disjointSet/quickFind/quick_find.iml

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/dataStructures/disjointSet/quickFind/simplified/QuickFind.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ public class QuickFind {
1111
private int size;
1212

1313
public QuickFind(int size) {
14-
// we will ignore index 0. So index 1 corresponds to element 1,
15-
// index 2 corresponds with element 2 and so on..
14+
// we will ignore index 0. So index 1 corresponds to element 1, index 2 corresponds with element 2 and so on.
1615
this.identity = new int[size + 1];
1716
this.size = size;
1817
for (int i = 0; i < size + 1; i++) {
@@ -26,10 +25,10 @@ public QuickFind(int size) {
2625
* @param to identity of the second element
2726
*/
2827
public void union(int fr, int to) {
29-
int updateComp = this.identity[fr];
30-
for (int i = 1; i < this.size + 1; i++) {
31-
if (this.identity[i] == updateComp) {
32-
this.identity[i] = this.identity[to]; // updates element i's identity to that of <to>
28+
int updateComp = identity[fr];
29+
for (int i = 1; i < size + 1; i++) {
30+
if (identity[i] == updateComp) {
31+
identity[i] = identity[to]; // updates element i's identity to that of <to>
3332
}
3433
}
3534
}
@@ -40,10 +39,10 @@ public void union(int fr, int to) {
4039
* @return all elements in the component
4140
*/
4241
public List<Integer> retrieveComponent(int element) {
43-
int id = this.identity[element];
42+
int id = identity[element];
4443
List<Integer> ret = new ArrayList<>();
4544
for (int i = 1; i < this.size + 1; i++) {
46-
if (this.identity[i] == id) {
45+
if (identity[i] == id) {
4746
ret.add(i);
4847
}
4948
}

src/dataStructures/disjointSet/weightedUnion/Union.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void combine(T obj1, T obj2) {
4949

5050
public boolean isSameComponent(T obj1, T obj2) {
5151
if (!items.containsKey(obj1) || !items.containsKey(obj2)) {
52-
System.out.println("Duuhh.. one or both of the items are not tracked.");
52+
System.out.println("One or both of the items are not tracked.");
5353
return false;
5454
}
5555
return items.get(obj1).findParent() == items.get(obj2).findParent();

0 commit comments

Comments
 (0)