Disjoint Set Data Structure - Union Find Tutorial

  Рет қаралды 12,855

Stable Sort

Stable Sort

3 жыл бұрын

In this tutorial we will learn all about a data structure called Disjoint-Set. Sometimes it’s also referred to as Union-Find or Merge-Find Set.
This is a remarkably simple data structure yet it has the power to solve certain problems that are otherwise very difficult to deal with. To better understand its purpose, let’s consider the following problem:
Suppose you are implementing a new social network, similar to Facebook. There are users, each of which could be friends with some number of other users. In this illustration, lines connecting 2 users indicated that those two users are friends and thus form a group. When a user from one group makes a friend with a user from another group, then those two groups become connected and from that point on they are considered to have been merged into a single large group. Meaning, all of the users in that one large group are now friends with one another.
In more concrete terms, imagine that each user has an integer ID and you are given a list of integer pairs. Each integer pair indicates that those two particular users are friends.
When you have millions and millions of users, the challenge is to answer the following questions:
Given two user IDs, can you check if they are friends?
What is the size of the largest group of friends?
Alternate formulations of the same problem are used in coding interviews. Here are a couple of samples:
leetcode 547 "Number of Provinces":
leetcode.com/problems/number-...
hackerrank "Components in a graph":
www.hackerrank.com/challenges...
Java implementation of Disjoint Set with Union Find operations:
bitbucket.org/StableSort/play...
More information on Ackermann function:
en.wikipedia.org/wiki/Ackerma...
Music: bensound.com

