Skip to content

Commit

Permalink
Merge pull request #81 from 4ndrelim/branch-refactorDS
Browse files Browse the repository at this point in the history
refactor: DisjointSet
  • Loading branch information
junnengsoo authored Apr 6, 2024
2 parents f5bc79f + f30a77a commit f09df8f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/team/profiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

| Name | Description/About | Website (LinkedIn/GitHub/Personal) | Contributions |
|-----------|-------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|-------------------------------------------------------------|
| Andre | Aspiring ML engineer. Developing this with wonderful ex-students. | You can find me [here](https://4ndrelim.github.io)! | Team lead |
| Andre | Aspiring ML engineer. Developing this with wonderful ex-students. | You can find me [here](https://4ndrelim.github.io) | Team lead |
| 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.. |
| Changxian | DevOps is right up his alley! | ... | Hashing variants! BTS DevOps - configure Gradle & workflows |
| 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. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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

/**
* Constructor to initialize Disjoint Set with a known array of objects.
* @param objects
*/
public DisjointSet(T[] objects) {
identifier = new HashMap<>();
int size = objects.length;
for (int i = 0; i < size; i++) {
// internally, component identity is tracked with integers
identifier.put(objects[i], identifier.size()); // each obj initialize with a unique identity using size;
}
}

public int size() {
return identifier.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Turns a list of objects into a data structure that supports union operations.
* <p>
* Note that implementation below includes path compression. Refer to README for more details
* Note DS structure is not suited with duplicate elements!
*
* @param <T> generic type of object to be stored
*/
Expand Down Expand Up @@ -39,6 +40,20 @@ public DisjointSet(List<T> objects) {
}
}

/**
* Constructor to initialize Disjoint Set structure with a known array of objects.
* @param objects
*/
public DisjointSet(T[] objects) {
parents = new HashMap<>();
size = new HashMap<>();
for (int i = 0; i < objects.length; i++) {
T obj = objects[i];
parents.put(obj, obj); // initially, every object forms a tree, with itself as the root
size.put(obj, 1); // each tree has size 1 at the start
}
}

/**
* Internal helper method to find the root (identifier) of an object. Note that path compression has been included.
* A point of concern might be performing path compression would require updating the sizes tracked by each node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public void construct_shouldCorrectlyInitializeNonEmpty() {
Assert.assertFalse(ds.find("andre", "kai ting"));
}

@Test
public void construct_shouldCorrectlyInitializeNonEmptyArray() {
String[] lst = new String[] { "andre", "chang xian", "jun neng", "kai ting", "shu heng" };

DisjointSet<String> ds = new DisjointSet<>(lst);
Assert.assertEquals(ds.size(), 5);

Assert.assertFalse(ds.find("andre", "kai ting"));
}

@Test
public void find_shouldCorrectlyFindItself() {
List<String> lst = Arrays.asList("andre", "chang xian", "jun neng");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public void construct_shouldCorrectlyInitializeNonEmpty() {
Assert.assertFalse(ds.find("andre", "kai ting"));
}

@Test
public void construct_shouldCorrectlyInitializeNonEmptyArray() {
String[] lst = new String[] { "andre", "chang xian", "jun neng", "kai ting", "shu heng" };

DisjointSet<String> ds = new DisjointSet<>(lst);
Assert.assertEquals(ds.size(), 5);

Assert.assertFalse(ds.find("andre", "kai ting"));
}

@Test
public void find_shouldCorrectlyFindItself() {
List<String> lst = Arrays.asList("andre", "chang xian", "jun neng");
Expand Down

0 comments on commit f09df8f

Please sign in to comment.