mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-07-01 04:56:29 +00:00
Update DSU.md
This commit is contained in:
parent
076214cf50
commit
68f18d7ec7
@ -1,12 +1,12 @@
|
||||
Suppose, you are giving a programming contest and one of the problem is: You are given a number of vertices and a list of undirected unweighted edges between these vertices. Now the queries are to find whether there is a path from some vertex $u$ to $v$. Note that the whole graph may not be connected. How can you solve it?
|
||||
|
||||

|
||||

|
||||
|
||||
DFS, Right? Start DFS from either u or v and check if we can reach to the other vertex. Done!
|
||||
|
||||
But what if the graph is **dynamic**, means that apart from the path query, you are given another type of query which is, to add an edge in the graph. Now, how to solve it?
|
||||
|
||||

|
||||

|
||||
|
||||
Again DFS? Yes, you can add any number of edges and still check if there is a path from vertex u and v. But if you do that then you will get **TLE**(Time limit exceed).
|
||||
|
||||
@ -41,7 +41,7 @@ In the image below, $c$ is parent of $d$ and $a$ is parent of $c$.
|
||||
|
||||
**Note:** Below is just for visualization purpose, if you don't understand it right now. Don't worry, you will understand it by the end of the article.
|
||||
|
||||

|
||||

|
||||
|
||||
- **Root** is an element of a set whose parent is itself. It is unique per set.
|
||||
$a$ is the root element for the disjoint set above in the image.
|
||||
@ -62,7 +62,7 @@ MAKE-SET(x)
|
||||
Here X is the only element in the set so it is parent of itself.
|
||||
|
||||
The image below represents sets generated by this operation. Where each one having arrow coming to itself, which represents that it is its own parent right now. Each one has size of 1.
|
||||

|
||||

|
||||
|
||||
We are working with arrays, so the code to make $n$ sets is as below:
|
||||
|
||||
@ -90,7 +90,7 @@ The root basically represents a unique ID for a particular disjoint set. (Look a
|
||||
|
||||
If we apply $\text{Find}(d)$ or $\text{Find}(b)$ operation for the set in the image below, then it will return '$a$' which is a root element.
|
||||
|
||||

|
||||

|
||||
|
||||
Here the thing to note is that, the root element of a root element of any disjoint set is itself i.e., $root.parent = root$
|
||||
|
||||
@ -107,7 +107,7 @@ FIND(X)
|
||||
return x;
|
||||
```
|
||||
**Visualization**
|
||||

|
||||

|
||||
|
||||
---------------
|
||||
### Quiz Time
|
||||
@ -151,7 +151,7 @@ We have a technique named **"Path compression"**. The idea of the Path compressi
|
||||
|
||||
If we apply $\text{Find}(d)$ operation with the path compression, then the following thing will happen.
|
||||
|
||||

|
||||

|
||||
|
||||
How can we do it? It is easy, we just need a little modification in $\text{Find}(X)$.
|
||||
|
||||
@ -237,7 +237,7 @@ Yes, we have two standard techniques: **By size and By rank**.
|
||||
### By Size
|
||||
Union by size technique decides it based on the sizes of the sets. Everytime, the smaller size set is attatched to the larger size set.
|
||||
|
||||

|
||||

|
||||
|
||||
**Note:** The numbers in square bracket represents the size of the set below it.
|
||||
|
||||
@ -265,9 +265,9 @@ If both sets have same rank, then the resulting rank will be one greater. Otherw
|
||||
|
||||
Example 1:
|
||||
|
||||

|
||||

|
||||
Example 2:
|
||||

|
||||

|
||||
|
||||
**Pseudocode:**
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user