An Animated Introduction to the Union Find (Disjoint Set)

  Рет қаралды 679

Robbie Hammond

Robbie Hammond

Жыл бұрын

Resources:
- An article on Path Compression and Union By Rank. Somewhat technical, specifically in time/space analysis: people.cs.georgetown.edu/jtha...
- A GeeksForGeeks article on Path Compression and Union By Rank. Has actual implementations in a variety of languages: www.geeksforgeeks.org/union-b...
Notes:
- A major "issue" of the union find is that even though you can easily merge 2 subsets, you cannot (easily) split them. If whatever problem you're dealing with requires splitting components after they've been created, and there's no way to re-format the problem such that the splitting can be avoided, the union find most likely isn't the data structure you want to go with.
- Another equally valid way to implement representative elements is to make it such that their parent is set to some sentinel value (ex: -1, INT_MAX, etc) rather than set to itself.
- You can easily implement the union find without creating a struct/class for it. Just declare the parent[] list, the union and find functions, and you're good to go. I used a class in the video because I think it's easiest to envision the union find as a self-contained structure rather than just a list and 2 functions, but at the end of the day, it doesn't really matter.
- The union by rank optimization requires allocating another array/list of the same size as the number of elements inside the union find, so there may be rare instances when this optimization causes problems (i.e. if the max number of elements is extremely large). Path compression doesn't utilize any additional space that isn't already allocated for the unoptimized find function, so that isn't a concern there.
Here's a problem I like for which the union find can be pretty much directly applied:
codeforces.com/contest/1167/p...

Пікірлер
Disjoint Set | UNION and FIND
26:43
Techdose
Рет қаралды 110 М.
Looks realistic #tiktok
00:22
Анастасия Тарасова
Рет қаралды 106 МЛН
Зачем он туда залез?
00:25
Vlad Samokatchik
Рет қаралды 3,3 МЛН
DAD LEFT HIS OLD SOCKS ON THE COUCH…😱😂
00:24
JULI_PROETO
Рет қаралды 15 МЛН
Smart Sigma Kid #funny #sigma #comedy
00:26
CRAZY GREAPA
Рет қаралды 21 МЛН
The Algorithm Behind Spell Checkers
13:02
b001
Рет қаралды 409 М.
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 140 М.
Monte Carlo Simulation in 239 seconds
5:08
Nick Tech
Рет қаралды 1,7 М.
Faster than Rust and C++: the PERFECT hash table
33:52
strager
Рет қаралды 526 М.
But what is a neural network? | Chapter 1, Deep learning
18:40
3Blue1Brown
Рет қаралды 16 МЛН
Minimax: How Computers Play Games
14:37
Spanning Tree
Рет қаралды 199 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Python Hash Sets Explained & Demonstrated - Computerphile
18:39
Computerphile
Рет қаралды 112 М.
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 384 М.
Union Find Path Compression
6:36
WilliamFiset
Рет қаралды 118 М.
Looks realistic #tiktok
00:22
Анастасия Тарасова
Рет қаралды 106 МЛН