Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4f7e345

Browse files
Added README.md for union find
1 parent 88cdc9f commit 4f7e345

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Union Find (Disjoint Set Union) - Implementation and Use
2+
3+
## Table of Contents
4+
- [Why Union Find?](#why-union-find)
5+
- [Functions and Examples](#functions-and-examples)
6+
- [Setup](#setup)
7+
- [Additional Resources](#additional-resources)
8+
- [Leetcode Questions](#leetcode-questions)
9+
10+
## Why Union Find?
11+
Union Find is a popular data structure that allows us to solve many different types of graph
12+
problems. It works best with undirected graphs, and it allows us to figure out whether a node
13+
is connected to another node.
14+
15+
Some problems it can be used to solve:
16+
- Find the minimum spanning tree in a graph (Kruskal's)
17+
- Check if there is a path between two nodes
18+
- Finding redundant edges
19+
- Representing networks
20+
21+
22+
## Functions and Examples
23+
Union Find seems complex at first, but it is actually a lot easier when you understand that there are
24+
only two functions.
25+
- Find(n) : returns the parent of a node n
26+
- Union(n1, n2) : connects n1 and n2 if they are not previously connected
27+
28+
Let's look at an example!
29+
```python
30+
u = UnionFind(7) # create a UnionFind object with 7 nodes (numbered 0 to 6)
31+
32+
u.union(0, 1) # connects 0 and 1 together
33+
u.union(5, 6) # connects 5 and 6 together
34+
35+
u.find(1) # returns 0, since 0 is parent of 1
36+
u.find(5) # returns 5, since 5 is its own parent
37+
38+
u.union(1, 2) # connects 2 to the component 0-1
39+
u.find(2) # 2s parent is now 0
40+
41+
# Now our structure looks like this
42+
43+
# 0-1-2 3 4 5-6
44+
45+
u.union(1, 6) # first we find the parents of 1 and 6
46+
# parents are 0, and 5
47+
# connect the smaller component to the bigger
48+
# now 5's parent is 0
49+
50+
u.find(6) # now this goes:
51+
# 6 parent is 5 -> 5 parent is 0 -> 0 is its own parent
52+
```
53+
54+
And that's it! You can use the sample code to test different examples with Union Find.
55+
In the code, par keeps track of the parent of each node and rank keeps track of the size of
56+
each component.
57+
58+
## Setup
59+
60+
First clone the repo
61+
> `cd union_find` to get into this folder.
62+
> call the verify function anywhere, consider adding ``` if __name__ == '__main__'```
63+
> `python union_find.py` to run the demo
64+
65+
You can modify the structure in the verify function and play around with it.
66+
67+
## Additional Resources
68+
69+
Here are some resources I found useful when learning:
70+
- Neetcode Graph Videos on YouTube
71+
- William Fiset - Union Find Video on YouTube
72+
- Union Find Medium Article by Claire Lee
73+
- Union Find Visualizer - Visualgo
74+
75+
## Leetcode Questions
76+
- 200 - Number of Islands
77+
- 684 - Redundant Connection
78+
- 695 - Max Area of an Island
79+
- 827 - Making a Large Island
80+
- 2316 - Count Unreachable Pairs of Nodes in an Undirected Graph
81+
- 2421 - Maximum Score of a Good Path
82+
- 2709 - Greatest Common Divisor Traversal
83+
84+
I hope this was helpful. If there are any mistakes or issues or if you want to contribute to union find, feel free to contact me at rawateshaan0 [at] gmail [dot] com

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /