Skip to content

Commit f30a77a

Browse files
committed
refactor: DisjointSet
1 parent f5bc79f commit f30a77a

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

docs/team/profiles.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
| Name | Description/About | Website (LinkedIn/GitHub/Personal) | Contributions |
44
|-----------|-------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|-------------------------------------------------------------|
5-
| Andre | Aspiring ML engineer. Developing this with wonderful ex-students. | You can find me [here](https://4ndrelim.github.io)! | Team lead |
5+
| Andre | Aspiring ML engineer. Developing this with wonderful ex-students. | You can find me [here](https://4ndrelim.github.io) | Team lead |
66
| Kai Ting | Likes algorithms and a committed TA! | [Linkedin](https://www.linkedin.com/in/kai-ting-ho-425181268/) | Cool sorting and obscure trees! B-Trees, ORS.. |
77
| Changxian | DevOps is right up his alley! | ... | Hashing variants! BTS DevOps - configure Gradle & workflows |
88
| Shu Heng | Interested in ML, aspiring researcher. | No website but here's my [Linkedin](https://www.linkedin.com/in/yeoshuheng), please give me a job :< | CS Fundamentals! Stacks and queues! RB-tree. |

src/main/java/dataStructures/disjointSet/quickFind/DisjointSet.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
/**
99
* Implementation of quick-find structure; Turns a list of objects into a data structure that supports union operations
10+
* Note DS structure is not suited with duplicate elements!
1011
*
1112
* @param <T> generic type of object to be stored
1213
*/
@@ -33,6 +34,19 @@ public DisjointSet(List<T> objects) {
3334
}
3435
}
3536

37+
/**
38+
* Constructor to initialize Disjoint Set with a known array of objects.
39+
* @param objects
40+
*/
41+
public DisjointSet(T[] objects) {
42+
identifier = new HashMap<>();
43+
int size = objects.length;
44+
for (int i = 0; i < size; i++) {
45+
// internally, component identity is tracked with integers
46+
identifier.put(objects[i], identifier.size()); // each obj initialize with a unique identity using size;
47+
}
48+
}
49+
3650
public int size() {
3751
return identifier.size();
3852
}

src/main/java/dataStructures/disjointSet/weightedUnion/DisjointSet.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Turns a list of objects into a data structure that supports union operations.
1111
* <p>
1212
* Note that implementation below includes path compression. Refer to README for more details
13+
* Note DS structure is not suited with duplicate elements!
1314
*
1415
* @param <T> generic type of object to be stored
1516
*/
@@ -39,6 +40,20 @@ public DisjointSet(List<T> objects) {
3940
}
4041
}
4142

43+
/**
44+
* Constructor to initialize Disjoint Set structure with a known array of objects.
45+
* @param objects
46+
*/
47+
public DisjointSet(T[] objects) {
48+
parents = new HashMap<>();
49+
size = new HashMap<>();
50+
for (int i = 0; i < objects.length; i++) {
51+
T obj = objects[i];
52+
parents.put(obj, obj); // initially, every object forms a tree, with itself as the root
53+
size.put(obj, 1); // each tree has size 1 at the start
54+
}
55+
}
56+
4257
/**
4358
* Internal helper method to find the root (identifier) of an object. Note that path compression has been included.
4459
* A point of concern might be performing path compression would require updating the sizes tracked by each node

src/test/java/dataStructures/disjointSet/quickFind/DisjointSetTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ public void construct_shouldCorrectlyInitializeNonEmpty() {
2424
Assert.assertFalse(ds.find("andre", "kai ting"));
2525
}
2626

27+
@Test
28+
public void construct_shouldCorrectlyInitializeNonEmptyArray() {
29+
String[] lst = new String[] { "andre", "chang xian", "jun neng", "kai ting", "shu heng" };
30+
31+
DisjointSet<String> ds = new DisjointSet<>(lst);
32+
Assert.assertEquals(ds.size(), 5);
33+
34+
Assert.assertFalse(ds.find("andre", "kai ting"));
35+
}
36+
2737
@Test
2838
public void find_shouldCorrectlyFindItself() {
2939
List<String> lst = Arrays.asList("andre", "chang xian", "jun neng");

src/test/java/dataStructures/disjointSet/weightedUnion/DisjointSetTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ public void construct_shouldCorrectlyInitializeNonEmpty() {
2424
Assert.assertFalse(ds.find("andre", "kai ting"));
2525
}
2626

27+
@Test
28+
public void construct_shouldCorrectlyInitializeNonEmptyArray() {
29+
String[] lst = new String[] { "andre", "chang xian", "jun neng", "kai ting", "shu heng" };
30+
31+
DisjointSet<String> ds = new DisjointSet<>(lst);
32+
Assert.assertEquals(ds.size(), 5);
33+
34+
Assert.assertFalse(ds.find("andre", "kai ting"));
35+
}
36+
2737
@Test
2838
public void find_shouldCorrectlyFindItself() {
2939
List<String> lst = Arrays.asList("andre", "chang xian", "jun neng");

0 commit comments

Comments
 (0)