Union Find in 5 minutes - Data Structures & Algorithms

  Рет қаралды 197,144

Potato Coders

Potato Coders

3 жыл бұрын

This video covers one of the most popular data structures and algorithms topic "Union Find". This is an instruction showing how to run Union-Find on a graph, with examples and code. Union Find and Disjoint Set are not as difficult as we think! 😊
#graph #data_structures #algorithms #faang #Union-find #disjoint set #data-structures #recursion #leetcode #interview #algo #disjoint-sets #disjoint sets
Facebook: / potatocoders
Linkedin: / potato-coders

Пікірлер: 88
@potatocoders2028
@potatocoders2028 3 жыл бұрын
Comment down any questions below! Please give it a like and subscribe to support our channel. Cheers, Potato Coders 🙂
@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 Жыл бұрын
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 11 ай бұрын
​​​@@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 9 ай бұрын
@@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 8 ай бұрын
@@neerajvshahpartially correct, if it is only weighted without path compression it is O(log n).
@Dev_God
@Dev_God 2 жыл бұрын
Thanks for sharing! It's very clearly and efficiently explained!
@richtigmann1
@richtigmann1 7 ай бұрын
This was actually insanely helpful in understanding the concept. Thanks!
@adam23sp
@adam23sp 2 жыл бұрын
Amazing video, very easy to follow, thank you! 🎉
@donaldcodes
@donaldcodes 2 жыл бұрын
Love it man, short and clear!
@user-dc7oj3el7o
@user-dc7oj3el7o 2 жыл бұрын
So practical and helpful video=) Thanks
@samruddhisaoji7195
@samruddhisaoji7195 2 жыл бұрын
Loved this bite sized explanation!
@arjunv7055
@arjunv7055 Жыл бұрын
wonderful explanation! Thanks mate!
@paccini1
@paccini1 2 жыл бұрын
Wow, thanks a lot, great explanation!
@chrismiaomiao9426
@chrismiaomiao9426 Жыл бұрын
Amazing! It's very clear. Thank you
@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)).
@manojmpatil1269
@manojmpatil1269 Жыл бұрын
Perfectly explained. Thanks
@shamitfatin3516
@shamitfatin3516 2 жыл бұрын
under-rated video.. thanks for the explanation
@jubiliudu
@jubiliudu 2 жыл бұрын
Amazing! thank you!
@kmishy
@kmishy 2 жыл бұрын
Best illustration... please provide more content
@nghiapham1632
@nghiapham1632 8 ай бұрын
It so simple and straight forward. Thanks you so much
@luisgualpa7019
@luisgualpa7019 3 жыл бұрын
This video has saved me many hours of my life. Thank you for this.
@potatocoders2028
@potatocoders2028 3 жыл бұрын
I'm so glad!
@alesblaze4745
@alesblaze4745 2 жыл бұрын
wow , i must say that this was really easy and nice video , really nice explanation
@JamesBrodski
@JamesBrodski Жыл бұрын
Thank you so much this is great!
@nightmare_9
@nightmare_9 3 жыл бұрын
Crystal clear and precise...Thanks!!
@potatocoders2028
@potatocoders2028 3 жыл бұрын
Glad it was helpful! Also I like your profile picture :)
@jakobmusic9187
@jakobmusic9187 Жыл бұрын
fantastic video! thanks!!!
@danielwang8833
@danielwang8833 28 күн бұрын
Really clear and concise explanation. This video helped a ton.
@ahmetemin7572
@ahmetemin7572 Жыл бұрын
Short and clear, thanks
@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 Жыл бұрын
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.
@nguyennguyendinh5215
@nguyennguyendinh5215 4 ай бұрын
very clear! Thanks!
@Hdhdushzhz57743
@Hdhdushzhz57743 3 ай бұрын
Thank you! This was very helpful
@gdthegreat
@gdthegreat 2 жыл бұрын
thanks a lot for explaining in few minutes.
@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 2 жыл бұрын
+1
@MinhPham-eh6lr
@MinhPham-eh6lr Жыл бұрын
Thanks for the concise and easy-to-understand explanation!
@bozecki
@bozecki 2 жыл бұрын
Thanks bro, now I finally understand it for tommorow exam ;p
@Baal3033
@Baal3033 3 жыл бұрын
Awesome explanation, thank you!
@potatocoders2028
@potatocoders2028 3 жыл бұрын
Np :) I am glad it helped
@mykz814
@mykz814 Ай бұрын
bro... come back ur vids r so good 😭
@piotr780
@piotr780 Жыл бұрын
best explanation every ! :D
@persas1683
@persas1683 Жыл бұрын
Thank you very much. Nice video.
@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 2 жыл бұрын
@@potatocoders2028 it helped me a lot, thank you man!
@jashmerchant5121
@jashmerchant5121 4 ай бұрын
Solved my hours long quandary in 5 minutes. Thank you!
@samwilson4597
@samwilson4597 Жыл бұрын
Thank you so much man
@SterlingVanlaere
@SterlingVanlaere 3 жыл бұрын
Excellent video, thanks!
@potatocoders2028
@potatocoders2028 3 жыл бұрын
Glad it helped!
@civilspot5912
@civilspot5912 4 ай бұрын
Great explanation
@tudorradu5848
@tudorradu5848 2 жыл бұрын
THANK YOU!
@Shasol
@Shasol 3 жыл бұрын
How do you select the representative? I struggle to understand that part of it.
@tenminuteamateurhour
@tenminuteamateurhour 6 ай бұрын
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.
@DeepakSankar88
@DeepakSankar88 11 ай бұрын
Great explanation. Was able to recall what I had learnt a while back. Thank you!
@WebSurfingIsMyPastime
@WebSurfingIsMyPastime Жыл бұрын
This video was great! definitely not something that's very clearly covered in alot of other sources
@aadityakiran_s
@aadityakiran_s 11 ай бұрын
You got a video on path compression?
@jeresher6821
@jeresher6821 3 жыл бұрын
Best video I've found on UF. Thank you!
@potatocoders2028
@potatocoders2028 3 жыл бұрын
Wow, thank you for the great comment! :)
@waynegreen7970
@waynegreen7970 3 жыл бұрын
Good content!
@potatocoders2028
@potatocoders2028 3 жыл бұрын
Thank you. Let me know if there is any more content that you would like to see! 😀
@chrisdahms9682
@chrisdahms9682 Жыл бұрын
Bro this topic is confusing AF, but at least this vid makes it somewhat understandable
@JackSparrow-vv2uq
@JackSparrow-vv2uq 3 жыл бұрын
Thank you
@Rockyzach88
@Rockyzach88 Жыл бұрын
Thanks Mr. Tater.
@BobTheScience
@BobTheScience 6 ай бұрын
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.
@algorithmdatastructures9244
@algorithmdatastructures9244 3 жыл бұрын
Subbed 💛
@potatocoders2028
@potatocoders2028 3 жыл бұрын
Thank you ☺️
@pamp3657
@pamp3657 11 ай бұрын
good video
@chku
@chku 6 ай бұрын
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 ✨
@corgirun7892
@corgirun7892 Жыл бұрын
awesome
@mahlahlana
@mahlahlana 3 жыл бұрын
wow!
@KoScosss
@KoScosss Жыл бұрын
Thanks
@rasheedlewis1
@rasheedlewis1 9 ай бұрын
The Big-Theta runtime for find() would be Θ(n).
@samuraipiyush
@samuraipiyush 8 ай бұрын
beauty
@kmishy
@kmishy 2 жыл бұрын
algorithm was easy but you should write code and execute
@helloyogeshpatel
@helloyogeshpatel Жыл бұрын
That’s how Algo is studied
@errorless7921
@errorless7921 Жыл бұрын
Yes important is implementation
@Luca_040
@Luca_040 2 ай бұрын
Wer sieht das auch für's HAW Studium?
@jashmerchant5121
@jashmerchant5121 4 ай бұрын
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
@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 *?
@quantum_psi
@quantum_psi 2 жыл бұрын
This really didn't make sense
G-46. Disjoint Set | Union by Rank | Union by Size | Path Compression
42:15
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 272 М.
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 13 МЛН
Stay on your way 🛤️✨
00:34
A4
Рет қаралды 13 МЛН
How Dijkstra's Algorithm Works
8:31
Spanning Tree
Рет қаралды 1,3 МЛН
Top 6 Coding Interview Concepts (Data Structures & Algorithms)
10:51
Union Find Kruskal's Algorithm
6:15
WilliamFiset
Рет қаралды 198 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 374 М.
The moment we stopped understanding AI [AlexNet]
17:38
Welch Labs
Рет қаралды 791 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Disjoint Set | UNION and FIND
26:43
Techdose
Рет қаралды 110 М.
The hidden beauty of the A* algorithm
19:22
polylog
Рет қаралды 848 М.
Kumanda İle Bilgisayarı Yönetmek #shorts
0:29
Osman Kabadayı
Рет қаралды 1,9 МЛН
Запрещенный Гаджет для Авто с aliexpress 2
0:50
Тимур Сидельников
Рет қаралды 338 М.
Что делать если в телефон попала вода?
0:17
Лена Тропоцел
Рет қаралды 2,7 МЛН
$1 vs $100,000 Slow Motion Camera!
0:44
Hafu Go
Рет қаралды 28 МЛН
Это Xiaomi Su7 Max 🤯 #xiaomi #su7max
1:01
Tynalieff Shorts
Рет қаралды 2 МЛН