From f30a77adaa9a2e7806f37c9ddd3e6f7a0b91501b Mon Sep 17 00:00:00 2001 From: 4ndrelim Date: Fri, 5 Apr 2024 23:00:07 +0800 Subject: [PATCH] refactor: DisjointSet --- docs/team/profiles.md | 2 +- .../disjointSet/quickFind/DisjointSet.java | 14 ++++++++++++++ .../disjointSet/weightedUnion/DisjointSet.java | 15 +++++++++++++++ .../disjointSet/quickFind/DisjointSetTest.java | 10 ++++++++++ .../weightedUnion/DisjointSetTest.java | 10 ++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) diff --git a/docs/team/profiles.md b/docs/team/profiles.md index c9040cf1..e33f656d 100644 --- a/docs/team/profiles.md +++ b/docs/team/profiles.md @@ -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. | diff --git a/src/main/java/dataStructures/disjointSet/quickFind/DisjointSet.java b/src/main/java/dataStructures/disjointSet/quickFind/DisjointSet.java index f5a5dce8..d9ae7e64 100644 --- a/src/main/java/dataStructures/disjointSet/quickFind/DisjointSet.java +++ b/src/main/java/dataStructures/disjointSet/quickFind/DisjointSet.java @@ -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 generic type of object to be stored */ @@ -33,6 +34,19 @@ public DisjointSet(List 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(); } diff --git a/src/main/java/dataStructures/disjointSet/weightedUnion/DisjointSet.java b/src/main/java/dataStructures/disjointSet/weightedUnion/DisjointSet.java index 6a2d47ed..9f1419f1 100644 --- a/src/main/java/dataStructures/disjointSet/weightedUnion/DisjointSet.java +++ b/src/main/java/dataStructures/disjointSet/weightedUnion/DisjointSet.java @@ -10,6 +10,7 @@ * Turns a list of objects into a data structure that supports union operations. *

* Note that implementation below includes path compression. Refer to README for more details + * Note DS structure is not suited with duplicate elements! * * @param generic type of object to be stored */ @@ -39,6 +40,20 @@ public DisjointSet(List 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 diff --git a/src/test/java/dataStructures/disjointSet/quickFind/DisjointSetTest.java b/src/test/java/dataStructures/disjointSet/quickFind/DisjointSetTest.java index 7d0c1695..8338cecc 100644 --- a/src/test/java/dataStructures/disjointSet/quickFind/DisjointSetTest.java +++ b/src/test/java/dataStructures/disjointSet/quickFind/DisjointSetTest.java @@ -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 ds = new DisjointSet<>(lst); + Assert.assertEquals(ds.size(), 5); + + Assert.assertFalse(ds.find("andre", "kai ting")); + } + @Test public void find_shouldCorrectlyFindItself() { List lst = Arrays.asList("andre", "chang xian", "jun neng"); diff --git a/src/test/java/dataStructures/disjointSet/weightedUnion/DisjointSetTest.java b/src/test/java/dataStructures/disjointSet/weightedUnion/DisjointSetTest.java index 4e923091..77fccebb 100644 --- a/src/test/java/dataStructures/disjointSet/weightedUnion/DisjointSetTest.java +++ b/src/test/java/dataStructures/disjointSet/weightedUnion/DisjointSetTest.java @@ -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 ds = new DisjointSet<>(lst); + Assert.assertEquals(ds.size(), 5); + + Assert.assertFalse(ds.find("andre", "kai ting")); + } + @Test public void find_shouldCorrectlyFindItself() { List lst = Arrays.asList("andre", "chang xian", "jun neng");