Skip to content

Commit c5b1f2e

Browse files
committed
docs: Add application and images
1 parent b9b45e7 commit c5b1f2e

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

docs/assets/images/QuickFind.png

108 KB
Loading
117 KB
Loading
267 KB
Loading

src/main/java/dataStructures/disjointSet/README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
## Background
44

55
A disjoint-set structure also known as a union-find or merge-find set, is a data structure
6-
keeps track of a partition of a set into disjoint (non-overlapping) subsets. In CS2040s, this
6+
keeps track of a partition of a set into disjoint (non-overlapping) subsets.
7+
8+
In CS2040s, this
79
is introduced in the context of checking for dynamic connectivity. For instance, Kruskal's algorithm
8-
in graph theory to find minimum spanning tree of the graph utilizes disjoint set to efficiently
9-
query if there exists a path between 2 nodes. <br>
10-
It supports 2 main operations:
10+
in graph theory to find minimum spanning tree of a graph utilizes disjoint set to efficiently
11+
query if there already exists a path between 2 nodes.
12+
13+
Generally, there are 2 main operations:
1114

1215
1. Union: Join two subsets into a single subset
1316
2. Find: Determine which subset a particular element is in. In practice, this is often done to check
@@ -17,16 +20,26 @@ The Disjoint Set structure is often introduced in 3 parts, with each iteration b
1720
previous either in time or space complexity (or both). More details can be found in the respective folders.
1821
Below is a brief overview:
1922

20-
1. Quick Find - Elements are assigned a component identity.
23+
1. **Quick Find** - Elements are assigned a component identity.
2124
Querying for connectivity and updating usually tracked with an internal array.
2225

23-
2. Quick Union - Component an element belongs to is now tracked with a tree structure. Nothing to enforce
26+
2. **Quick Union** - Component an element belongs to is now tracked with a tree structure. Nothing to enforce
2427
a balanced tree and hence complexity does not necessarily improve
2528
- Note, this is not implemented but details can be found under weighted union folder.
2629

27-
3. Weighted Union - Same idea of using a tree, but constructed in a way that the tree is balanced, leading to improved
30+
3. **Weighted Union** - Same idea of using a tree, but constructed in a way that the tree is balanced, leading to improved
2831
complexities. Can be further augmented with path compression.
2932

33+
## Applications
34+
Because of its efficiency and simplicity in implementing, Disjoint Set structures are widely used in practice:
35+
1. As mentioned, it is often sued as a helper structure for Kruskal's MST algorithm
36+
2. It can be used in the context of network connectivity
37+
- Managing a network of computers
38+
- Or even analyse social networks, finding communities and determining if two users are connected through a chain
39+
3. Can be part of clustering algorithms to group data points based on similarity - useful for ML
40+
4. It can be used to detect cycles in dependency graphs, e.g, software dependency management systems
41+
5. It can be used for image processing, in labelling different connected components of an image
42+
3043
## Notes
3144
Disjoint Set is a data structure designed to keep track of a set of elements partitioned into a number of
3245
non-overlapping subsets. It is not suited for handling duplicates and so our implementation ignores duplicates.

src/main/java/dataStructures/disjointSet/quickFind/README.md

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

33
## Background
44
Every object will be assigned a component identity. The implementation of Quick Find often involves
5-
an underlying array or hash map that tracks the component identity of each object. Here, we use a hash map.
5+
an underlying array or hash map that tracks the component identity of each object.
6+
Our implementation uses a hash map (to easily handle the case when objects aren't integers).
7+
8+
<div align="center">
9+
<img src="../../../../../../docs/assets/images/QuickFind.png" width="50%">
10+
<br>
11+
Credits: CS2040s Lecture Slides
12+
</div>
613

714
### Union
815
Between the two components, decide on the component d, to represent the combined set. Let the other

src/main/java/dataStructures/disjointSet/weightedUnion/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ are balanced.
2626
**Space**: O(n), implementation still involves wrapping the n elements with some structure / wrapper (e.g. Node class).
2727

2828
# Weighted Union
29+
2930
## Background
3031
Now, we improve upon the Quick Union structure by ensuring trees constructed are 'balanced'. Balanced
3132
trees have a nice property that the height of the tree will be upper-bounded by O(log(n)). This considerably speeds
@@ -34,6 +35,12 @@ We additionally track the size of each tree and ensure that whenever there is a
3435
tree becomes a child of the larger tree.**
3536
It can be mathematically shown the height of the tree is bounded by O(log(n)).
3637

38+
<div align="center">
39+
<img src="../../../../../../docs/assets/images/WeightedUnion.png" width="50%">
40+
<br>
41+
Credits: CS2040s Lecture Slides
42+
</div>
43+
3744
### Intuition - Why It Works
3845
First, it is crucial to know that Weighted Union's efficiency relies on careful **construction** of the trees. <br>
3946
Every element / object starts off in its own tree (i.e. its own component). When two components are merged, the smaller
@@ -71,3 +78,9 @@ Interested readers can find out more [here](https://dl.acm.org/doi/pdf/10.1145/3
7178
**Space**: O(n)
7279

7380
## Notes
81+
### Sample Demo - LeetCode 684: Redundant Connections
82+
The 'objects' in the question are given to be integers. Using int arrays instead of HashMap mapping in our
83+
implementation would suffice. But below uses the code exactly from our implementation to show its versatility.
84+
85+
<img src="../../../../../../docs/assets/images/WeightedUnionLeetCode.png">
86+

0 commit comments

Comments
 (0)