Skip to content

Commit 8150b13

Browse files
authored
Merge pull request #4 from priyansh14/master
Added learn link in the main README.md
2 parents 256208d + 07ed69e commit 8150b13

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

Disjoint_Sets.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Disjoint Sets
2+
Disjoint Sets are those sets of elements where no value or element is present in more than one set i.e there are no common elements and no overlapping. This data structure supports three major operations :
3+
1. Find ()
4+
2. Union()
5+
3. make_set()
6+
7+
8+
9+
## Explanation / Working
10+
Union-Find works on sets via trees.Each tree represents a set and root is the representative of set.
11+
12+
- Initially every element is a single set i.e a tree with only one vertex.
13+
- An array is created such that every index of the array represents a tree and the value at that index represents its ancestor/parent.
14+
- For this we use the make_set() function as follows :
15+
16+
```sh
17+
def make_set(vertex):
18+
19+
parent[vertex]=vertex
20+
21+
```
22+
23+
#### Find :
24+
The find() function takes a vertext as input and returns the representative of the set that the input vertex belongs to. We simply traverse from the input vertext to its parent and repeat until we fin the representative.
25+
26+
```sh
27+
def find_set(vertex):
28+
29+
if(vertex==parent[vertex]):
30+
return v
31+
else
32+
return find_set(parent[vertex]) #Recursive function
33+
34+
```
35+
36+
#### Union Function :
37+
It takes 2 sets as input and then we find representative of both the inputs.
38+
If they are same,then we do nothing as they are already in the same set.Otherwise,we make the representative of any one tree the parent of the combined set of both trees.
39+
40+
```sh
41+
def union_set(a,b):
42+
a = find_set(a)
43+
b = find_set(b)
44+
if(a! = b):
45+
parent[b] = a; #COMBINING THE TREES
46+
```
47+
48+
## Analysis
49+
50+
Since the union and make set function takes constant time and the find function takes linear time in traversing N elements , the overall complexity becomes O(1) + O(N) = O(N) as we only take the largest term and ignore other terms. However, this can be improved by introducting certain modifications explained below.
51+
52+
## Modifications
53+
54+
1. Path Compression :
55+
In this method, we simply set the parent of each visited vertex directly to its representative. Code :
56+
```sh
57+
def find_set(vertex):
58+
59+
if(vertex==parent[vertex]):
60+
return v
61+
else
62+
return parent[vertex] = find_set(parent[vertex])
63+
```
64+
2. Union by Rank/Weight:
65+
Another approach for reducing time complexity is that we sdefine how the combination will take place.We can merge the trees on the basis of greater rank or more weight. This means that the tree with less number of nodes will be merged into the bigger tree.
66+
67+
## Analysis after modifications :
68+
The time complexity after applying both of the modifications is O(Ackermann function(N)) which is approximately a constant. So we can say that complexity is O(1).
69+
70+
71+
# Applications
72+
73+
- Kruskal's Algorithm.
74+
- Finding cycles in a graph.
75+
- Maze Generation
76+
- Online maintenance of biconnected components
77+
- Lowest Common Ancestor Problem
78+
- Diving an image into sets of different color.
79+
- Efficient Connected component labeling or CCL algorithms use the Union-Find data structure. In real life when photoshop allows you to "grab" different objects in an image and differentiate between an object and "background" in either an assisted or fully automated way they are probably using the connected components technique. CCL is extensively used in image analysis, from automated segmentation of cancer cells to segmentation of persons or objects of interest in surveillance.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This repository contains Data structures, Algorithms and their common usecases i
1818
### Data Structures-
1919
* Trie Data Structure [Code](Trie_Data_Structure.py) [Learn](Trie_Data_Structure.md)
2020
* Segment Tree (For sum) (Java + Python) [Code](segment_Tree.py)
21-
* Union Find Data Structures (Disjoint Set Data Structure) [Code](UnionFindDS.py) [Learn](Disjoint Sets.md)
21+
* Union Find Data Structures (Disjoint Set Data Structure) [Code](UnionFindDS.py) [Learn](Disjoint_Sets.md)
2222
* Binary Search Tree [Code](BST.py)
2323

2424
## Learning Python ( the pythonic way)

0 commit comments

Comments
 (0)