|
| 1 | +## Overview: |
| 2 | +A way that we can easily check membership in an undirected graph is through the use of UnionFind aka. Disjoint Set. |
| 3 | +The word Union Find and Disjoint Set are relatively interchangable within the coding world with Disjoint Set reffering to |
| 4 | +the data structure and Union Find referring to the actual algorithm. This data structure plays an especially important role in Kruskal's algorithm for finding a minimum spanning tree as it can |
| 5 | +help determine if an undirected graph contains any cycles. |
| 6 | + |
| 7 | +## What is a Disjoint Set (Union Find)? |
| 8 | +A Disjoint set is essentially an undirected graph. It's a datastructure that stores a collection of disjoint (non-overlapping) sets. |
| 9 | +It stores a subsection of a set into disjoint subsets. |
| 10 | +The basic operations are as follows: |
| 11 | + |
| 12 | +### Find() |
| 13 | +Find the root of a given disjoint subset |
| 14 | + |
| 15 | +```python |
| 16 | + def find(self, a): |
| 17 | + while a != self.root[a]: |
| 18 | + a = self.root[a] |
| 19 | + return a |
| 20 | +``` |
| 21 | + |
| 22 | +### Union() |
| 23 | +Combine subsets into a larger subset |
| 24 | +```python |
| 25 | + def union(self, x, y): |
| 26 | + rootX = self.find(x) |
| 27 | + rootY = self.find(y) |
| 28 | + if rootX != rootY: |
| 29 | + for i in range(len(self.root)): |
| 30 | + if self.root[i] == rootY: |
| 31 | + self.root[i] = rootX |
| 32 | +``` |
| 33 | + |
| 34 | +At the initialization of a disjoint set each element represents a separate subset with its parent being the element itself. |
| 35 | + |
| 36 | +This is what's called the root array. |
| 37 | + |
| 38 | + |
| 39 | +## Algorithm of UnionFind in Python |
| 40 | +Every time we reach a new node, we will take the following steps: |
| 41 | +1. Call find(x), and find(y) to find the root of each of the subsets, with x and y representing the elements to combine. |
| 42 | +2. Loop through entire root array and update the root of y with the root of x |
| 43 | + |
| 44 | +## Time & Space Complexity |
| 45 | +* **Time Complexity:** |
| 46 | +Time complexity of the basic UnionFind algorithm is `O(N)`, for find and `O(N)` for union, where N is the number of elements |
| 47 | + |
| 48 | +However, depending on the uitilization of this algorithm it often can scale to `O(N^2)` as you often times find yourself doing |
| 49 | +N operations of Union. |
| 50 | + |
| 51 | + |
| 52 | +* **Space Complexity:** |
| 53 | +Since the initialization array is of length N. |
| 54 | + |
| 55 | +The space complexity of the UnionFind algorithm is `O(N)`, where N is the number of elements |
| 56 | + |
| 57 | +## Input & Output: |
| 58 | + |
| 59 | +At the start each element is the root of itself. |
| 60 | +```python |
| 61 | +uf = UnionFind(6) |
| 62 | + ``` |
| 63 | +<img width=50% src="../UnionFind/Images/basic_union/basic_union.png"> |
| 64 | + |
| 65 | +```python |
| 66 | +uf.union(0,1) |
| 67 | + ``` |
| 68 | +When we call the union function we update the root of one element to the root of the other. |
| 69 | + |
| 70 | +<img width=50% src="../UnionFind/Images/basic_union/basic_union_1.png"> |
| 71 | + |
| 72 | +```python |
| 73 | +uf.union(4,5) |
| 74 | + ``` |
| 75 | + |
| 76 | + |
| 77 | +<img width=50% src="../UnionFind/Images/basic_union/basic_union_2.png"> |
| 78 | + |
| 79 | +```python |
| 80 | +uf.union(1,4) |
| 81 | + ``` |
| 82 | + |
| 83 | + This also applies to all subsequent elements with the same root |
| 84 | + |
| 85 | +<img width=50% src="../UnionFind/Images/basic_union/basic_union_3.png"> |
| 86 | + |
0 commit comments