Пікірлер: 64
@yoshi9358
@yoshi9358 3 жыл бұрын
Using the negative scale to store size is a great idea! Love your videos
@stablesort
@stablesort 3 жыл бұрын
Thanks! Let me also mention that the idea behind this implementation isn't mine - I saw it back in college =)
@raydot
@raydot 3 ай бұрын
Awesome! Been reading about this for two weeks now and this is the first time I've understood the point/purpose of a disjoint set.
@ravishankarprasad2320
@ravishankarprasad2320 3 жыл бұрын
Your videos are best value for time. To the point, with all the necessary details and visuals making it easy to understand and visualize, and no rambling, and wastage of time in writing/typing.
@shantanutripathi
@shantanutripathi 3 жыл бұрын
4
@stablesort
@stablesort 2 жыл бұрын
Thanks for compliment! That's what I strive for =)
@a..b1343
@a..b1343 3 жыл бұрын
Hi, your videos helped me a lot when I was looking for a job! I know it takes a lot of effort for these, just wanted to let you know we appreciate it! I think if you made videos every week or 2 you would explode, but understand you're probably busy with your full time commitments. Thanks
@stablesort
@stablesort 3 жыл бұрын
That's great! Congrats on scoring a job! I wish I could pump out a new video every couple of weeks... But you would not believe how many hours it takes me to make one. I am sure I'll get more efficient at the whole editing/recording/animations/etc. as I go, but for now, it's a slow process =)
@uimbtw6300
@uimbtw6300 Жыл бұрын
@@stablesort you haven't made a video since though :(
@jean-lucsedits4319
@jean-lucsedits4319 Жыл бұрын
Better explanation then the first suggestion when searching "union find", thank you
@cripz4203
@cripz4203 3 жыл бұрын
Loved the video! Can you make one on the topic flows, cuts, bridges, articulation point, dfs tree?
@stablesort
@stablesort 3 жыл бұрын
I appreciate your topic suggestions - adding them to my list!
@rajneeshverma4026
@rajneeshverma4026 3 жыл бұрын
one of the best explanation, precise and compact implementation 👍
@stablesort
@stablesort 2 жыл бұрын
Thanks for the compliment and please check out the full force code, linked in the description. :)
@rajneeshverma4026
@rajneeshverma4026 2 жыл бұрын
@@stablesort saw the source code, very neat. I implemented same in GoLang :)
@Vaaaaadim
@Vaaaaadim 3 жыл бұрын
Neat. By delaying the update of the parent index for as long as possible, you can deduplicate work. Some values will have some parts of their paths compressed for free, provided that union operations are frequent enough. As well it's a very simple representation. In a worst case scenario though, you could imagine that every time two sets are merged, every single element of the resulting set has the find operation performed on it. For this kind of input, it would be no better(asymptotically) than a naive solution. The naive solution that comes to mind for me is that you can have a hashmap which maps values to the id of the hashset they belong to, have a hashmap which maps set id's to hashsets, and the find and union implementation would be obvious. That being said, the data structure you present would have far less overhead in comparison to two hashmaps and many hashsets.
@stablesort
@stablesort 3 жыл бұрын
Hi Vadim! Right, right, you could juggle hashsets, allocate IDs for them, then when merging have another hashmap that can look up an IDs of a hashset given some other IDs, etc. etc. It's possible to take this route but it gets complicated =) By the way, the implementation shown in the video is also not the *most* efficient. But I went with it as I think it's the most readable. Anyway, I feel like this is a good data structure to keep in the "tool box" and it can certainly make your life a lot easier in an interview situation.
@V000idZer000
@V000idZer000 Жыл бұрын
@@stablesort However to allocate a list to save the path is way more costly, than simply following the pointers another time to do the compression 😉
@sumeetagarwal6561
@sumeetagarwal6561 3 жыл бұрын
thanks! I actually had this DS on my list to look up and learn
@stablesort
@stablesort 3 жыл бұрын
Good timing!
@adai4035
@adai4035 3 жыл бұрын
Love your videos - the most intuitive ones I've ever watched. More please? :P
@stablesort
@stablesort 3 жыл бұрын
Thank you! Will do!
@shantanutripathi
@shantanutripathi 3 жыл бұрын
4
@kevinsmirnov264
@kevinsmirnov264 3 жыл бұрын
boah, ur videos burn through the matter. Pure delight. I hope u can keep the quality up. Are u planning on doin a video on global alignment? Since u already covered the string matching it would fit (imo).
@stablesort
@stablesort 3 жыл бұрын
Thanks for the topic suggestion, Kevin. I have not heard the term "global alignment" - do you have a link that I can read up on?
@kevinsmirnov264
@kevinsmirnov264 3 жыл бұрын
​@@stablesort Maybe I'm confusing it with sequence alignment..? As I understoud global alignment deals with finding the overall difference between two strings and is the generalization of "longest common subsequence" (LCS), and algorithms like the Levensthein-distance as well as the Needleman-Wunsch-Algorithm fall into it. I sadly have no outstanding online sources except for wiki :/
@jeffwangvids
@jeffwangvids 10 ай бұрын
great vid thanks learning this in class right now
@aNeutrino
@aNeutrino 3 ай бұрын
Thank you for your effort on the algorithm video. I wanted to share that I found the background music quite distracting, to the point where it was impossible for me to focus on the content.
@stablesort
@stablesort 3 ай бұрын
Thanks for the feedback. A few other people have also mentioned the same thing. Point noted - I'll be sure to keep the volume down for the next one.
@89aehf
@89aehf 3 жыл бұрын
Amazing video!
@stablesort
@stablesort 3 жыл бұрын
Thanks for the compliment!
@backistall3452
@backistall3452 2 жыл бұрын
Thanks!
@astamurkirillin6727
@astamurkirillin6727 3 жыл бұрын
Great explanations, Andrey! Your channel is awesome!
@stablesort
@stablesort 3 жыл бұрын
Thanks, Астамур! I do appreciate you leaving a few good words =)
@shantanutripathi
@shantanutripathi 3 жыл бұрын
4
@ManojKumaryOyOmaDy
@ManojKumaryOyOmaDy 3 жыл бұрын
You also given links for practice, thank you.
@stablesort
@stablesort 3 жыл бұрын
Happy practicing!
@vibhu613
@vibhu613 2 жыл бұрын
Lottssss of love..... Keep it up... 💛❤🧡💛💚
@y.violetguo5305
@y.violetguo5305 3 жыл бұрын
can you make a video on the kernel mapping function? your animations are amazing!
@stablesort
@stablesort 3 жыл бұрын
I haven't seen it before, which is great - that's the kind of stuff I like to investigate and learn myself. Thanks for suggesting the topic!
@madhukiranattivilli2321
@madhukiranattivilli2321 2 жыл бұрын
Hi Andrev, What quality content u have‼‼‼ I'm ammmmazed‼‼‼ There are many yt channels with amazing DS ALGO content, but I guess u r having the best presentation -- graphics, animations, etc I would love to know, if u r interested to share, how u make ur videos. May be u can make a video on this aspect. I will deeply appreciate. And, please don't stop making videos on programming concepts (DS, ALGO, problem solving techniques). Please make millions of them. I became ur fan. I'm gonna watch every videos of yours. Cheers Madhukiran 《btw, did I write ur name correctly❓ -- Andrev》
@stablesort
@stablesort 2 жыл бұрын
Thank you for such warm words! To answer your question, I use my cell phone for filming and I use PowerPoint for creating the animations/presentation. I am sure there are better tools out there but hey, this does the job =) Cheers! Andre
@NarpatHSuthar
@NarpatHSuthar 3 жыл бұрын
Amazing video, Great explanation :)
@stablesort
@stablesort 3 жыл бұрын
Glad you liked it!
@shantanutripathi
@shantanutripathi 3 жыл бұрын
4
@ketanyadav5618
@ketanyadav5618 2 жыл бұрын
How this can be implemented when node are dynamic and edges also can change for a node
@user-uu1bx4xv1s
@user-uu1bx4xv1s 3 жыл бұрын
BREAKING NEWS!!!!!!!!!!!! Stable sort updated new episode!
@stablesort
@stablesort 3 жыл бұрын
Haha, it's really nice of you to say that! I hope you like it and find it useful.
@imadudin2489
@imadudin2489 2 жыл бұрын
Still waiting for the next video..!!
@noahibarra3379
@noahibarra3379 2 жыл бұрын
And when we needed him the most…he vanished
@spyrowolf2211
@spyrowolf2211 Жыл бұрын
Thank you for the video!, Please try avoid using catchy music in the videos, it makes it hard to concentrate (if you have to use music please try use very subtle or barely noticeably)
@worldwide6626
@worldwide6626 3 жыл бұрын
Please make more videos
@shantanutripathi
@shantanutripathi 3 жыл бұрын
4
@stablesort
@stablesort 3 жыл бұрын
I wish to have more time in a day =)
@SauravSahu01
@SauravSahu01 2 жыл бұрын
Your content and voice are always super cool. But addition of the music in this video is distracting. Could have avoided background sound. Thanks.
@stablesort
@stablesort 2 жыл бұрын
cool, thanks for the feedback. I'll tone it down a little for the next one.
@moritzgro2442
@moritzgro2442 2 жыл бұрын
please keep going :(
@stablesort
@stablesort 2 жыл бұрын
I'll make you a deal - watch my kids for a few hours while I write up the script and shoot them video :) Life has gotten a bit more complicated here but more vids are on their way. Just need to find some free time!
@moritzgro2442
@moritzgro2442 2 жыл бұрын
@@stablesort You and I might appreciate that deal, but the kids wouldn't :D
@stablesort
@stablesort 2 жыл бұрын
@@moritzgro2442 😁
@dernuntius679
@dernuntius679 11 ай бұрын
The music in 0:46 is distracting.
@stablesort
@stablesort 11 ай бұрын
Got it. Several people have mentioned this. Will keep that in mind for the next one. Thanks for the feedback.
@gouadriasouheyl910
@gouadriasouheyl910 11 ай бұрын
bro never add music background when explaining
@stablesort
@stablesort 11 ай бұрын
Noted. Will do better for the next one!
Union Find in 5 minutes - Data Structures & Algorithms
5:46
Potato Coders
Рет қаралды 197 М.
Convex Hull Algorithm - Graham Scan and Jarvis March tutorial
7:24
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 13 МЛН
Summer shower by Secret Vlog
00:17
Secret Vlog
Рет қаралды 9 МЛН
Best KFC Homemade For My Son #cooking #shorts
00:58
BANKII
Рет қаралды 62 МЛН
Segment Tree Data Structure - Min Max Queries - Java source code
8:47
Disjoint Set | UNION and FIND
26:43
Techdose
Рет қаралды 110 М.
Union Find - Union and Find Operations
10:53
WilliamFiset
Рет қаралды 246 М.
An Animated Introduction to the Union Find (Disjoint Set)
8:11
Robbie Hammond
Рет қаралды 678
The Algorithm Behind Spell Checkers
13:02
b001
Рет қаралды 409 М.
Disjoint Sets
14:28
Mary Elaine Califf
Рет қаралды 2 М.
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 13 МЛН