Info
Why disjoint set is used ?? Let’s imagine a graph with two components.
DSU1
⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠
Text Elements
1
2
3
5
6
7
4
Link to original
- To find if
1
and5
belong to the same component we’ll doDFS
(O(N+E)) which is a brute force method.- Disjoint joint tells this in CONSTANT TIME.
- It’s used in dynamic graphs, graphs whose configurations keep on changing.
- It gives us functionalities like:
- findParent()
- Union - It basically connects two nodes during graph formation. There’re two methods for this:
- By Rank
- By Size
1.
- Imagine we did union till (6,7) and before we proceeds someone asks
4
&1
belong to the same component or not- The answer will be NO.
2.
- Now after all the operations are done,
4
&1
belong a single set.
Notes (Click to expand):
![[DSU-Notes.pdf |height=200]]
UNION BY RANK (LESS INTUITIVE):
UNION BY SIZE (MORE INTUITIVE):
- Because in unionByRank we initially calculate the rank with the height but after path compression that height destroys and rank is no more intuitive.
- Here, smaller size set attaches to larger size set.
- By merging the smaller set into the larger set, we minimize the depth of the resulting tree and keep the tree more balanced.