Skip to content

Commit

Permalink
Added Basic UnionFind
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDoctor561 committed Oct 9, 2022
1 parent 4520d84 commit 167970b
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ def find(self, a):
a = self.root[a]
return a

uu = UnionFind(10)
uu = UnionFind(6)

print(uu.root)
uu.union(0,1)
uu.union(0,2)
uu.union(1,3)
uu.union(4,8)
uu.union(5,6)
uu.union(5,7)
uu.union(4,5)
uu.union(1,4)

print(uu.root)
print([i for i in range(10)])
#print([i for i in range(6)])
35 changes: 0 additions & 35 deletions Graphs/UnionFind/DisjointSet/02_path_compression.py

This file was deleted.

37 changes: 0 additions & 37 deletions Graphs/UnionFind/DisjointSet/03_quickfind_disjoint.py

This file was deleted.

44 changes: 0 additions & 44 deletions Graphs/UnionFind/DisjointSet/04_quickunion_disjoint.py

This file was deleted.

44 changes: 0 additions & 44 deletions Graphs/UnionFind/DisjointSet/05_optimized_disjoint.py

This file was deleted.

Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions Graphs/UnionFind/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
## Overview:
A way that we can easily check membership in an undirected graph is through the use of UnionFind aka. Disjoint Set.
The word Union Find and Disjoint Set are relatively interchangable within the coding world with Disjoint Set reffering to
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
help determine if an undirected graph contains any cycles.

## What is a Disjoint Set (Union Find)?
A Disjoint set is essentially an undirected graph. It's a datastructure that stores a collection of disjoint (non-overlapping) sets.
It stores a subsection of a set into disjoint subsets.
The basic operations are as follows:

### Find()
Find the root of a given disjoint subset

```python
def find(self, a):
while a != self.root[a]:
a = self.root[a]
return a
```

### Union()
Combine subsets into a larger subset
```python
def union(self, x, y):
rootX = self.find(x)
rootY = self.find(y)
if rootX != rootY:
for i in range(len(self.root)):
if self.root[i] == rootY:
self.root[i] = rootX
```

At the initialization of a disjoint set each element represents a separate subset with its parent being the element itself.

This is what's called the root array.


## Algorithm of UnionFind in Python
Every time we reach a new node, we will take the following steps:
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.
2. Loop through entire root array and update the root of y with the root of x

## Time & Space Complexity
* **Time Complexity:**
Time complexity of the basic UnionFind algorithm is `O(N)`, for find and `O(N)` for union, where N is the number of elements

However, depending on the uitilization of this algorithm it often can scale to `O(N^2)` as you often times find yourself doing
N operations of Union.


* **Space Complexity:**
Since the initialization array is of length N.

The space complexity of the UnionFind algorithm is `O(N)`, where N is the number of elements

## Input & Output:

At the start each element is the root of itself.
```python
uf = UnionFind(6)
```
<img width=50% src="../UnionFind/Images/basic_union/basic_union.png">

```python
uf.union(0,1)
```
When we call the union function we update the root of one element to the root of the other.

<img width=50% src="../UnionFind/Images/basic_union/basic_union_1.png">

```python
uf.union(4,5)
```


<img width=50% src="../UnionFind/Images/basic_union/basic_union_2.png">

```python
uf.union(1,4)
```

This also applies to all subsequent elements with the same root

<img width=50% src="../UnionFind/Images/basic_union/basic_union_3.png">

0 comments on commit 167970b

Please sign in to comment.