Update DSU.md

This commit is contained in:
Aakash Panchal 2020-05-19 02:38:35 +05:30 committed by GitHub
parent 076214cf50
commit 68f18d7ec7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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? 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?
![enter image description here](https://lh3.googleusercontent.com/H_pixss3v5apcsEkUmk_hAzOhgif-O43ce8IijOw3AhCmATXdw0QpG6eQCJEnmwcLs0NYUa96_XU) ![enter image description here](https://github.com/KingsGambitLab/Lecture_Notes/blob/master/articles/Akash%20Articles/md/Images/DSU/1.jpg)
DFS, Right? Start DFS from either u or v and check if we can reach to the other vertex. Done! 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? 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?
![](https://lh3.googleusercontent.com/AK-Y9QBBXX0mB1twpZdPPTA2gcEhPjKwAh0cOxGaXltv6S2xcup9HPF2CDpjvhBlp3v4IiS341lz) ![](https://github.com/KingsGambitLab/Lecture_Notes/blob/master/articles/Akash%20Articles/md/Images/DSU/2.jpg)
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). 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. **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.
![enter image description here](https://lh3.googleusercontent.com/iaVsRRMUzGUK-PlEl7gFtUoDnatg9O2tBF-gMJ_qm5FyNWJSWXnCL6jAxX5siijx1L57Tg-3A0HZ) ![enter image description here](https://github.com/KingsGambitLab/Lecture_Notes/blob/master/articles/Akash%20Articles/md/Images/DSU/3.jpg)
- **Root** is an element of a set whose parent is itself. It is unique per set. - **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. $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. 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. 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.
![enter image description here](https://lh3.googleusercontent.com/UW-R9Hbi7YaCOyrVd2F0ThzzQ9pAF1zqoASJDhGKjbBHN8P-dJJr4sZubW1csc97l6iQMo3L39Bc) ![enter image description here](https://github.com/KingsGambitLab/Lecture_Notes/blob/master/articles/Akash%20Articles/md/Images/DSU/4.jpg)
We are working with arrays, so the code to make $n$ sets is as below: 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. 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.
![enter image description here](https://lh3.googleusercontent.com/j1H9MBKoSzyQV_8ObjBOjD1W2Na57kYg8aGMrbI8dLepF2IIqbRJSKzccH7rgfWrBqgFJ3LtYzAN) ![enter image description here](https://github.com/KingsGambitLab/Lecture_Notes/blob/master/articles/Akash%20Articles/md/Images/DSU/5.jpg)
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$ 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; return x;
``` ```
**Visualization** **Visualization**
![enter image description here](https://lh3.googleusercontent.com/ex1uHjYzU0MXu0auog7GwQsAbawGVSROIvn0COglh33LHbHYFc8sTHnkn3Qgjb1FgJIaeLOz8Qig) ![enter image description here](https://github.com/KingsGambitLab/Lecture_Notes/blob/master/articles/Akash%20Articles/md/Images/DSU/6.jpg)
--------------- ---------------
### Quiz Time ### 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. If we apply $\text{Find}(d)$ operation with the path compression, then the following thing will happen.
![enter image description here](https://lh3.googleusercontent.com/ltQXkpZAjEO543ibrVodpMMZp2IHXVJ7Rjxevm2ztJQAC67UnvBeMmwEoIB9qZ0_2PgpSs98nWV9) ![enter image description here](https://github.com/KingsGambitLab/Lecture_Notes/blob/master/articles/Akash%20Articles/md/Images/DSU/7.jpg)
How can we do it? It is easy, we just need a little modification in $\text{Find}(X)$. 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 ### 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. 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.
![enter image description here](https://lh3.googleusercontent.com/O9Q-Sbfm2LvjdbEgVoUwWSVfs4vA9MLxCgNGuzOWiyWgE_j9O2NOgTmOrhlZb5QMI_nPgG5lDfIo) ![enter image description here](https://github.com/KingsGambitLab/Lecture_Notes/blob/master/articles/Akash%20Articles/md/Images/DSU/8.jpg)
**Note:** The numbers in square bracket represents the size of the set below it. **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 1:
![enter image description here](https://lh3.googleusercontent.com/6DjycKp_SUVdInkEVhT89_v_YcblSZAyHZaAxiAI60MN81f9ZNIZW2G0UjivAW2AIwDuH6z0EF0V) ![enter image description here](https://github.com/KingsGambitLab/Lecture_Notes/blob/master/articles/Akash%20Articles/md/Images/DSU/9.jpg)
Example 2: Example 2:
![enter image description here](https://lh3.googleusercontent.com/_q-grBb90uOEu9v2EH1TXsHcHZh6QtyBRroxDiLch0vMdzLPvcvTd_YGoRa85fZYTMG_Qo_JDxOl) ![enter image description here](https://github.com/KingsGambitLab/Lecture_Notes/blob/master/articles/Akash%20Articles/md/Images/DSU/10.jpg)
**Pseudocode:** **Pseudocode:**
``` ```