Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: DisjointSet #81

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading