Union Find in 5 minutes - Data Structures & Algorithms

  Рет қаралды 247,705

Potato Coders

Potato Coders

Күн бұрын

Пікірлер: 104
@potatocoders2028
@potatocoders2028 4 жыл бұрын
Comment down any questions below! Please give it a like and subscribe to support our channel. Cheers, Potato Coders 🙂
@cartooncartel6493
@cartooncartel6493 4 ай бұрын
Bro you cooked
@robirahman
@robirahman 2 жыл бұрын
You forgot to mention you have to union the shorter tree into the longer tree! Otherwise the trees in a set of n elements can be up to n elements long (just a straight chain, instead of branching) and you don't get log(n) complexity.
@tunguyenxuan8296
@tunguyenxuan8296 2 жыл бұрын
true. if we represent the group as a tree, it will take O(n) for find function in worst case. We better to represent a group as a trie, where all children point to one root.
@neerajvshah
@neerajvshah Жыл бұрын
@@tunguyenxuan8296 Trie's don't change the runtime complexity, a trie is just a type of tree (keep in mind not all trees are binary trees, a trie is just an k-ary tree). As Robi mentioned it's path compression which makes union find efficient. This video is incorrect - O(logn) is never the time complexity for union find. It's either O(n) without path compression or O(a(n)) with path compression.
@lightlysal
@lightlysal Жыл бұрын
​​​@@neerajvshahWhat's a(n) mean in your explanation? Is it the amount of times we "compress" a parent? I thought when people say the time complexity of union find they mean O(log N) on average over all use cases
@sirmonke8946
@sirmonke8946 Жыл бұрын
@@lightlysal a(n) is the reverse ackermann function. It grows insanely slow (slower than any log) and can basically be rounded to constant time
@MrKlaygomes
@MrKlaygomes Жыл бұрын
@@neerajvshahpartially correct, if it is only weighted without path compression it is O(log n).
@BobTheScience
@BobTheScience 11 ай бұрын
To optimize it, the find function should set the parent of it's parent to find(x). (This will probably help a lot) function find(x): if Parent[x] != x: parent[x] = find(parent[x]) return parent[x] If I'm wrong please tell me.
@nomealow
@nomealow 4 ай бұрын
Yes, this is called path compression, by setting the parent of x and all of the ancestors of x to be the root ancestor of x. This makes it so that find(), for that particular tree in the future, will be O(1) instead of being dependent on the tree depth
@luisgualpa7019
@luisgualpa7019 3 жыл бұрын
This video has saved me many hours of my life. Thank you for this.
@potatocoders2028
@potatocoders2028 3 жыл бұрын
I'm so glad!
@richtigmann1
@richtigmann1 Жыл бұрын
This was actually insanely helpful in understanding the concept. Thanks!
@tenminuteamateurhour
@tenminuteamateurhour Жыл бұрын
Correction, this does not run in O(logn), but in O(n). To get O(logn) optimization, you need to use union by rank or by size.
@Coffeycup27
@Coffeycup27 3 жыл бұрын
Awesome video, thank you! Question though; isn't the find operation O(n) time? I'm reading around that it isn't until you use 'Union By Rank' that it becomes O(log(n)).
@jakubucinski
@jakubucinski 2 жыл бұрын
There is a little technicality, that when you do find(x), you want to do path compression, by connecting each vertex on the corresponding branch (the branch where x is) to the root. This results in subsequent find operations being cheaper (and you only double your cost one time).
@rm-stl
@rm-stl 2 жыл бұрын
I’ve seen this done but never heard it explained. It looked odd to have a side effect in a find(). Makes perfect sense now. Thanks
@stevenhicks3321
@stevenhicks3321 Жыл бұрын
I also noticed this and thought it may be better to use sets if you don't care about the three structure of the disjoineted set.
@pieterjanvandecasteele135
@pieterjanvandecasteele135 3 жыл бұрын
I was always stuck with the implementation of UF, now I finally got it, thanks!
@potatocoders2028
@potatocoders2028 3 жыл бұрын
You're welcome!
@vishaldhawan9236
@vishaldhawan9236 3 жыл бұрын
+1
@danielwang8833
@danielwang8833 6 ай бұрын
Really clear and concise explanation. This video helped a ton.
@nightmare_9
@nightmare_9 3 жыл бұрын
Crystal clear and precise...Thanks!!
@potatocoders2028
@potatocoders2028 3 жыл бұрын
Glad it was helpful! Also I like your profile picture :)
@estebanzavala9533
@estebanzavala9533 12 күн бұрын
wow really good preparing for an upcoming interview
@jerrygord3131
@jerrygord3131 13 күн бұрын
This was really helpful, thank you!
@nghiapham1632
@nghiapham1632 Жыл бұрын
It so simple and straight forward. Thanks you so much
@Anirozannay
@Anirozannay 3 жыл бұрын
You explained it so well! Thank you for your video!
@potatocoders2028
@potatocoders2028 3 жыл бұрын
Thank you! We hope it has helped!
@tudorradu5848
@tudorradu5848 3 жыл бұрын
@@potatocoders2028 it helped me a lot, thank you man!
@송예은-h7b
@송예은-h7b 3 жыл бұрын
So practical and helpful video=) Thanks
@kmishy
@kmishy 3 жыл бұрын
Best illustration... please provide more content
@Shasol
@Shasol 3 жыл бұрын
How do you select the representative? I struggle to understand that part of it.
@UsamaAziz-lb7ky
@UsamaAziz-lb7ky 4 ай бұрын
Great video, worth adding path compression as well.
@jashmerchant5121
@jashmerchant5121 10 ай бұрын
Solved my hours long quandary in 5 minutes. Thank you!
@shamitfatin3516
@shamitfatin3516 3 жыл бұрын
under-rated video.. thanks for the explanation
@vinitsunita
@vinitsunita 3 ай бұрын
Awesome explaination
@chrisdahms9682
@chrisdahms9682 Жыл бұрын
Bro this topic is confusing AF, but at least this vid makes it somewhat understandable
@manojmpatil1269
@manojmpatil1269 Жыл бұрын
Perfectly explained. Thanks
@donaldcodes
@donaldcodes 2 жыл бұрын
Love it man, short and clear!
@alesblaze4745
@alesblaze4745 2 жыл бұрын
wow , i must say that this was really easy and nice video , really nice explanation
@gregoriuswillson4153
@gregoriuswillson4153 Ай бұрын
Great content bro
@samruddhisaoji7195
@samruddhisaoji7195 2 жыл бұрын
Loved this bite sized explanation!
@JamesBrodski
@JamesBrodski 2 жыл бұрын
Thank you so much this is great!
@chrismiaomiao9426
@chrismiaomiao9426 Жыл бұрын
Amazing! It's very clear. Thank you
@bozecki
@bozecki 3 жыл бұрын
Thanks bro, now I finally understand it for tommorow exam ;p
@civilspot5912
@civilspot5912 10 ай бұрын
Great explanation
@ahmetemin7572
@ahmetemin7572 Жыл бұрын
Short and clear, thanks
@SterlingVanlaere
@SterlingVanlaere 3 жыл бұрын
Excellent video, thanks!
@potatocoders2028
@potatocoders2028 3 жыл бұрын
Glad it helped!
@piotr780
@piotr780 2 жыл бұрын
best explanation every ! :D
@Baal3033
@Baal3033 3 жыл бұрын
Awesome explanation, thank you!
@potatocoders2028
@potatocoders2028 3 жыл бұрын
Np :) I am glad it helped
@Hdhdushzhz57743
@Hdhdushzhz57743 9 ай бұрын
Thank you! This was very helpful
@aadityakiran_s
@aadityakiran_s Жыл бұрын
You got a video on path compression?
@Dev_God
@Dev_God 2 жыл бұрын
Thanks for sharing! It's very clearly and efficiently explained!
@OtitoAmuga
@OtitoAmuga 29 күн бұрын
This not efficient. "Set the root of one tree to the child of another" Another what? Another node? Another number? Another graph? Lol 😅
@OtitoAmuga
@OtitoAmuga 29 күн бұрын
Mediocre at best
@Dev_God
@Dev_God 28 күн бұрын
@@OtitoAmuga Thanks! I'll get to know more about this.
@jeresher6821
@jeresher6821 3 жыл бұрын
Best video I've found on UF. Thank you!
@potatocoders2028
@potatocoders2028 3 жыл бұрын
Wow, thank you for the great comment! :)
@persas1683
@persas1683 Жыл бұрын
Thank you very much. Nice video.
@mykz814
@mykz814 7 ай бұрын
bro... come back ur vids r so good 😭
@waynegreen7970
@waynegreen7970 4 жыл бұрын
Good content!
@potatocoders2028
@potatocoders2028 4 жыл бұрын
Thank you. Let me know if there is any more content that you would like to see! 😀
@gdthegreat
@gdthegreat 2 жыл бұрын
thanks a lot for explaining in few minutes.
@jashmerchant5121
@jashmerchant5121 10 ай бұрын
Python code: # WITH Path Compression def find(self, parent, x): # finds root or representative of disjoint-set/union if parent[x] != x: parent[x] = self.find(parent, parent[x]) # path compression step return parent[x] def union(self, parent, x, y): root_x = self.find(parent, x) root_y = self.find(parent, y) parent[root_x] = root_y # WITHOUT Path Compression def find(self, parent, x): # finds root or representative of disjoint-set/union if parent[x] != x: return self.find(parent, parent[x]) return parent[x] def union(self, parent, x, y): root_x = self.find(parent, x) root_y = self.find(parent, y) parent[root_x] = root_y
@austecon6818
@austecon6818 4 ай бұрын
Thanks and other implementations include rank. Does it matter?
@arjunv7055
@arjunv7055 Жыл бұрын
wonderful explanation! Thanks mate!
@jubiliudu
@jubiliudu 3 жыл бұрын
Amazing! thank you!
@adam23sp
@adam23sp 2 жыл бұрын
Amazing video, very easy to follow, thank you! 🎉
@collyyang9664
@collyyang9664 3 ай бұрын
good, thank you very much
@paccini1
@paccini1 2 жыл бұрын
Wow, thanks a lot, great explanation!
@samwilson4597
@samwilson4597 2 жыл бұрын
Thank you so much man
@DeepakSankar88
@DeepakSankar88 Жыл бұрын
Great explanation. Was able to recall what I had learnt a while back. Thank you!
@chku
@chku Жыл бұрын
This video is so gud but looks like the channel ain't active anymkre 🥺💔 I really loved the explanation n quality so thankuu n hope u r doing well ✨
@WebSurfingIsMyPastime
@WebSurfingIsMyPastime Жыл бұрын
This video was great! definitely not something that's very clearly covered in alot of other sources
@Rockyzach88
@Rockyzach88 Жыл бұрын
Thanks Mr. Tater.
@pamp3657
@pamp3657 Жыл бұрын
good video
@MinhPham-eh6lr
@MinhPham-eh6lr 2 жыл бұрын
Thanks for the concise and easy-to-understand explanation!
@tudorradu5848
@tudorradu5848 3 жыл бұрын
THANK YOU!
@rasheedlewis1
@rasheedlewis1 Жыл бұрын
The Big-Theta runtime for find() would be Θ(n).
@mehraan8592
@mehraan8592 4 күн бұрын
beautiful
@algorithmdatastructures9244
@algorithmdatastructures9244 4 жыл бұрын
Subbed 💛
@potatocoders2028
@potatocoders2028 4 жыл бұрын
Thank you ☺️
@corgirun7892
@corgirun7892 2 жыл бұрын
awesome
@Luca_040
@Luca_040 8 ай бұрын
Wer sieht das auch für's HAW Studium?
@kmishy
@kmishy 3 жыл бұрын
algorithm was easy but you should write code and execute
@helloyogeshpatel
@helloyogeshpatel 2 жыл бұрын
That’s how Algo is studied
@errorless7921
@errorless7921 Жыл бұрын
Yes important is implementation
@mahlahlana
@mahlahlana 3 жыл бұрын
wow!
@KoScosss
@KoScosss 2 жыл бұрын
Thanks
@emerald_eyes
@emerald_eyes 3 күн бұрын
Didn't know I'd be learning computer science from Jaqen H'ghar
@JackSparrow-vv2uq
@JackSparrow-vv2uq 3 жыл бұрын
Thank you
@samuraipiyush
@samuraipiyush Жыл бұрын
beauty
@moshebixenshpaner5094
@moshebixenshpaner5094 2 жыл бұрын
There are two issues here: 1. When performing union, you set parent of the smaller set to the representative of the larger set. 2. When finding a path, you compress the path on your way back, improving O(logn) ops to O(log* n) in average.
@CSSFACE
@CSSFACE 2 жыл бұрын
what do you mean by O(log* n) what is the *?
@supratimdas1006
@supratimdas1006 3 ай бұрын
​@@CSSFACE study
@OtitoAmuga
@OtitoAmuga 29 күн бұрын
Bro speaking not exactly matching what he's saying. I guess that's why he's a potato coder.
@quantum_psi
@quantum_psi 3 жыл бұрын
This really didn't make sense
@jakobmusic9187
@jakobmusic9187 2 жыл бұрын
fantastic video! thanks!!!
@nguyennguyendinh5215
@nguyennguyendinh5215 10 ай бұрын
very clear! Thanks!
Union Find Path Compression
6:36
WilliamFiset
Рет қаралды 127 М.
Маусымашар-2023 / Гала-концерт / АТУ қоштасу
1:27:35
Jaidarman OFFICIAL / JCI
Рет қаралды 390 М.
24 Часа в БОУЛИНГЕ !
27:03
A4
Рет қаралды 7 МЛН
Vampire SUCKS Human Energy 🧛🏻‍♂️🪫 (ft. @StevenHe )
0:34
Alan Chikin Chow
Рет қаралды 138 МЛН
COMPUTER SCIENCE explained in 17 Minutes
16:49
Wacky Science
Рет қаралды 1,7 МЛН
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Top 5 Most Common Graph Algorithms for Coding Interviews
13:01
DISJOINT SETS || WEIGHTED UNION || COLLAPSING FIND
15:27
t v nagaraju Technical
Рет қаралды 37 М.
An Animated Introduction to the Union Find (Disjoint Set)
8:11
Robbie Hammond
Рет қаралды 2,2 М.
100+ Computer Science Concepts Explained
13:08
Fireship
Рет қаралды 2,7 МЛН
Disjoint Set | UNION and FIND
26:43
Techdose
Рет қаралды 114 М.
Маусымашар-2023 / Гала-концерт / АТУ қоштасу
1:27:35
Jaidarman OFFICIAL / JCI
Рет қаралды 390 М.